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
Move file to folder based on part of file name
Moderator: DosItHelp
Re: Move file to folder based on part of file name
Untested:Same for the other patterns.
Steffen
Code: Select all
for %%i in ("*-mC-*.pdf") do move "%%~i" "subfolder1\"
Steffen
-
- Posts: 128
- Joined: 23 May 2016 15:39
- Location: Spain
Re: Move file to folder based on part of file name
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
Thank you so much!aGerman wrote: ↑08 Feb 2019 13:45Untested:Same for the other patterns.Code: Select all
for %%i in ("*-mC-*.pdf") do move "%%~i" "subfolder1\"
Steffen
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
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.
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.