如何通过浏览弹出窗口设置路径?

时间:2013-05-29 10:44:18

标签: batch-file

我有下面的脚本,在这里有两个路径,一个是目标路径(只有一个)和另一个源路径(变量)。

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

注意:从respacted源复制的文件应按照以下脚本重命名: “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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

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

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

  

我有不同客户端的不同客户端的可变目标路径   路径将是F:\ Financial \ ClientA \ Data \%DateFolder%\ Final Reports ..&   对于客户B“F:\ Financial \ ClientB \ Data \%DateFolder%\ Final Reports”

     

在客户端A路径中的源路径中也是如此   “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 : "");

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

example.bat ClientA

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

编辑添加了一些解释

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

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

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