[英]Copy and Rename newest file
我想将最新文件从源路径复制到目标路径,然后按以下方式重命名:
目标路径:C; \\ User \\ Client1 \\ FinalReports
源路径:C:\\ User \\ Client1 \\ Reports \\ ReportFolderA(报告文件夹A中的文件应重命名为目标文件夹,作为File1.csv)C:\\ User \\ Client1 \\ Reports \\ ReportFolderB(报告文件夹B中的文件应重命名为目标文件夹作为File2.csv)C:\\ User \\ Client1 \\ Reports \\ ReportFolderD(来自Report FolderD的文件应重命名为目标文件夹作为File4.csv)C:\\ User \\ Client1 \\ Reports \\ ReportFolderF(应将Report FolderF的文件重命名为目标文件夹为File5.csv)
“ C:\\ User \\ Client1 \\ Reports”源路径是固定的,后跟变量ReportFolderA,ReportFolderB.etc ..,因此我们只能在脚本中设置一个源路径。
我需要一个脚本来通过浏览弹出方法选择路径。 我只选择两条路径“源和目标”
通过浏览弹出窗口,是因为下次我将有不同的位置时,我们无法在一次脚本中修复它们。 我想根据差异路径的需要运行脚本。
尝试使用以下方法从文件夹复制最新文件:
@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
在VBScript中,可以使用Shell.BrowseForFolder
方法选择文件夹。 选择源文件夹的示例:
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
查找和复制文件夹中的最新文件可以通过以下方式实现:
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
尝试这个:
@echo off &setlocal set "src=C:\User\Client1\Reports\ReportFolderA" set "dst=C:\User\Client1\FinalReports" cd /d "%src%" for /f "delims=" %%a in ('dir /b /a-d /od') do set "file=%%~a" copy "%file%" "%dst%\File1.csv"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.