3

我有以下脚本,其中有两个路径,一个是目标路径(只有一个),另一个是源路径(变量)。

关于以下脚本功能:我将在一个月内运行一次,它将进入源路径(10 路径)并复制lates 文件,然后复制并重命名到目标路径(所有文件通用)。

注意:从复制的源文件中复制的文件应根据脚本重命名,例如:来自“F:\Financial\Data\Reports\AccruntPnLMTD”的文件应重命名为“PNL.csv”

@echo off
setlocal
set DateFolder=04.2013
set TargetFolder=F:\Financial\Data\%DateFolder%\Final Reports

:: copy the newest file from AccruntPnLMTD and rename it to PNL.csv
call :copyAndRename "F:\Financial\Data\Reports\AccruntPnLMTD" "%TargetFolder%\PNL.csv"

:: copy the newest file from AccountPnlMTD and rename it to AC.csv
call :copyAndRename "F:\Financial\Data\Reports\AccountPnlMTD" "%TargetFolder%\AC.csv"

:: copy the newest file from ExpensesMTD and rename it to EXPMTD.csv
call :copyAndRename "F:\Financial\Data\Reports\ExpensesMTD" "%TargetFolder%\EXPMTD.csv"

:: copy the newest file from ExpensesYTD and rename it to EXPYTD.csv
call :copyAndRename "F:\Financial\Data\Reports\ExpensesYTD" "%TargetFolder%\EXPYTD.csv"

:: copy the newest file from AccrualPnLYTD and rename it to PNLYTD.csv
call :copyAndRename "F:\Financial\Data\Reports\AccrualPnLYTD" "%TargetFolder%\PNLYTD.csv"

:: copy the newest file from AccountYTD and rename it to ACYTD.csv
call :copyAndRename "F:\Financial\Data\Reports\AccountYTD" "%TargetFolder%\ACYTD.csv"

:: copy the newest file from BalanceMTD and rename it to BSMTD.csv
call :copyAndRename "F:\Financial\Data\Reports\BalanceMTD" "%TargetFolder%\BSMTD.csv"

:: copy the newest file from BalanceYTD and rename it to BSYTD.csv
call :copyAndRename "F:\Financial\Data\Reports\BalanceYTD" "%TargetFolder%\BSYTD.csv"

:: copy the newest file from FinancialStmtMTD and rename it to FSMTD.csv
call :copyAndRename "F:\Financial\Data\Reports\FinancialStmtMTD" "%TargetFolder%\FSMTD.csv"

:: copy the newest file from FinancialStmtYTD and rename it to FSYTD.csv
call :copyAndRename "F:\Financial\Data\Reports\FinancialStmtYTD" "%TargetFolder%\FSYTD.csv"


:: Done
goto :eof

:copyAndRename
set SourceFolder=%~1
set TargetFile=%~2

:: Find the newest file in the source folder
for /f "tokens=*" %%F in ('dir /b /od /a-d "%SourceFolder%"') do set "NewestFile=%%F"

:: copy and rename it to the target
copy "%SourceFolder%\%NewestFile%" "%TargetFile%"


:: Done with this subroutine
goto :eof

我想在运行脚本后给出两个路径(弹出窗口应该询问路径)

4

2 回答 2

0

这是未经测试的 - 它将您的代码与弹出例程合并,并要求源文件夹一次,所有文件夹都应该在其中。

@echo off
setlocal
set DateFolder=04.2013
:: set the source and target folders
call :getpath

set "TargetFolder=%TargetFolder%\%DateFolder%\Final Reports"


:: copy the newest file from AccruntPnLMTD and rename it to PNL.csv
call :copyAndRename "AccruntPnLMTD" "%TargetFolder%\PNL.csv"

:: copy the newest file from AccountPnlMTD and rename it to AC.csv
call :copyAndRename "AccountPnlMTD" "%TargetFolder%\AC.csv"

:: copy the newest file from ExpensesMTD and rename it to EXPMTD.csv
call :copyAndRename "ExpensesMTD" "%TargetFolder%\EXPMTD.csv"

:: copy the newest file from ExpensesYTD and rename it to EXPYTD.csv
call :copyAndRename "ExpensesYTD" "%TargetFolder%\EXPYTD.csv"

:: copy the newest file from AccrualPnLYTD and rename it to PNLYTD.csv
call :copyAndRename "AccrualPnLYTD" "%TargetFolder%\PNLYTD.csv"

:: copy the newest file from AccountYTD and rename it to ACYTD.csv
call :copyAndRename "AccountYTD" "%TargetFolder%\ACYTD.csv"

:: copy the newest file from BalanceMTD and rename it to BSMTD.csv
call :copyAndRename "BalanceMTD" "%TargetFolder%\BSMTD.csv"

:: copy the newest file from BalanceYTD and rename it to BSYTD.csv
call :copyAndRename "BalanceYTD" "%TargetFolder%\BSYTD.csv"

:: copy the newest file from FinancialStmtMTD and rename it to FSMTD.csv
call :copyAndRename "FinancialStmtMTD" "%TargetFolder%\FSMTD.csv"

:: copy the newest file from FinancialStmtYTD and rename it to FSYTD.csv
call :copyAndRename "FinancialStmtYTD" "%TargetFolder%\FSYTD.csv"


:: Done
goto :eof

:copyAndRename
set "FileFolder=%SourceFolder%\%~1"
set "TargetFile=%~2"

echo copying "%FileFolder%"

:: Find the newest file in the source folder
for /f "tokens=*" %%F in ('dir /b /od /a-d "%FileFolder%"') do set "NewestFile=%%F"

:: copy and rename it to the target
copy "%FileFolder%\%NewestFile%" "%TargetFile%" >nul


:: Done with this subroutine
goto :eof

:getsourcepath

Call :BrowseFolder "Choose Source folder" "C:\Program Files\"
Set "SourceFolder=%Result%"

Call :BrowseFolder "Choose Target folder" "C:\Users\"
Set TargetFolder=%Result% 

REM echo %SourceFolder%
REM echo %TargetFolder%

:: Done
goto :eof


:BrowseFolder
set Result=
set input="%~1" & set default="%~2"
:: Temporary files
set vbs=%temp%\_.vbs
set tmp=%temp%\_.cmd
:: Build VBScript file
findstr "'%skip%VBS" "%~f0" > "%vbs%"
:: Run the script with WSH and set Path as Env variable %Result%
for /f "delims=" %%a in ('cscript /nologo "%vbs%" ') do set "Result=%%a"
DEL %VBS%
set vbs= & set tmp= & set input= & set default=
goto :EOF

set WshShell=WScript.CreateObject("WScript.Shell") 'VBS
set shell=WScript.CreateObject("Shell.Application") 'VBS
sInput=WshShell.ExpandEnvironmentStrings("%input%") 'VBS
sDefault=WshShell.ExpandEnvironmentStrings("%default%") 'VBS
sInput = Replace(sInput, chr(34), "") 'VBS
sDefault = replace(sDefault,chr(34),"") 'VBS
set folder=shell.BrowseForFolder(0,sInput,0,sDefault) 'VBS
if typename(folder)="Nothing" Then  'VBS
wscript.echo "set Result=Dialog Cancelled" 'VBS
WScript.Quit(1) 'VBS
end if 'VBS
set folderItems=folder.Items() 'VBS
set folderItem=folderItems.Item() 'VBS
pathname=folderItem.Path 'VBS
wscript.echo pathname 'VBS
于 2013-05-29T11:20:33.127 回答
0

打扰一下。你的问题不清楚。我假设您要复制和重命名 10 个给定文件,但不是使用程序中给出的固定路径,而是使用程序运行时给出的可变路径。如果这是正确的,程序必须首先获取目标路径(只有一个),然后是每个文件的源路径。

下面的批处理文件是实现先前过程的初步版本。如果此解决方案是您想要的,则可以添加路径的“浏览弹出”部分而不是简单的"set /P folder=Enter folder:"命令。或者这个版本对你来说已经足够了吗?

编辑:我修改了以下解决方案以包含这些新请求:

我有不同客户端的可变目标路径,例如客户端路径将是 F:\Financial\ClientA\Data\%DateFolder%\Final Reports..& 对于客户端 B "F:\Financial\ClientB\Data\%DateFolder%\最终报告”

在源路径中也是如此,例如 Client A path "F:\Financial\Data\Reports\Client A\AccruntPnLMTD ;对于客户端 B 路径将是 F:\Financial\Data\Reports\Client B\AccruntPnLMTD..file 文件夹名称(AccruntPnLMTD ,AccruntPnLMTD..etc)将为每个客户重新制造相同的

最后编辑:下面的批处理文件已根据此答案的最后一段进行了最后一次修改:浏览磁盘中现有的文件夹并选择一个

@if (@CodeSection == @Batch) @then


@echo off
setlocal

rem Activate the browsing pop-up and ask for TargetFolder
for /F "delims=" %%a in ('CScript //nologo //E:JScript "%~F0" "Select the Target folder"') do (
   set TargetFolder=%%a
)

rem Activate the browsing pop-up and ask for SourceFolder
for /F "delims=" %%a in ('CScript //nologo //E:JScript "%~F0" "Select the Source folder"') do (
   set ClientSourceFolder=%%a
)

rem Process the list of "sourceFolder=fileName" pairs
for %%a in ("AccruntPnLMTD=PNL" "AccountPnlMTD=AC" "ExpensesMTD=EXPMTD" "ExpensesYTD=EXPYTD" "AccrualPnLYTD=PNLYTD"
            "AccountYTD=ACYTD" "BalanceMTD=BSMTD" "BalanceYTD=BSYTD" "FinancialStmtMTD=FSMTD" "FinancialStmtYTD=FSYTD"
           ) do (
   rem copy the newest file from sourceFolder and rename it to fileName.csv
   for /F "tokens=1,2 delims==" %%b in (%%a) do (
      call :copyAndRename "%%b" "%%c"
   )
)

:: Done
goto :eof

:copyAndRename
set SourceFolder=%ClientSourceFolder%\%~1
set TargetFile=%TargetFolder%\%~2.csv

:: Find the newest file in the source folder
for /f "tokens=*" %%F in ('dir /b /od /a-d "%SourceFolder%"') do set 

"NewestFile=%%F"

:: copy and rename it to the target
copy "%SourceFolder%\%NewestFile%" "%TargetFile%"

:: Done with this subroutine
goto :eof


@end


// JScript section

// Creates a dialog box that enables the user to select a folder.
// http://msdn.microsoft.com/en-us/library/windows/desktop/bb774065(v=vs.85).aspx

var shl = new ActiveXObject("Shell.Application");
var folder = shl.BrowseForFolder(0, WScript.Arguments(0), 0, 0);
WScript.Stdout.WriteLine(folder ? folder.self.path : "");

在这个新的解决方案中,您可以通过批处理文件的参数选择所需的客户端。例如,如果调用了批处理文件,则example.bat对 ClientA 使用以下命令:

example.bat ClientA

您必须注意,浏览文件夹是一种交互式操作,它会显示一个包含所有现有文件夹的弹出窗口,并允许您选择其中一个。

编辑添加了一些解释

这里似乎有一个混乱。在您的问题中,您将这些文件夹显示为目标和源文件夹的示例:

set TargetFolder=F:\Financial\Data\%DateFolder%\Final Reports

:: copy the newest file from AccruntPnLMTD and rename it to PNL.csv
call :copyAndRename "F:\Financial\Data\Reports\AccruntPnLMTD"

但是,在后面的评论中,您说:

I have variable target path for diffrent clients like for client a path wil be 
F:\Financial\ClientA\Data\%DateFolder%\Final Reports..& for client B
F:\Financial\ClientB\Data\%DateFolder%\Final Reports

same goes in source path like Client A path
F:\Financial\Data\Reports\Client A\AccruntPnLMTD ; for client B Path will be
F:\Financial\Data\Reports\Client B\AccruntPnLMTD..
file folder names (AccruntPnLMTD,AccruntPnLMTD..etc) will reman same for each clients

您必须意识到,前两种形式完全不同:第一种形式的文件夹路径是不变的,但第二种形式的文件夹路径必须为每个客户端更改。批处理文件解决方案始终按照固定要求进行设计。您的回答和评论中都不清楚这一点,因此我假设了某些细节以编写批处理解决方案。我认为有两种方法可以解决此问题,具体取决于问题所在:

1-为每个客户端选择适当的文件夹:在这种情况下,我假设路径文件夹具有以下形式:

目标文件夹由“F:\Financial\”后跟选择每个客户端的变量部分组成,然后是“\Data\%DateFolder%\Final Reports”。

源路径由“F:\Financial\Data\Reports\”组成,后跟选择每个客户端的变量部分,然后是 10 个不同文件夹中的每一个(AccruntPnLMTD、AccruntPnLMTD..etc)。

如果这是问题,那么我上面的解决方案可以解决。您只需将所需的文件夹名称作为批处理文件的参数。例如,如果客户端 a 的文件夹名称为“ClientA”,则执行以下命令:

nameOfTheBatchFile ClientA

如果客户端 B 的文件夹名称为“ClientB”,请执行以下命令:

nameOfTheBatchFile ClientB

如果文件夹名称有空格,请用引号将其括起来;例如,对于“任何其他客户端”,执行以下命令:

nameOfTheBatchFile "Any other client"

但是,您的后期评论和坚持使用“浏览弹出窗口”、“询问路径”等术语,让我认为前面解释的问题不是您想要解决的问题。还有另一种可能:

2-浏览磁盘中现有的文件夹并选择一个:在这种情况下,当程序运行时,它会显示一个“浏览弹出”窗口,可以访问磁盘中的所有文件夹并允许您选择其中的任何一个。请注意,浏览窗口不能限制任何特定名称格式的浏览;如果您希望所选文件夹具有某些特征,例如放在“Data\”部分之后的数据是“MM.YYYY”格式的今天日期或任何其他限制,则必须在用户选择文件夹后进行此检查,因此程序将提示所选文件夹无效并再次弹出浏览窗口。

我鼓励你清楚地解释你的要求。请修改您的原始问题,以便任何人在阅读后都可以理解问题,并且不需要查看所有答案中的所有评论。

于 2013-05-29T18:50:21.007 回答