Hi,
I have lots of photos and I want to move those photos to year and month folders based on filenames. Some image files have this name syntax:
IMG-20160819-WA0016.jpg, I want to move it to a 2016/08, it means only year and month folder, not day folder. If folder doesn't exist it should be created first.
Some file names have this syntax; WP_20160818_002.jpg, again 2016/08
It would be better if script copies the files to a remote server using ftp.
How can I do that?
Thanks.
Batch script to move files based on filenames
Moderator: DosItHelp
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Batch script to move files based on filenames
Are there any other naming schemes, or just those two?
Re: Batch script to move files based on filenames
I assume the part in front of the date stamp does not contain additional hyphens or underscores.
This code only simulates the actions. Remove the ECHOs and PAUSE if it looks okay.
Steffen
Code: Select all
@echo off &setlocal
for /f "delims=" %%i in ('dir /a-d /b /s "*.jpg"^|findstr /reic:"\\[^\\_-][^\\_-]*[_-]20[0-9][0-9][01][0-9][0-3][0-9][_-][^\\]*\.jpg"') do (
for /f "delims=_- tokens=2" %%j in ("%%~ni") do (
set "file=%%i"
set "datestamp=%%j"
setlocal EnableDelayedExpansion
ECHO md "!datestamp:~,4!\!datestamp:~4,2!" 2>nul
ECHO move "!file!" "!datestamp:~,4!\!datestamp:~4,2!\"
endlocal
)
)
PAUSE
This code only simulates the actions. Remove the ECHOs and PAUSE if it looks okay.
Steffen
Re: Batch script to move files based on filenames
kahlenberg wrote:I have lots of photos and I want to move those photos to year and month folders based on filenames.
It would be better if script copies the files to a remote server using ftp.
Do you want them moved or sent to an FTP server?
Some image files have this syntax: IMG-20160819-WA0016.jpg
Some file names have this syntax; WP_20160818_002.jpg
I want to move it to a 2016/08, it means only year and month folder, not day folder.
The forward slash is not a path element separator in Windows and is otherwise an illegal filename character.
You could mean you want a folder called 08 inside another folder called 2016
or you could want it in a single folder called something like 2016-08
-
- Posts: 6
- Joined: 24 Jun 2016 01:47
Re: Batch script to move files based on filenames
Thanks for answers! I love this forum
Sorry for inadequate information.
There are only two naming schemes, but there are additional file endings, so all file endings are: jpg, amr, mp4, mp3.
I mistakenly wrote forward slash. I ment of course a folder 08 inside of another folder 2016, so 2016\08.
I run the script. It outputs:
Sorry for inadequate information.
There are only two naming schemes, but there are additional file endings, so all file endings are: jpg, amr, mp4, mp3.
I mistakenly wrote forward slash. I ment of course a folder 08 inside of another folder 2016, so 2016\08.
I run the script. It outputs:
Code: Select all
...
move "D:\tmp\IMG-20160520-WA0010.jpg" "2016\05\"
md "2016\05"
move "D:\tmp\IMG-20160520-WA0011.jpg" "2016\05\"
md "2016\05"
move "D:\tmp\IMG-20160520-WA0012.jpg" "2016\05\"
md "2016\05"
move "D:\tmp\IMG-20160520-WA0013.jpg" "2016\05\"
md "2016\05"
move "D:\tmp\IMG-20160520-WA0016.jpg" "2016\05\"
md "2016\05"
move "D:\tmp\IMG-20160520-WA0021.jpg" "2016\05\"
md "2016\05"
move "D:\tmp\IMG-20160520-WA0023.jpg" "2016\05\"
md "2016\05"
move "D:\tmp\IMG-20160521-WA0002.jpg" "2016\05\"
md "2016\05"
move "D:\tmp\IMG-20160521-WA0004.jpg" "2016\05\"
md "2016\05"
move "D:\tmp\IMG-20160521-WA0009.jpg" "2016\05\"
md "2016\05"
move "D:\tmp\IMG-20160521-WA0011.jpg" "2016\05\"
md "2016\05"
move "D:\tmp\IMG-20160521-WA0012.jpg" "2016\05\"
md "2016\05"
move "D:\tmp\IMG-20160521-WA0013.jpg" "2016\05\"
md "2016\05"
...
Re: Batch script to move files based on filenames
kahlenberg wrote:I run the script. It outputs:Code: Select all
md "2016\05"
move "D:\tmp\IMG-20160520-WA0011.jpg" "2016\05\"
md "2016\05"
move "D:\tmp\IMG-20160520-WA0012.jpg" "2016\05\"
Take out the ECHO keywords and it should work well for you.
If you remove the "*.jpg" and the \.jpg then it should process any file with the correct filename format.
-
- Posts: 6
- Joined: 24 Jun 2016 01:47
Re: Batch script to move files based on filenames
foxidrive wrote:Take out the ECHO keywords and it should work well for you.
If you remove the "*.jpg" and the \.jpg then it should process any file with the correct filename format.
Thanks, it worked!