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.