One of my clients was recently merged into another business and they had an archaic system for record keeping that I call “Pile of Files”. They wanted to go through the pile of files and folders and archive all files before the merger date.
This is the script that was the result. Don’t take this script lightly and I would highly recommend going through the generated log file and making sure that everything is good to go before calling an archival project complete.
Also, make sure you have backups before starting any sort of data archival or migration!
@ECHO OFF SETLOCAL EnableDelayedExpansion REM ************************************************** REM This is a custom script whose purpose is to: REM - Recursively search through folders REM - Create a list of old files based on a date REM - Move old files to a storage device REM - Recursively delete leftover empty folders REM ************************************************** REM Search for files prior to the date using format MM/DD/YYYY SET _prior_to=01/01/1900 REM Source locations, using batch "array", list each directory separately to search REM DO NOT USE JUST DRIVE LETTERS (C:\, D:\, etc) OR THE SCRIPT WILL NOT WORK! SET _source[0]=C:\Users\John Doe\Folder 1 SET _source[1]=C:\Stuff SET _source[2]=S:\Old Junk REM Set this to the last element number of _source "array" SET _sourceLastElement=2 REM Destination location SET _dest=D:\Archive REM If these strings are found in a file's path, exclude them from copy/delete SET _exclude[0]=DO NOT DELETE SET _exclude[1]=ExcludeMe REM Set this to the last element number of _exclude "array" SET _excludeLastElement=1 REM ************************************************** REM NOTHING BELOW HERE NEEDS TO BE EDITED! REM ************************************************** REM Log file location SET _log=%UserProfile%\Desktop\archive.log DEL "%_log%" >NUL 2>&1 REM This is a temporary file we create to hold filenames to be moved due REM to a 253 character constraint in the FORFILES /C argument SET _fil_temp=%TEMP%\fil_tmp DEL "%_fil_temp%" >NUL 2>&1 REM Loop through all files from _source[N] and add them to the temporary file REM if older than _prior_to FOR /L %%N in (0,1,%_sourceLastElement%) DO ( FORFILES /s /p "!_source[%%N]!" /d -%_prior_to% /C "cmd /c IF @ISDIR==FALSE ECHO @PATH>>^"%_fil_temp%^"" ) REM Loop through temporary files and perform action FOR /F "tokens=*" %%A IN ( %_fil_temp% ) DO ( SET _exclusionFound= FOR /L %%N in (0,1,%_excludeLastElement%) DO ( ECHO "%%~DPNXA" | FIND /I "!_exclude[%%N]!" >NUL && ( SET _exclusionFound=1 ) ) IF NOT DEFINED _exclusionFound ( ECHO XCOPY "%%~DPNXA" "%_dest%%%~PA">>%_log% 2>&1 XCOPY "%%~DPNXA" "%_dest%%%~PA">>%_log% 2>&1 IF EXIST "%_dest%%%~PNXA" ( ECHO DEL "%%~DPNXA">>%_log% 2>&1 DEL "%%~DPNXA">>%_log% 2>&1 ) ) ) REM Recursively delete all empty directories from our source folders. First REM show all sub directories in reverse order, then remove directories with REM RD. This command will fail for each non-empty directory. FOR /L %%N in (0,1,%_sourceLastElement%) DO ( REM First, clean out Thumbs.db from our _source folders ECHO DEL /S /F /Q !_source[%%N]!\*Thumbs.db>>%_log% 2>&1 DEL /S /F /Q !_source[%%N]!\*Thumbs.db>>%_log% 2>&1 ECHO DEL /S /F /Q /A:H !_source[%%N]!\*Thumbs.db>>%_log% 2>&1 DEL /S /F /Q /A:H !_source[%%N]!\*Thumbs.db>>%_log% 2>&1 FOR /F "delims=" %%A IN ('dir /s /b /ad !_source[%%N]!^| SORT /R') DO ( SET _exclusionFound= FOR /L %%M in (0,1,%_excludeLastElement%) DO ( ECHO "%%~DPNXA" | FIND /I "!_exclude[%%M]!" >NUL && ( SET _exclusionFound=1 ) ) IF DEFINED _exclusionFound ( ECHO Skipping delete of "%%~DPNXA" exclusion found.>>%_log% 2>&1 ) ELSE ( ECHO RD "%%A">>%_log% 2>&1 RD "%%A">>%_log% 2>&1 ) ) ) REM Cleanup our temporary file DEL "%_fil_temp%" >NUL 2>&1 ENDLOCAL