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
0 comments:
Post a Comment