Batch File Scripting
I was messing around with the Windows Shell (aka cmd), which I do quite frequently, and I found out/remembered a few helpful hints that might make your batch scripting easier. Note that this is just regular old command line, not PowerShell.
Initializing a cmd window
First, opening cmd can be pretty easy. Running a single command is easy too, think ipconfig. Combining both is only "difficult" if you want to have the window stay open when it's finished running the command. I used to execute "ipconfig /all" from the run dialogue box only to have the little black window flash before my eyes before I could read any of the text. After using a little bit of Google-fu I learned that "cmd /k ipconfig" will run cmd, run ipconfig and "/keep" the window open, at least that's what I'll pretend /k stands for.
tl;dr- How to keep the window open and run a command AT THE SAME TIME!:
cmd /k command
As a bonus, an easy way to run a cmd window as an administrator (on Vista/7) is hit the Windows key, type "cmd" then ctrl+shift+enter and the UAC should prompt you to confirm running as an administrator.
Environment Variables
I love Environment Variables in batch files because they are easily declared and easy to use. Most likely you will use them through the "set" command, like so:
Declare and initialize a variable:
set varName=43
Declare and initialize to a value entered by the user, brackets not required:
set /p varName=[String to Display at the Prompt]
To display the variable or use it in a command simply type the variable name surrounded by %'s:
echo %varName%
del /q /f %varName%
There are a lot of built in variables that can be used to find out information about the computer you are using, to see some of them and their variables simply enter "set" and hit return.
Some of the most common ones that I use are %username%, %computername%, %date%, %time%. They are especially useful for logfiles and the like. Since most of my batch files are related to logging something, those variables are the best to recognize who is running the command, on what computer and when.
Sometimes you don't need the entire variable, that is where the ~ key comes in. For a real world example, I needed to store the current drive in a filename as part of the logfiles name (because all the logfiles were being stored in the same place on the same directory). If you view the %cd% variable it will look something like this:
C:\Users\Administrator\Desktop
As you all know : and \ can not be in a filename on windows, so it would be helpful if we could grab just a few characters from the beginning of the %cd% variable. %username:~0,5% will only display the 0th to the 5th characters of the %username% variable, or if you just wanted the current drive letter,
%cd:~0,1%
or to get the root of the system drive (where windows is installed):
%systemDrive:~0,1%
Directory Stacks
A directory stack works much like a computer stack does, meaning first of all it operates on a First In Last Out (FILO) or Last In First Out (LIFO) basis. Directory stacks are very helpful if you need to perform a bunch of operations in many different directories.
The way Windows works is that when you run the "pushd" command it pushes the current directory into the directory stack, then cd's to the directory you set as the parameter. The way to get back to the previous directories is to run the command "popd".
For an example, I want to run the dir command in a few different directories and then return to the original directory:
C:\Windows>pushd D:\Archive D:\Archive>dir D:\Archive>pushd C:\Games C:\Games>dir C:\Games>pushd P:\Movies P:\Movies>dir P:\Movies>popd C:\Games>popd D:\Archive>popd C:\Windows>_
Jumping around different drives and different directories and running the dir command probably is not all that useful, but having a stack with previous directories in it certainly is.
That's it for now.