* You are viewing the archive for the ‘Development’ Category

Use PowerShell utilities in your Windows CMD scripts

Want a random number in your Windows command file? This little trick works wonders and uses PowerShell to generate the number, mapping the returned value to a script variable.

for /f “usebackq” %%i in (`PowerShell $randomNumber ^= Get-Random^; $randomNumber;^`) do set VALUE=%%i
echo Value is %VALUE%

Use the same pattern to enrich your CMD scripts with other PowerShell utilities and code, for example…

for /f “usebackq” %%i in (`PowerShell $date ^= Get-Date^; $date.ToString^(‘yyyy-MM-ddThh:mm:ss’^)`) do set LOGDATE=%%i
echo Date is %LOGDATE%

just remember to escape any script special characters (< > | etc) using a caret (^)

Use LINQ to “Zip” collections together

The Zip method pairs together the items in 2 Enumerable collections into 1 single¬† collection that can be used to enumerate through the paired items .¬† Each pair of items can then be operated on to produce a final collection. Put simply, the item at index 1 in my “Zipped” collection contains the items at index 1 from the original 2 collections. The Zip stops when one of the source collections runs out of items (since Zip cant make any more pairs of items).

Zip takes the two collections to index through and a function to apply on each index … Continue Reading