Mati
Mati

Reputation: 1

Batch file - Windows 10. How to move files based on mid part of their name to folders named after that part

Would you help me with this simple task i'm struggling with?
Here's the scenario.

We have a folder, let's called it "Folder1", with a huge amount of files named with six pairs of numbers, like this

"78 28 96 32 94 93.jpg"
"52 67 30 29 63 40.jpg"
"17 33 07 11 08 00.jpg"
"16 34 33 05 41 53.jpg"

Next to this files we have folders named from "00" to "99". And inside each of this folders we have another set of folders named the same way.
So, what we need to do is to move this files into the existent folders based on the 4th and 5th pair of numbers in their names, like this

"78 28 96 32 94 93.jpg" should be moved to "Folder1\ 32 \ 94"
"52 67 30 29 63 40.jpg" should be moved to "Folder1\ 29 \ 63"
"17 33 07 11 08 00.jpg" should be moved to "Folder1\ 11 \ 08"
and so on.

How can we do this with a batch file in "Folder1"?
The folders already exist, we only need to move the files.
Thanks!

Upvotes: 0

Views: 108

Answers (1)

Magoo
Magoo

Reputation: 79982

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
 'dir /b /a-d "%sourcedir%\?? ?? ?? ?? ?? ??.jpg" '
 ) DO (
 FOR /f "tokens=4,5delims= " %%p IN ("%%~na") DO (
  ECHO MOVE "%sourcedir%\%%a" "%sourcedir%\%%p\%%q\"
 )
)

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

Perform a directory-scan of files (only) matching the mask supplied and then with the name part of each filename found, select tokens 4 and 5 to %%p and %%q.

The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)

Upvotes: 1

Related Questions