computing
  • 4

MOVE Files Based On DATE MODIFIED

  • 4

So I created the below code to grab the YEAR of a file from the DATE MODIFIED and if it is NOT EQUAL to 2010, then move all the files in that DIR by creating a second .BAT. However, I have MANY sub-dirs, with MORE files, how can I have it go through ALL sub-dirs without having to code the sub dir each time.

echo off & setLocal EnableDELAYedExpansion
PUSHD Y:\DIR1\DIR2\
SET source=Y:\DIR1\SUBDIR1\
SET dest=”F:\DIR1\SUBDIR1\”
for /f “tokens=* delims= ” %%a in (‘dir/b/a-d’) do (
SET DATETIME=%%~Ta
for /f “tokens=1 delims=/: ” %%m in (“%%~Ta”) DO SET month=%%m
for /f “tokens=2 delims=/: ” %%d in (“%%~Ta”) DO SET day=%%d
for /f “tokens=3 delims=/: ” %%y in (“%%~Ta”) DO SET year=%%y
IF “!year!” NEQ “2010” (SET FILE=%%a)
IF “!year!” NEQ “2010” (>>”F:\MOVE.bat” ECHO MOVE /Y “!source!!FILE!” !dest! )
)
CALL “F:\MOVE.bat”

Share

1 Answer

  1. I think OP already got a good solution to his problem. Here is one possible alternative for consideration.

    # Scipt MoveModDate.txt
    var str src, dest, date
    var str list, file, newlocation
    lf -r -n "*" $src ($fmtime < $date) > $list
    while ($list <> "")
    do
        lex "1" $list > $file
        sal -p ("^"+$src+"^") $dest $file > $newlocation
        system -s move ("""+$file+"'"") (""+$newlocation+""")
    done
    

    This in biterscripting. Here is how it works. The lf (list files – http://www.biterscripting.com/helpp… ) finds all files in $src directory (recursively in subfolders -r) whose file mod time is earlier than $date. The $newlocation is created by replacing $src with $dest in $file. Finally, each such $file is moved to corresponding $newlocation. Call the script as

    script MoveModDate.txt src("D:/MAINDIR") dest("E:/MAINDIR") date("20100101")
    

    i think both forward and backward slashes work in file paths.

    • 0