Move file to folder based on part of file name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
advaya
Posts: 2
Joined: 08 Feb 2019 11:42

Move file to folder based on part of file name

#1 Post by advaya » 08 Feb 2019 12:07

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

aGerman
Expert
Posts: 3454
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 08 Feb 2019 13:45

Untested:

Code: Select all

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

Steffen

elzooilogico
Posts: 107
Joined: 23 May 2016 15:39
Location: Spain

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

#3 Post by elzooilogico » 08 Feb 2019 13:47

maybe misuse of string substitution, surf this place and tell us where you're stuck!

advaya
Posts: 2
Joined: 08 Feb 2019 11:42

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

#4 Post by advaya » 08 Feb 2019 14:53

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

Squashman
Expert
Posts: 4050
Joined: 23 Dec 2011 13:59

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

#5 Post by Squashman » 08 Feb 2019 22:56

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.

Post Reply