StartWith

Execute a Windows command line using .NET format parameters
1.1, 2007-02-04

Let's say you are scheduling a task that needs some date parameters relative to when the task is run, or you are executing a for loop at the command line that needs better control of formatting (such as zero-padding). Wouldn't it be great if you could leverage your existing knowledge of .NET format strings, rather than having to learn a new syntax?

StartWith allows you to do just this. It even determines the correct type of your argument values by trying to parse them as Decimal, Double (use scientific notation, e.g. 5E0 or -2.3E12), DateTime, TimeSpan (returns a DateTime offset from Now), a YearMonthSpan (similar to the XPath 2 yearMonthDuration, using ISO8601 duration syntax: e.g. P1Y2M, -P1M, that also returns a DateTime offset from Now), and just assuming the value is a string otherwise.

Download StartWith.zip (includes C# source code and binary).

Usage

StartWith command-format-string value0 value1 ...

Example 1.1. Test value parsing

StartWith "echo {7}" %random% %random%E0 %random%E-2 %date% %time% P0M 0.00:00 OK

Screen 1.1. Output

0       System.Decimal  28787
1       System.Double   20863
2       System.Double   99.66
3       System.DateTime 2007-02-04 00:00:00
4       System.DateTime 2007-02-04 14:06:14
5       System.DateTime 2007-02-04 14:06:14
6       System.DateTime 2007-02-04 14:06:14
7       System.String   OK
OK

Example 1.2. Compressing last month's IIS log files

StartWith "\"C:\Program Files\7-zip\7z.exe\" ex{0:yyMM}.7z ex{0:yyMM}*.log" -P1M

Example 1.3. Zero-padding for loop variables

for /l in %a in (3,1,11) do StartWith "curl -T ex0611{0:00}.log ftp://example.net/severAlogs/" %a

Example 1.4. Archive a current log file using environment parameters

StartWith "xcopy current.log {0:yyyy-MM-dd}.log" %date%

Example 1.5. Email an image file with yesterday's date in the name using a TimeSpan

StartWith "blat screencap{0:yyyyMMdd}.png -to root@example.com -subject \"{0:ddd} screencap\"" -1.00:00