如何通过浏览弹出窗口设置路径? [英] How can I set Path by browsing popup?

查看:96
本文介绍了如何通过浏览弹出窗口设置路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

关于以下脚本功能:我将每月运行一次,它将进入源路径(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

我想在运行脚本后给出两个路径(弹出窗口要求输入路径)

解决方案

不好意思.您的问题不清楚. 我假设您要复制并重命名10个给定的文件,但不希望使用程序中给定的固定路径,而要使用程序运行时给定的可变路径.如果正确,则程序必须首先获取目标路径(只有一个),然后获取每个文件的源路径.

下面的批处理文件是实现先前过程的初步版本.如果您要使用此解决方案,则可以添加路径的浏览弹出窗口"部分,而不是简单的"set /P folder=Enter folder:"命令.也许这个版本对您来说足够了?

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

对于不同的客户(例如客户a),我有可变的目标路径 路径将为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 : "");

在此新解决方案中,您可以通过Batch文件的参数 SELECT 选择所需的客户端.例如,如果批处理文件名为example.bat,请对ClientA使用以下命令:

example.bat ClientA

您必须注意,浏览文件夹是一种交互式操作,它会弹出一个带有 all 个现有文件夹的弹出窗口,并允许您选择其中的一个./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"格式或 any 其他限制将放置在"Data \"部分之后的数据设置为今天的日期,则必须执行之后用户选择一个文件夹,因此程序将指示所选文件夹无效,然后再次弹出浏览窗口.

我鼓励您清楚说明您的要求.请修改您的原始问题,以便任何人都可以在阅读后理解该问题,而无需查看所有答案中的所有评论.

I have below script, in this there are two path one is Target path (only one) and another source path (variables).

About below script function: I'll run this once in a month and it'll go the souce path (10 path) and copy lates file then copy&rename to target path (common for all the files).

note : file which is copyed form the respacted source should be rename as per script like: file from "F:\Financial\Data\Reports\AccruntPnLMTD" should be rename as "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

I want's to give both path after run the script (popup should ask for the path)

解决方案

Excuse me. Your question is not clear. I assume that you want to copy and rename the 10 given files but not with the fixed paths given in the program, but with variable paths given when the program run. If this is correct, the program must get first the Target path (only one) and then the source path for each one of the files.

The Batch file below is a preliminary version that achieve previous process. If this solution is what you want, then the "browsing popup" part for the paths may be added instead of the simple "set /P folder=Enter folder:" commands. Or perhaps this version is enough for you?

EDIT: I modified the solution below in order to include these new requests:

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

LAST EDIT: The Batch file below has been modified for the last time in accordance with the last paragraph in this answer: Browse the existent folders in the disk and pick one.

@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 : "");

In this new solution you may SELECT the desired client via the parameter of the Batch file. For example, if the Batch file is called example.bat, use this command for ClientA:

example.bat ClientA

You must note that BROWSING FOR A FOLDER is an interactive operation that present a pop-up window with all existent folders and allows you to choose one of them.

EDIT: Some explanations added

It seems that there is a confusion here. In your question you show as examples of target and source folders these ones:

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"

However, in posterior comments you said:

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

You must realize that two previous forms are entirely different: in the first one the folder path is constant, but in the second one the folder path must be changed for each client. A Batch file solution is always designed with fixed requirements. This point is not clear in your answer nor in your comments, so I assumed certain details in order to write a Batch solution. I think there are two ways to solve this problem, depending of what the problem is:

1- Select the appropriate folders for each client: in this case I assumed that path folders have this form:

Target Folder is formed by "F:\Financial\" followed by a variable part that select each client, followed by "\Data\%DateFolder%\Final Reports".

Source path is formed by "F:\Financial\Data\Reports\" followed by a variable part that select each client, followed by each one of the 10 different folders (AccruntPnLMTD,AccruntPnLMTD..etc).

If this is the problem, then my solution above solve it. You just need to place the desired folder name as parameter of the Batch file. For example, if the name of the folder for client a is "ClientA", execute this command:

nameOfTheBatchFile ClientA

If the name of the folder for client B is "ClientB", execute this command:

nameOfTheBatchFile ClientB

If the name of the folder have spaces, enclose it in quotes; for example, for "Any other client" execute this command:

nameOfTheBatchFile "Any other client"

However, your posterior comments and insistence on using terms like "browsing popup", "asking for the path", etc. makes me think that the previously explained problem is not the one you want to solve. There is another possibility:

2- Browse the existent folders in the disk and pick one: in this case when the program run it present a "browsing popup" window that give access to all folders in the disk and allows you to select anyone of them. Note that the browsing window can NOT restrict the browse for any particular name format; if you want that the selected folder have certain characteristics, like the data placed after "Data\" part be today's date in "MM.YYYY" format or any other restriction, this checking must be done after the user choose a folder, so the program will indicate that the selected folder was invalid and popup the browsing window again.

I encourage you to clearly explain your requirements. Please, modify your original question so anyone can understand the problem after read it and don't require to review all the comments in all answers.

这篇关于如何通过浏览弹出窗口设置路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆