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!

Tuesday, September 2, 2014

Display the second instance of a word

A quick post to share a handy command that displays the second instance of a word. For this example I was trying to quickly get the IP address for a list of switches by using nslookup and only printing the device name, and it's IP (not other details like dns server).

for x in `cat /tmp/switch.list`; do echo ${x}:; nslookup ${x} | awk '/Address/{i++}i==2'; done