Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file to move files by date modified

I have written a batch file, which creates empty folders for each date. My next task is to create another batch file, which moves each file in a directory, into the relevant date folder, based on their date modified. I have read numerous forums and articles on how I can achieve this, but with my limited batch file knowledge, I just cannot seem to get it to work. The code I currently have is shown below, though this does not seem to pull in the date modified. Any help is much appreciated!

SET directory="\directory\path\archive"

FOR /f %%a in ('dir /b "%directory%"') do (

SET fdate=%%~Ta

MOVE "%directory%\%%a" "%directory%\%fdate%"
like image 691
tob88 Avatar asked Dec 13 '11 11:12

tob88


Video Answer


1 Answers

Until you provide more info as to format of dates, I can't give a definitive answer. But I can show you how I would do it on my machine.

I use yyyy-mm-dd format within file and folder names, so December 13, 2011 would be 2011-12-13. My machine uses mm/dd/yyyy format for dates (12/13/2011). So I would need to translate the %%~tF output from 12/13/2011 into 2011-12-13. Note - / cannot be used used in file or folder names.

So this code would do what you want on my machine:

set "source=\directory\path\archive"
set "targetRoot=\directory\path\archive"
for %%F in ("%source%\*") do (
  for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
    move "%%~fF" "%targetRoot%\%%C-%%A-%%B"
  )
)

Addendum - Question in comment asks for method to left pad a number with zeros for dir creation. I see two easy choices. (This really should be a different question)

This first method is simple but tedious and not practical as a general solution

for %%A in (01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31) do ...

The second method is a general solution. Since your assignment is within parentheses, you need to use delayed expansion.

setlocal enableDelayedExpansion
for /l %%A in (1 1 31) do (
  set "day=0%%A"
  set "day=!day:~-2!
  ...
)

You increase the number of leading zeros by adding more 0 to the front and then increasing the number of characters you preserve in the substring operation.

BUT - why prepopulate the directories? Your strategy will add directory days that don't exist in the calendar, plus you are likely to have many unused folders for which no files were modified on that day. Better to create the folders only as needed. Then the 0 padding is already done for you, and no unneeded folders are created.

set "source=\directory\path\archive"
set "targetRoot=\directory\path\archive"
for %%F in ("%source%\*") do (
  for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
    if not exist "%targetRoot%\%%C\%%A\%%B" mkdir "%targetRoot%\%%C\%%A\%%B"
    move "%%~fF" "%targetRoot%\%%C\%%A\%%B"
  )
)
like image 53
dbenham Avatar answered Sep 28 '22 05:09

dbenham