[Solved] Move file to incremental subfolder based on file name prefix
Posted: 14 Nov 2023 16:45
Hello guys,
I have a folder named "PROJECT" with subfolder "2", "3", "4".
I have also few files named FIL004_xxx.xml , FIL005_xxx.xml , FIL006_xxx.xml , FIL007_xxx.xml as shown below
*If found 2 files FIL005_*.xml then move one of it to subfolder "2".
*If found 3 files FIL005_*.xml then move one of it to subfolder "2" then another one to subfolder "3".
*If found 4 files FIL005_*.xml then move one of it to subfolder "2" then move one of it to subfolder "3" and another one to subfolder "4".
This should apply to FIL004_xxx.xml , FIL006_xxx.xml , FIL007_xxx.xml as well.
This is the result it looks like
I've created a batch file that do the dirty work and it's working fine (so far).
- I use trick by rename all xml files to *_<incremental number> at the end of file name
- Move *_2.xml to subfolder "2", *_3.xml to subfolder "3", *_4.xml to subfolder "4"
- Remove last 2 characters (_<number>) before extension to revert to original file name
Yeah it's dirty and ugly. Appreciate if anyone got better idea and more efficient and cleaner way for the same purpose (without need to rename file if possible)
I have a folder named "PROJECT" with subfolder "2", "3", "4".
I have also few files named FIL004_xxx.xml , FIL005_xxx.xml , FIL006_xxx.xml , FIL007_xxx.xml as shown below
What I wanted to do is for exampleX:\PROJECT
| FIL004_20231110113437.xml
| FIL005_20231110113443.xml
| FIL005_20231110113444.xml
| FIL005_20231110113445.xml
| FIL005_20231110113446.xml
| FIL006_20231110113413.xml
| FIL006_20231110113421.xml
| FIL006_20231110113438.xml
| FIL007_20231110113601.xml
| FIL007_20231110113603.xml
|
+---2
+---3
\---4
*If found 2 files FIL005_*.xml then move one of it to subfolder "2".
*If found 3 files FIL005_*.xml then move one of it to subfolder "2" then another one to subfolder "3".
*If found 4 files FIL005_*.xml then move one of it to subfolder "2" then move one of it to subfolder "3" and another one to subfolder "4".
This should apply to FIL004_xxx.xml , FIL006_xxx.xml , FIL007_xxx.xml as well.
This is the result it looks like
Hope you get the idea.X:\PROJECT
| FIL004_20231110113437.xml
| FIL005_20231110113443.xml
| FIL006_20231110113438.xml
| FIL007_20231110113603.xml
|
+---2
| FIL005_20231110113446.xml
| FIL006_20231110113421.xml
| FIL007_20231110113601.xml
|
+---3
| FIL005_20231110113445.xml
| FIL006_20231110113413.xml
|
\---4
FIL005_20231110113444.xml
I've created a batch file that do the dirty work and it's working fine (so far).
- I use trick by rename all xml files to *_<incremental number> at the end of file name
- Move *_2.xml to subfolder "2", *_3.xml to subfolder "3", *_4.xml to subfolder "4"
- Remove last 2 characters (_<number>) before extension to revert to original file name
Code: Select all
:: RENAME FIL004_* TO INCREMENTAL NUMBER AT THE END OF FILE NAME
setlocal enabledelayedexpansion
set "count=1"
for /f "usebackq delims=*" %%f in (`dir /b /o:-d /tc "X:\Project\FIL004_*.xml" 2^>nul`) do (
ren X:\Project\%%f %%~nf_!count!.xml
set /a count+=1
)
endlocal
:: RENAME FIL005_* TO INCREMENTAL NUMBER AT THE END OF FILE NAME
setlocal enabledelayedexpansion
set "count=1"
for /f "usebackq delims=*" %%f in (`dir /b /o:-d /tc "X:\Project\FIL005_*.xml" 2^>nul`) do (
ren X:\Project\%%f %%~nf_!count!.xml
set /a count+=1
)
endlocal
:: RENAME FIL006_* TO INCREMENTAL NUMBER AT THE END OF FILE NAME
setlocal enabledelayedexpansion
set "count=1"
for /f "usebackq delims=*" %%f in (`dir /b /o:-d /tc "X:\Project\FIL006_*.xml" 2^>nul`) do (
ren X:\Project\%%f %%~nf_!count!.xml
set /a count+=1
)
endlocal
:: RENAME FIL007_* TO INCREMENTAL NUMBER AT THE END OF FILE NAME
setlocal enabledelayedexpansion
set "count=1"
for /f "usebackq delims=*" %%f in (`dir /b /o:-d /tc "X:\Project\FIL007_*.xml" 2^>nul`) do (
ren X:\Project\%%f %%~nf_!count!.xml
set /a count+=1
)
endlocal
:: MOVE *_2.xml TO SUBFOLDER .\2\
move /y X:\Project\*_2.xml X:\Project\2\ 2>nul
:: MOVE *_3.xml TO SUBFOLDER .\3\
move /y X:\Project\*_3.xml X:\Project\3\ 2>nul
:: MOVE *_4.xml TO SUBFOLDER .\4\
move /y X:\Project\*_4.xml X:\Project\4\ 2>nul
:: REMOVE LAST 2 CHARACTERS BEFORE EXTENSION TO REVERT TO ORIGINAL FILE NAME IN MAIN FOLDER
setlocal enabledelayedexpansion
for %%f in (X:\Project\*_1.xml) do if %%f neq %~nx0 (
set "filename=%%~nf"
ren "%%f" "!filename:~0,-2!%%~xf"
)
endlocal
:: REMOVE LAST 2 CHARACTERS BEFORE EXTENSION TO REVERT TO ORIGINAL FILE NAME IN SUBFOLDER .\2\
setlocal enabledelayedexpansion
for %%f in (X:\Project\2\*_2.xml) do if %%f neq %~nx0 (
set "filename=%%~nf"
ren "%%f" "!filename:~0,-2!%%~xf"
)
endlocal
:: REMOVE LAST 2 CHARACTERS BEFORE EXTENSION TO REVERT TO ORIGINAL FILE NAME IN SUBFOLDER .\3\
setlocal enabledelayedexpansion
for %%f in (X:\Project\3\*_3.xml) do if %%f neq %~nx0 (
set "filename=%%~nf"
ren "%%f" "!filename:~0,-2!%%~xf"
)
endlocal
:: REMOVE LAST 2 CHARACTERS BEFORE EXTENSION TO REVERT TO ORIGINAL FILE NAME IN SUBFOLDER .\4\
setlocal enabledelayedexpansion
for %%f in (X:\Project\4\*_4.xml) do if %%f neq %~nx0 (
set "filename=%%~nf"
ren "%%f" "!filename:~0,-2!%%~xf"
)
endlocal