win 2000 server batch script to move all files older than X days from directory tree to network share with rewrite

daveaths used Ask the Experts™
Have a directory structure (see code A) that I need to parse for files older than 30 days and *move* them to a network share drive.

The destination needs to drop the files/directories into a directory based on the first three characters of the original directory.  For example see Code B

note the creation of the additional ABC and DEF directories which group them.

quick and dirty solution is all we need as this is a stop gap measure, though it should create missing directories in the target as needed.

Code A +++++++++++++++
Code B +++++++++++++++
The above examples would be moved into the following structure;

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Paste the script below into a text file with a .cmd extension.  Customize the value of the source variable on line 4 with the location of the folder to search under.  Customize the value of the dest variable on line 5 with the location of the folder to copy files under.  Place robocopy.exe in the same directory to the script, or in a folder in the system path, e.g. C:\WINDOWS.

Running the script will echo robocopy commands to be run.  Once you have tested it successfully and are sure it will do what you intend, remove the word ECHO from line 11 to move the files.

@echo off
setlocal enabledelayedexpansion
set source=E:\ParentDir
set dest=\\FileServer\ParentDir
set daysold=30
set log=log.txt
for /F "tokens=*" %%G in ('dir "%source%" /A:D /B /S') do (
 set folder=%%~nG
 ECHO robocopy "%%G" "%dest%\!folder:~0,3!\!folder!" /MOV /MINAGE:%daysold% /LOG+:"%log%"

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
For test purpose, instead of echoing the commands you can use the line

robocopy  "%%G" "%dest%\!folder:~0,3!\!folder!" /MOV /MINAGE:%daysold% /LOG+:"%log%" /L

The /L does start a simulation mode, no changes to files or folders are done, but all actions to be performed are shown.


took a bit of playing, only because there were over 1mil files in the directory tree (appeared to be hanging but was actually crunching).

One point to note for future reference, the robocopy had to be downloaded as part of the 2003 resource kit which had to be installed on a 2003/XP system first...  I then copied over the robocopy executable to the win200 server and it worked like a charm.

Original post worked and solved the problem, partial points awarded for the use of the native test mode which made it more eloquent.  Hope this is fair.

Thanks a bundle

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial