I need to migrate a bunch of files across several subfolders in a single directory. Each subfolder containing a maximum of 100 data files (0000-0099,0100-0199, etc.) and is named dir_001, dir_002 etc. respectfully.
For example I can successfully transfer all the files that begin with a prefix of "F_0" using the following:
for /r X:\<PATH1>\ %%F in (F_0*.txt) do copy %%F E:\<PATH2>\
This will grab all the files from all the subfolders covering all images from F_00001.txt to F_09999.txt (or whatever the upper limit is). But most of the time I only need a smaller subset such as from F_04395.txt through F04542.txt.
Here is what I have been trying that does not work
for /r X:\PATH1\ %%F in (F_0*.txt) do (
For %%A in (%%F) do (
Set Folder=%%~dpA
For /L %%i in (4395 1 4542)do if /I exist Folder\F_0%%i.txt copy %%F E:\PATH2\
)
)
This seems to break when it comes to converting the file path to a variable.
It also would need to work for a sequence from F_000001.txt through F_010001.txt or in other words from file number 1 through 1001 (with a different number of leading zeros for 0-9, 10-99,100-999,1000-9999, so it is always a 5 digit number plus the prefix of "F_0")
Any advice would be most appreciated!
Update: Thanks for @Gerhard I've changed the code to what is below but still not working and there is an issue combining/concatenating the %%dpA variable with the rest of the filename. The result of the below code is a double slash between the filename and the path, or if I take out the hard coded clash then the code also fails.
for /r X:\PATH1\ %%F in (F_0*.txt) do (
For %%A in (%%F) do (
For /L %%i in (4395 1 4542)do if /I exist %%dpA\F_0%%i.txt copy %%F E:\PATH2\
)
)
CodePudding user response:
Thanks again to @Gerhard you beat me to the optomization but still helped greatly. Here is my final code that also address the multiple subfolders, and consolidated into a single line!
setlocal EnableDelayedExpansion
for /L %%i in (43,1,45,) do ((SET "formattedValue=000000%%i" & SET PathSource2=X:\Path1\Dir_!formattedValue:~-3!) & (For /L %%i in (4395 ,1,4542) do (if exist "!PathSource2!\F_0%%i.jpg" copy "!PathSource2!\F_0%%i.txt" "E:\Path2\")))
