Batch script to move files based on filenames

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kahlenberg
Posts: 6
Joined: 24 Jun 2016 01:47

Batch script to move files based on filenames

#1 Post by kahlenberg » 21 Aug 2016 12:22

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.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch script to move files based on filenames

#2 Post by ShadowThief » 21 Aug 2016 13:13

Are there any other naming schemes, or just those two?

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

Re: Batch script to move files based on filenames

#3 Post by aGerman » 21 Aug 2016 13:18

I assume the part in front of the date stamp does not contain additional hyphens or underscores.

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch script to move files based on filenames

#4 Post by foxidrive » 22 Aug 2016 02:11

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

kahlenberg
Posts: 6
Joined: 24 Jun 2016 01:47

Re: Batch script to move files based on filenames

#5 Post by kahlenberg » 22 Aug 2016 04:01

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:


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"
...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch script to move files based on filenames

#6 Post by foxidrive » 22 Aug 2016 06:55

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.

kahlenberg
Posts: 6
Joined: 24 Jun 2016 01:47

Re: Batch script to move files based on filenames

#7 Post by kahlenberg » 22 Aug 2016 09:13

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!

Post Reply