«

»

Oct 13 2009

Print this Post

Windows Bulk File Move Script

We have some applications that leave files sitting in a folder location out on some file server.  Eventually the disk space gets used up, or Windows starts choking because there are too many files in a directory, etc.  To delete the ‘old’ files, a support technician will try to connect to the file share, sort the files by date, and delete the files within a date range, e.g., older than a year.

What can happen, especially on a Window server, is that the operating system will attempt to load all the file information into memory, so that you can see it, and then tell it to sort all those files by date, which in turn, increases the time and memory needed by the OS to accomplish this.  If the file size/quantity is too great, the problem that caused you to want to clean up the disk space will reappear and choke Windows once more.

After going through this more than twice, I decided to write a JavaScript and execute it using the Windows Scripting Host (WSH).  The script performs very well and was able to iterate through a directory of 700,000+ files in a matter of an hour.  This was including the log statements from being enabled.  Disabling the ‘Echo’ statements may increase performance.

Here is the script:

var fso = new ActiveXObject('Scripting.FileSystemObject');
var argArray = new Array(WScript.Arguments.Named("src"), WScript.Arguments.Named("s"), ".");
var inSrcDirName = coalesceArray(argArray);

argArray = new Array(WScript.Arguments.Named("dest"), WScript.Arguments.Named("d"));
var archiveDir = coalesceArray(argArray);

argArray = new Array(WScript.Arguments.Named("year"), WScript.Arguments.Named("y"));
var beforeYear = coalesceArray(argArray);

if (archiveDir != null) {
	if (!fso.FolderExists(archiveDir)) {
		fso.CreateFolder(archiveDir);
	} else {
		fso.CreateFolder(archiveDir + "1");
	}
}

var gfldr = fso.GetFolder(inSrcDirName);

ArchiveFiles(gfldr);

function ArchiveFiles(fldr) {
	var i, bias;

	WScript.Echo("Loading all files...");
	for (files = new Enumerator(fldr.files); !files.atEnd(); files.moveNext()) {
		f = files.item();
		dt = new Date(f.DateCreated);
		bias = dt.getTimezoneOffset();

		dt.setMinutes(dt.getMinutes() - bias);

		fileYear = dt.getFullYear();
		//WScript.Echo(f.Name + ":" + fileYear);
		if (beforeYear > fileYear) {
			WScript.Echo("Moving \"" + f.Name + "\" with year " + fileYear + " to " + archiveDir);
			f.Move(archiveDir+"\\" + f.Name)
		} else {
			WScript.Echo("Skipping \"" + f.Name + "\" with year " + fileYear);
		}
	}
	WScript.Echo("Done loading files.");

}

function coalesceArray(arr) {
	var i;
	for (i = 0; i < arr.length; i++) {
		if (arr[i] != null) {
			return arr[i];
		}
	}
	return null;
}

Executing the script is performed by passing a source directory, destination directory (to move the files to), and a year to mark the year that all files must be equal to or younger than to not be moved as parameters.

Example:

cscript movefiles.js /s:c:\myapp\data /d:c:\backup\archive_2006 /y:2007

This will move all files from the c:\myapp\data folder that were created before January 1, 2007 to the c:\backup\archive_2006 folder.  The script can be easily modified to use the ‘modified’ or ‘accessed’ date instead.

Hope this helps someone else as much as it helped me.

Permanent link to this article: http://ericsilva.org/2009/10/13/windows-bulk-file-move-script/

1 comment

  1. Eric Silva

    I have since been informed there is a freeware/commercial tool, XXCopy, that performs a similar function and more. I’ll keep that in mind for next time.

    http://www.xxcopy.com

Leave a Reply