Move/sort files to subfolders based on the file's date?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Move/sort files to subfolders based on the file's date?

#1 Post by ©opy[it]®ight » 18 Aug 2012 12:38

Hello again!

I've been searching all over the web for a way to move/sort 18.950+ txt-files i have.

I would like to move the files to a (sub)folder of which the folder-names are based on the (creation) date of the files (DD-MM-YYYY).
This way i don't have to browse through an ever increasing list of .txt files.

The script will be run from one EU/dutch based Win7 system, and the filenames are in the following format:

ip address - name - date (number).txt

(duplicate filesnames will have a number enclosed in parentheses)

Are you up to the task? :wink:

Regards,
- c.i.r.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Move/sort files to subfolders based on the file's date?

#2 Post by abc0502 » 18 Aug 2012 13:03

Try this, but test it first

Code: Select all

@echo off
cls
setlocal enabledelayedexpansion
For %%a in (*.txt) Do (
   set "s=%%~ta"
   IF not exist "!s:~0,2!-!s:~3,2!-!s:~6,4!" MD "!s:~0,2!-!s:~3,2!-!s:~6,4!"
   Move "%%~na.txt" "!s:~0,2!-!s:~3,2!-!s:~6,4!\%%~na.txt"
)

and what duplicate could happen to the names if they already in the same folder

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Move/sort files to subfolders based on the file's date?

#3 Post by ©opy[it]®ight » 18 Aug 2012 13:23

Amazing abc0502! :shock: (jumps from his seat of joy)

Most files got moved correctly (18.965) and only a 15 files had characters that weren't or couldn't be escaped, but that isn't such a problem ;)

Now, i was looking at how the files got moved (in)to their appropriate folder, until another idea popped into my head.

Can you manage to make the sorting even stricter i.e. first create a folder with the year, then a subfolder with the month, and then a subfolder with the day, where the file goes?

Hopefully it's not too much asked! :oops:

(in the mean time i'll try to decypher and learn about your wonderful code :) )

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Move/sort files to subfolders based on the file's date?

#4 Post by abc0502 » 18 Aug 2012 13:44

Code: Select all

@echo off
cls
setlocal enabledelayedexpansion
For %%a in (*.txt) Do (
   set "s=%%~ta"
   IF not exist "!s:~6,4!" MD "!s:~6,4!"
   IF not exist "!s:~6,4!\!s:~3,2!" MD "!s:~6,4!\!s:~3,2!"
   IF not exist "!s:~6,4!\!s:~3,2!\!s:~0,2!" MD "!s:~6,4!\!s:~3,2!\!s:~0,2!"
   Move "%%~na.txt" "!s:~6,4!\!s:~3,2!\!s:~0,2!\%%~na.txt"
)

pause
Last edited by abc0502 on 18 Aug 2012 14:01, edited 1 time in total.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Move/sort files to subfolders based on the file's date?

#5 Post by abc0502 » 18 Aug 2012 13:55

The code is simple,
I will explain as if we was just working on one file only

Code: Select all

For %%a in (*.txt) Do (
set "s=%%~ta"

This get the date and time when the file was created and assign that to the variable "s"
The full creation date/time should look like this:

Code: Select all

07/18/2012 04:45 PM

Code: Select all

IF not exist "!s:~6,4!" MD "!s:~6,4!"

This check to see if a folder exist with the name of "!s:~6,4!" which is the year taken from the full date/time of the file ":~6" skip first 6 characters and ",4" take the next 4 character

then it check to see if there is a folder inside the year folder match the month then acheck for another one inside the month folder match the day folder after that it move that file to the day folder

The for loop will repeat all this steps on all files

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Move/sort files to subfolders based on the file's date?

#6 Post by Ed Dyreen » 18 Aug 2012 14:04

©opy[it]®ight wrote:Most files got moved correctly (18.965) and only a 15 files had characters that weren't or couldn't be escaped, but that isn't such a problem ;)
abc had delayed expansion on, that fail if the file contains carets or exclamation marks.
©opy[it]®ight wrote:Can you manage to make the sorting even stricter i.e. first create a folder with the year, then a subfolder with the month, and then a subfolder with the day, where the file goes?

Code: Select all

@echo off &setlocal disableDelayedExpansion

for %%§ in (

       "*.TXT"

) do   set "$date=" &for %%? in (

       %%~t§

) do   if not defined $date set "$date=%%~?" &for /f "tokens=1-3 delims=/" %%a in (

       "%%~?"

) do   2>nul md "%%~c" "%%~c\%%~b" "%%~c\%%~b\%%~a" &move /y "%%~§" "%%~c\%%~b\%%~a\%%~§"

pause
exit
But there are still some ANSI characters that just can't be handled in DOS.


Groeten,
ED

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Move/sort files to subfolders based on the file's date?

#7 Post by ©opy[it]®ight » 18 Aug 2012 15:52

@ abc0502: Hm! Its less complicated than i thought! :)

I've tested the new script and it works! Thanks a lot man :D

@ Ed Dyreen: Thanks for contributing to my topic, i tested your code but the only thing it did was echoing 'Kan het netwerkpad niet vinden.' (no folders were made)

The folder i run the script from is on my desktop so i'm not sure what or where it goes wrong :o

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Move/sort files to subfolders based on the file's date?

#8 Post by Ed Dyreen » 18 Aug 2012 16:38

'
Not sure why that wouldn't happen with abc's script, they are practically the same :?

Code: Select all

@echo off &setlocal disableDelayedExpansion

pushd "%~dp0" &&(

       for %%§ in (

              "*.TXT"

       ) do   set "$date=" &for %%? in (

              %%~t§

       ) do   if not defined $date set "$date=%%~?" &for /f "tokens=1-3 delims=/" %%a in (

              "%%~?"

       ) do   2>nul md "%%~c" "%%~c\%%~b" "%%~c\%%~b\%%~a" &move /y "%%~§" "%%~c\%%~b\%%~a\%%~§"

       popd
)

pause
exit
But adding 'pushd' should make it run from UNC paths.

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Move/sort files to subfolders based on the file's date?

#9 Post by ©opy[it]®ight » 18 Aug 2012 17:31

Using your modified script i'm still getting the same output as before o_O (i've removed the 2>nul part to see what's going on behind the 'scenes'):

De syntaxis van de bestandsnaam, mapnaam of volumenaam is onjuist.
Er is een fout opgetreden tijdens het verwerken van: .
Toegang geweigerd.
Er is een fout opgetreden tijdens het verwerken van: \.
Het opgegeven pad is ongeldig.
Er is een fout opgetreden tijdens het verwerken van: \\01-07-2012.
Kan het netwerkpad niet vinden.
De syntaxis van de bestandsnaam, mapnaam of volumenaam is onjuist.
Er is een fout opgetreden tijdens het verwerken van: .
Toegang geweigerd.
Er is een fout opgetreden tijdens het verwerken van: \.
Het opgegeven pad is ongeldig.
Er is een fout opgetreden tijdens het verwerken van: \\01-07-2012.
Kan het netwerkpad niet vinden.
De syntaxis van de bestandsnaam, mapnaam of volumenaam is onjuist.
Er is een fout opgetreden tijdens het verwerken van: .
Toegang geweigerd.
Er is een fout opgetreden tijdens het verwerken van: \.
Het opgegeven pad is ongeldig.
Er is een fout opgetreden tijdens het verwerken van: \\01-07-2012.
Kan het netwerkpad niet vinden.


This, while running it from a conventional (local) folder..
I'm flabbergasted :?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Move/sort files to subfolders based on the file's date?

#10 Post by abc0502 » 18 Aug 2012 18:04

i think for some reason the $date is not set, so the folders won't be created

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Move/sort files to subfolders based on the file's date?

#11 Post by abc0502 » 18 Aug 2012 18:17

OK, I found the mistake, the first and second batchs Ed Dyreen posted is working fine, just remove the qoutes arround "*.TXT" in the first For loop

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Move/sort files to subfolders based on the file's date?

#12 Post by ©opy[it]®ight » 18 Aug 2012 19:26

Nope.. that didn't work. But i think i've found the problem.

The value "%%~c\%%~b\%%~a" is always empty, which results in two \\ in the path from %%~c\ and \%%~a.
Since %%~b has no value it breaks the script (even when treating it as UNC by using PUSHD and POPD).

The solution? I don't know :-/

edit: hmm, its even worse. The value %%~c is also empty.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Move/sort files to subfolders based on the file's date?

#13 Post by Ed Dyreen » 18 Aug 2012 20:14

©opy[it]®ight wrote:@ Ed Dyreen: Thanks for contributing to my topic, i tested your code but the only thing it did was echoing 'Kan het netwerkpad niet vinden.' (no folders were made)
Not really helpful, until you posted your debug :)
Both scripts work for XP and the latter even works from UNC paths.
Looks like they changed the delimiter when they released 7 however, the win7 fix shouldn't be too hard.

Code: Select all

@echo off &setlocal disableDelayedExpansion

pushd "%~dp0" &&(

       for %%§ in (

              "*.TXT"

       ) do   set "$date=" &for %%? in (

              %%~t§

       ) do   if not defined $date set "$date=%%~?" &for /f "tokens=1-3 delims=/-" %%a in (

              "%%~?"

       ) do   2>nul md "%%~c" "%%~c\%%~b" "%%~c\%%~b\%%~a" &move /y "%%~§" "%%~c\%%~b\%%~a\%%~§"

       popd
)

pause
exit
Couldn't sleep either, it's too hot over here :wink:

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Move/sort files to subfolders based on the file's date?

#14 Post by ©opy[it]®ight » 18 Aug 2012 21:58

Ahh, works! :-)

You two make it look SO simple, hehe.

Before i make another attempt to try to sleep, i have a final 'challenge' for you Ed Dyreen.

Make the script so that it doesn't try to create the same folder over and over, but add an IF EXIST statement (like abc0502 did with his script).
Then it will skip the MD command if it encounters an already existing folder ;-)

Thanks a lot, abc0502 and Ed Dyreen!

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

Re: Move/sort files to subfolders based on the file's date?

#15 Post by foxidrive » 18 Aug 2012 23:36

©opy[it]®ight wrote:Before i make another attempt to try to sleep, i have a final 'challenge' for you Ed Dyreen.


This is not a place to challenge people to write batch files for you. It makes you seem like a prat who uses other people for your own gain.

Make the script so that it doesn't try to create the same folder over and over, but add an IF EXIST statement (like abc0502 did with his script).
Then it will skip the MD command if it encounters an already existing folder ;-)


Why?? The overhead of the command and redirecting to nul is small.

And why can't you add an if exist command yourself if you want it?

Post Reply