Page 1 of 3
Move/sort files to subfolders based on the file's date?
Posted: 18 Aug 2012 12:38
by ©opy[it]®ight
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? 
Regards,
- c.i.r.
Re: Move/sort files to subfolders based on the file's date?
Posted: 18 Aug 2012 13:03
by abc0502
Try this,
but test it firstCode: 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
Re: Move/sort files to subfolders based on the file's date?
Posted: 18 Aug 2012 13:23
by ©opy[it]®ight
Amazing abc0502!

(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!

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

)
Re: Move/sort files to subfolders based on the file's date?
Posted: 18 Aug 2012 13:44
by abc0502
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
Re: Move/sort files to subfolders based on the file's date?
Posted: 18 Aug 2012 13:55
by abc0502
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
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
Re: Move/sort files to subfolders based on the file's date?
Posted: 18 Aug 2012 14:04
by Ed Dyreen
©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
Re: Move/sort files to subfolders based on the file's date?
Posted: 18 Aug 2012 15:52
by ©opy[it]®ight
@
abc0502: Hm! Its less complicated than i thought!

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

@
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

Re: Move/sort files to subfolders based on the file's date?
Posted: 18 Aug 2012 16:38
by Ed Dyreen
'
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.
Re: Move/sort files to subfolders based on the file's date?
Posted: 18 Aug 2012 17:31
by ©opy[it]®ight
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

Re: Move/sort files to subfolders based on the file's date?
Posted: 18 Aug 2012 18:04
by abc0502
i think for some reason the $date is not set, so the folders won't be created
Re: Move/sort files to subfolders based on the file's date?
Posted: 18 Aug 2012 18:17
by abc0502
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
Re: Move/sort files to subfolders based on the file's date?
Posted: 18 Aug 2012 19:26
by ©opy[it]®ight
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.
Re: Move/sort files to subfolders based on the file's date?
Posted: 18 Aug 2012 20:14
by Ed Dyreen
©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

Re: Move/sort files to subfolders based on the file's date?
Posted: 18 Aug 2012 21:58
by ©opy[it]®ight
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!
Re: Move/sort files to subfolders based on the file's date?
Posted: 18 Aug 2012 23:36
by foxidrive
©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?