venerdì 16 ottobre 2009

Sorting files into folders by date [ENG]

If you happen to have a folder ridden with tons of files and want to split them in folders by arrival date (think log files, or automatic downloads, or... ehm... p0rn, maybe?) these batch script may lend a hand:


move_file_to_date_dir.bat
@echo off
set tempo=%~t1
if NOT "%tempo%" EQU "" goto do_it
echo Error processing file "%~f1": time was not set
goto :exit_batch
:do_it
set tempo_formatted=%tempo:~6,4%%tempo:~3,2%%tempo:~0,2%
echo Moving "%~f1" to "%tempo_formatted%"...
if exist "%tempo_formatted%" goto move_file
mkdir "%tempo_formatted%"
:move_file
move "%~f1" "%tempo_formatted%"
:exit_batch

move_files_to_date_dir.bat
@echo off
for %%v in (%1) do (
call "move_file_to_date_dir.bat" "%%v"
)

The latter loops through the files in a given directory and invokes the former to move the file to a directory based on the file creation date. If the directory does not exist it will be created. With little tweaks these can be used to group files for month or year.

NOTE: there was a very good reason for the double %% in the second batch, but at the moment I can't remember it to save my life

NOTE2: there was a very good reason for splitting the script in two, but at the moment I can't remember it to save my life

Nessun commento: