2013-07-16 18 views
-1

我要最新的文件从源路径复制到目标路径,然后重命名如下:复制和重命名最新的文件

目标路径:C:\用户\客户端1 \ FinalReports

源路径:C: \ User \ Client1 \ Reports \ ReportFolderA(来自Report FolderA的文件应重命名为File1.csv的目标文件夹) C:\ User \ Client1 \ Reports \ ReportFolderB(来自报告FolderB的文件应重命名为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 ..因此我们只能在脚本中设置一个源路径。

我需要一个脚本通过浏览弹出方法来选择路径。我只选择两条路径“源&目标”

通过浏览弹出窗口,因为下次我有不同的位置时,我们无法在一次脚本中修复它们。我想根据需要运行脚本来避开路径。

+0

我把它用“我想制作一个剧本”你其实是指“我想让其他人为我制作剧本”? –

回答

3

尝试这样的事情从一个文件夹复制最新的文件:

@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 
+0

thx Ansgar ...但我想在弹出的路径中询问源和目标路径。 –

+0

批处理不是正确的工具。您可能需要考虑切换到VBScript。 –

+0

哦,太棒了...你能帮我做到吗? –

1

试试这个:

 
@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" 
+0

我想在浏览弹出窗口中给出路径。这也不是关于唯一的来源,有4-5个源路径和文件需要被重新命名。 –