Page 1 of 1

[Solved] Move file to incremental subfolder based on file name prefix

Posted: 14 Nov 2023 16:45
by capiot23
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
X:\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
What I wanted to do is for example
*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
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
Hope you get the idea.

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
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) :oops:

Re: Move file to incremental subfolder based on file name prefix

Posted: 14 Nov 2023 20:34
by Aacini
I think this works:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

cd /D X:\PROJECT

set "number=0"
for %%a in (*.xml) do (
   set "file=%%a"
   set "num=!file:~5,1!"
   if !num! neq !number! (
      set /A "number=num, count=1"
   ) else (
      set /A "count+=1"
      ECHO move "!file!" !count!
   )
)
Output:

Code: Select all

move "FIL005_20231110113444.xml" 2
move "FIL005_20231110113445.xml" 3
move "FIL005_20231110113446.xml" 4
move "FIL006_20231110113421.xml" 2
move "FIL006_20231110113438.xml" 3
move "FIL007_20231110113603.xml" 2
Antonio

Re: Move file to incremental subfolder based on file name prefix

Posted: 15 Nov 2023 06:23
by miskox
Aacini's solutions are always so clever and short. I'm speechless.

Saso

Re: Move file to incremental subfolder based on file name prefix

Posted: 15 Nov 2023 11:11
by capiot23
Aacini wrote:
14 Nov 2023 20:34
I think this works:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

cd /D X:\PROJECT

set "number=0"
for %%a in (*.xml) do (
   set "file=%%a"
   set "num=!file:~5,1!"
   if !num! neq !number! (
      set /A "number=num, count=1"
   ) else (
      set /A "count+=1"
      ECHO move "!file!" !count!
   )
)
Output:

Code: Select all

move "FIL005_20231110113444.xml" 2
move "FIL005_20231110113445.xml" 3
move "FIL005_20231110113446.xml" 4
move "FIL006_20231110113421.xml" 2
move "FIL006_20231110113438.xml" 3
move "FIL007_20231110113603.xml" 2
Antonio
I've tested and it works perfectly amazing as what I need.
I'm speechless. I knew the process could be simpler but I didn't expect how could you code it very short! You're so genius.
I spent more than one hour just to post-mortem your code and finally understand it.
Thank you so much. :)
miskox wrote:
15 Nov 2023 06:23
Aacini's solutions are always so clever and short. I'm speechless.

Saso
Agreed. He's like got little more brain inside his brain :?