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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
capiot23
Posts: 5
Joined: 10 Nov 2023 07:29

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

#1 Post by capiot23 » 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
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:
Last edited by capiot23 on 16 Nov 2023 04:58, edited 1 time in total.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

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

#2 Post by Aacini » 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

miskox
Posts: 631
Joined: 28 Jun 2010 03:46

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

#3 Post by miskox » 15 Nov 2023 06:23

Aacini's solutions are always so clever and short. I'm speechless.

Saso

capiot23
Posts: 5
Joined: 10 Nov 2023 07:29

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

#4 Post by capiot23 » 15 Nov 2023 11:11

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 :?

Post Reply