Wednesday, 14 April 2010

Date and Time Injection for Command Line Parameters

I like to backup files using tools such as robocopy, richcopy and 7-Zip, and sometimes I want to include the day or date and time in the backup path or file name. For example, I might want to copy some files to an external hard disk and have them go in to a Monday folder on a Monday, a Tuesday folder on a Tuesday etc.

After looking around I failed to find a satisfactory solution so being a software developer I decided to create my own solution - the result was DateParseCmd.exe.

This is a very simple command line tool written in Delphi 7. The parameters for this command are actually just another command with it's parameters. However, in the child command's parameters, if you want to specify the current date and/or time you can do so by enclosing a Delphi style DateTime Format String in {[ and ]}.

Suppose I want to use a single robocopy script to backup to a DayOfTheWeek folder, I can now do something like this:

DateParseCmd robocopy "C:\My Projects" "F:\Backups\{[dddd]}\My Projects" /MIR /ZB /R:3 /W:10

DateParseCmd first parses it's command line parameters and {[dddd]} will resolve to "Monday" on a Monday. It will then launch the robocopy command with it's newly evaluated parameters i.e.

robocopy "C:\My Projects" "F:\Backups\Monday\My Projects" /MIR /ZB /R:3 /W:10

If I want to archive a folder using the standalone 7-Zip command line tool and have the current date in the archive name so that I never overwrite an archive:

DateParseCmd 7za.exe a -r "D:\My Backups\Source Code {[yyyy-mm-dd]}.7z" "C:\Source Code\*.*"

This will create an archive named "Source Code 2010-04-14.7z". Simple stuff, but very useful.

No comments:

Post a Comment