Page 1 of 1

Move file to folder based on part of file name

Posted: 08 Feb 2019 12:07
by advaya
Hello, I'm trying to build a batch file that will automatically move files to 1 of 3 folders depending on which tag is in the file name.

Tags
-mC-
-mF-
-mP-

Example file names
253434276_weddingphotos-mC-_44.pdf <- Would go to folder 1
picturesToSend-mF-_08092018.pdef <- Would go to folder 2
354534_photos-mP-BestDogEver.pdf <- Would go to folder 3
3664Surfing_-mP-pictureFromToday.pdf <- Would go to folder 3

The file name will only have one of the above tags in the name and needs to be moved from the base folder to a sub directory based on the tag.

The script will most likely be used on a computer with Windows 10.

Any advice or pointers would be greatly appreciated!

-Thanks

Re: Move file to folder based on part of file name

Posted: 08 Feb 2019 13:45
by aGerman
Untested:

Code: Select all

for %%i in ("*-mC-*.pdf") do move "%%~i" "subfolder1\"
Same for the other patterns.

Steffen

Re: Move file to folder based on part of file name

Posted: 08 Feb 2019 13:47
by elzooilogico
maybe misuse of string substitution, surf this place and tell us where you're stuck!

Re: Move file to folder based on part of file name

Posted: 08 Feb 2019 14:53
by advaya
aGerman wrote:
08 Feb 2019 13:45
Untested:

Code: Select all

for %%i in ("*-mC-*.pdf") do move "%%~i" "subfolder1\"
Same for the other patterns.

Steffen
Thank you so much!

Can I run this instantly or within seconds of a new file(s) being placed in the base folder? I have added a loop set for 10 seconds and is working fine, but is there a way to run the command once it detects new files in the folder?

My current solution:
To automate this I put it in a loop.

Code: Select all

:loop
for %%i in ("*-mC-*.pdf") do move "%%~i" "subfolder1\"
timeout /t 10
goto loop

Re: Move file to folder based on part of file name

Posted: 08 Feb 2019 22:56
by Squashman
The move command will only execute if the FOR command iterates any files. I know Windows has a way to alert the system of a new file but there is no API for a batch file to use to trigger that event.

I heavily use automation software and because most of the folders I watch are located on network attached storage my only option is to set my software to sweep the directory after a time interval.

Your only option with pure batch is to keep checking the folder for new files every few seconds.