Wednesday, September 24, 2014

Move a file into a directory of the same name

The need arose recently at $work that required a script to search a directory for all PST files, create a directory with the same name of each file (excluding the file extension) and move that file into the directory of the same name.

This was accomplished using  powershell:

get-childitem . *.pst | `

foreach-object{
$cur=$_.BaseName
new-item -type directory $cur
move-item $_.name $cur
}

Display the start and finish time of a batch file

Not an overly difficult task, but something useful I now use when a batch file is required in Windows land.

To Display the start time of a script

@ECHO OFF
setlocal enableDelayedExpansion

echo Started: !DATE! - !TIME!

The enableDelayedExpansion line stops the script form expanding the date and time variables at compile time, and instead executes them real time. So you can add the above to the start of the script and the following at the end to display the date and time before script execution completes:

echo Finished: !DATE! - !TIME!