Pages

Wednesday, July 15, 2009

General Quick Reference: Batch File Tips

This will be kind of out-of-place here... but every once in a while I need to use batch files to get something done.  These are some of the more complicated things I tend to use, and as such, I'm constantly forgetting them and having to look for old batch files that do the same thing.  That's hard.  This is easier.  As always, I'll update this as I find other things that I need to find faster :) (Am I a dinosaur?  Has Microsoft replaced this venerable technology with something else baked in to Windows? Let me know...)
Resources
These are sites that I usually run into when searching the web for help for this kind of stuff.
Computer Hope (Batch Files)
Rob van der Woude's Site
Substring Equivalent
To get partial contents of a variable at a known position and length, use:
%[variable]:~[start],[length]%
Where [start] is a zero-based index into the [variable], and [length] is a non-zero length.
Examples
To reformat the contents of the DATE environment variable (which on my system is DD/MM/YYYY) to YYYY-MM-DD format:
Echo %DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%
or
SET REFORMATTED_DATE=%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%
Replace Equivalent
To find and replace a specific character in a variable, use:
%[variable]:[string to find]=[replacement string]%
Where [replacement string] can be empty.
Examples
To strip quotations off of a variable (I just found this one - it was causing me grief for long path names passed in as arguments to the batch file):
SET LONG_PATH_NAME="C:\Program Files\Microsoft SQL Server\"
Echo %LONG_PATH_NAME:"=%

Using FOR Loops
Complete reference at Computer Hope.
FOR %%C IN ("filespec here") DO command here
Common replacement terms for the resulting file argument:
%%~nC uses just the filename itself, no pathing
Waiting A Specific Length of Time
Complete reference at Rob van der Woude's blog.
PING 127.0.0.1 -n
Example that waits five seconds:
PING 127.0.0.1 -n 6
Finding the Current Working Folder
Complete reference at Wes' Puzzling Blog
To find what folder the user/system has executed your batch file from, use the CD environment variable.
Example:
Echo %CD%
Finding the Batch File's Pathname
Complete reference at Wes' Puzzling Blog
To find what the full pathname of the currently executing batch file is (NOT the folder the batch file is being executed from), use the "0" (zero) environment variable.
Example:
Echo %0
This can be parsed into removing quotes, and/or only showing the path portion, for example:
Echo %~fp0
Echo %~dp0
More Magic With Arguments
Do any of the following where "I" is the ordinal number of the argument to the batch file, like "0" for the first argument (the name of the batch file itself):
%~I         - expands %I removing any surrounding quotes ("")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first one found.  If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string.

No comments:

Post a Comment