复制和重命名最新文件 [英] Copy and Rename newest file
问题描述
我想将最新文件从源路径复制到目标路径,然后重命名如下:
I want to copy latest file from source path to target Path and then rename as below:
目标路径:C; \User\Client1\FinalReports
Target Path : C;\User\Client1\FinalReports
源路径:C:\User\Client1\Reports\ReportFolderA(来自报告文件夹A的文件应以File1.csv重命名为目标文件夹)
C:\User\Client1\Reports\ReportFolderB(来自Report FolderB的文件应该以File2.csv重命名为目标文件夹)
C:\User\Client1\Reports\ReportFolderD (来自Report FolderD的文件应该以File4.csv重命名为目标文件夹)
C:\User\Client1\Reports\ReportFolderF(来自Report FolderF的文件应以File5.csv重命名为目标文件夹)
Source Path: C:\User\Client1\Reports\ReportFolderA (file from Report FolderA should be renamed to target Folder as File1.csv) C:\User\Client1\Reports\ReportFolderB (file from Report FolderB should be renamed to target Folder as File2.csv) C:\User\Client1\Reports\ReportFolderD (file from Report FolderD should be renamed to target Folder as File4.csv) C:\User\Client1\Reports\ReportFolderF (file from Report FolderF should be renamed to target Folder as File5.csv)
C:\User\Client1\Reports源路径固定后跟变量ReportFolderA,ReportFolderB.etc ...我们只能设置一个源路径
"C:\User\Client1\Reports" source Path is fixed followed by variable ReportFolderA, ReportFolderB.etc..so we can set only one source path in the script.
我需要一个脚本通过浏览弹出方法来选择路径。其中我将只选择两个路径源&目标
I need a script to selecting the path by browsing pop-up method. where I would select only two path "source & target"
通过浏览弹出窗口,因为下一次我将有不同的位置,脚本。
By Browsing pop-up because next time I would have different locations, we can not fix them up in once script. I want to run the script as per need to diffident paths.
推荐答案
尝试这样从文件夹复制最新的文件:
Try something like this for copying the newest file from a folder:
@echo off
setlocal
set "src=C:\User\Client1\Reports\ReportFolderA"
set "dst=C:\User\Client1\FinalReports"
pushd "%src%"
for /f "delims=" %%f in ('dir /b /a:-d /o:-d') do (
copy "%%~f" "%dst%\File1.csv"
goto next
)
:next
popd
$ b b
在VBScript中,您可以使用 Shell.BrowseForFolder
方法选择文件夹。选择源文件夹的示例:
In VBScript you could use the Shell.BrowseForFolder
method for selecting a folder. Example for selecting the source folder:
Set os = CreateObject("Shell.Application")
basedir = os.Namespace("C:\").Self.Path
Set fldr = os.BrowseForFolder(0, "Select source folder:", &h10&, basedir)
If fldr Is Nothing Then
WScript.Echo "User pressed [Cancel]."
WScript.Quit 1
End If
src = fldr.Self.Path
查找和复制文件夹中的最新文件可以这样实现:
Finding and copying the most recent file in a folder can be achieved like this:
Set fso = CreateObject("Scripting.FileSystemObject")
Set mostRecent = Nothing
For Each f In fso.GetFolder(src).Files
If mostRecent Is Nothing Then
Set mostRecent = f
ElseIf f.DateLastModified > mostRecent.DateLastModified Then
Set mostRecent = f
End If
Next
If Not mostRecent Is Nothing Then
mostRecent.Copy fso.BuildPath(dst, "File1.csv")
End If
这篇关于复制和重命名最新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!