I am trying to organize all files (and directories) on the T:
drive into two directories: Pre2010
and PostAnd2010
. The files should be organized based on their last modified time--so if file T:\dirA\fileA1
has the timestamp 12/28/2015 03:42pm
it should be moved to T:\PostAnd2010\dirA\
, maintaining the same file name. Also, if the file T:\dirA\fileA2
has the timestamp 1/13/2009 12:23am
it should be moved to T:\Pre2010\dirA\
with the same filename. So the new Pre2010
and PostAnd2010
directories should maintain the same directory structure as the T:
drive previously had, but splitting the files by date.
I don't really have any experience writing batch files, but I have been trying! Here's my code so far:
@ECHO OFF
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
chdir /d T:
FOR /r %%i IN (*) do (
FOR %%a in (%%i) do set FileDate=%%~ta
set year=!FileDate:~6,4!
if !year! LSS 2010 (
REM Move to T:\Pre2010\<filepath>
echo !year! is before 2010
) else (
REM Move to T:\PostAnd2010\<filepath>
echo !year! is 2010 or after
)
REM echo !year!
pause
)
echo Done.
pause
This seemed to be working okay, but then I noticed that the year would occasionally be printed as ~6,4
instead of the actual year. I checked the timestamps of the files that were causing the issue, but nothing seemed strange about them. I am not sure why FileDate
and year
are blank on some files. I want to figure out this issue before I finish the moving the files portion.
Any help would be greatly appreciated! Thanks!