http://www.cherifrost.com/?tag=slap-chop |
There are 2 types of environment variables and the options available for creating substrings on those variables are different. Environment variables that are preceded with a single % sign are passed as arguments into the batch file or subroutine or from the FOR command. Environment variables that are bracketed by % signs are created by the operating system or with the SET command.
OS and SET variables:
%var:str1=str2% | Replace "str1" with "str2" in %var%. If "str1" starts with "*" then replace text up to "str1" |
%var:~a,b% | Substring "b" characters long starting at offset "a" |
%var:~a% | Substring from character "a" to the end |
%var:~-a% | Substring of the last "a" characters |
%var:~a,-b% | Substring starting at "a" of all but the last "b" characters |
Parameters and FOR variables:
%~X | remove quotes from var %X |
%~fX | full path |
%~dX | drive letter only |
%~pX | path only |
%~nX | file name only |
%~xX | extension only |
%~sX | short name |
%~aX | attributes of file |
%~tX | date/time of file |
%~zX | size of file |
All well and good but what about if you have a parameter variable from which you need a positional substring? Use the SET command to assign it to substringable variable.
SETLOCAL
SET X=%1
ECHO %X:~0,1%
This batch file will echo the first character of the string passed as the parameter.SET X=%1
ECHO %X:~0,1%
What about the opposite case where you have a SET variable but you need to parse it as a file path? I use a CALL statement to a subroutine which turns the variable back to a number parameter.
SETLOCAL
SET X=C:\windows\cmd.exe
CALL :Parse %X%
:Parse
ECHO %~d1 %~p1 %~n1 %~x1
The variable %X% becomes my %1 parameter in the subroutine, and I can parse that to get the drive, path, filename, and extension.
SET X=C:\windows\cmd.exe
CALL :Parse %X%
:Parse
ECHO %~d1 %~p1 %~n1 %~x1
So let's put it all together to demo all the slapping and chopping. A couple of notes on the demo below. In the first part of the script the "&" lets you combine multiple commands on a single line. In this case I'm just adding a comment. (I prefer using "::" to make comments rather then the "REM" statement.) In the bottom part of the script I'm parsing the %0 variable which is the script file as its own parameter.
@ECHO Off
SETLOCAL
SET X=You're gonna love my nuts
ECHO %X:~13,4% & :: Word in the middle
ECHO %X:~13% & :: Words to the end
ECHO %X:~-7% & :: Last 7 characters
ECHO %X:~7,-8% & :: Skip first 7 and last 8 chars
ECHO %X:'= a% & :: Replace apostrophe
ECHO.
ECHO Full path of this batch : %~f0
ECHO Drive letter : %~d0
ECHO Relative path : %~p0
ECHO Filename : %~n0
ECHO Extension : %~x0
ECHO Short name : %~s0
ECHO File attributes : %~a0
ECHO Date/time of last save : %~t0
ECHO Size in bytes : %~z0
SETLOCAL
SET X=You're gonna love my nuts
ECHO %X:~13,4% & :: Word in the middle
ECHO %X:~13% & :: Words to the end
ECHO %X:~-7% & :: Last 7 characters
ECHO %X:~7,-8% & :: Skip first 7 and last 8 chars
ECHO %X:'= a% & :: Replace apostrophe
ECHO.
ECHO Full path of this batch : %~f0
ECHO Drive letter : %~d0
ECHO Relative path : %~p0
ECHO Filename : %~n0
ECHO Extension : %~x0
ECHO Short name : %~s0
ECHO File attributes : %~a0
ECHO Date/time of last save : %~t0
ECHO Size in bytes : %~z0
http://www.dostips.com/ BTW, this guy has a bunch of cool tips and tricks if you get stuck having to work some complex batch files.
Commenters: How else do you use substring capabilities for DOS environment variables?
No comments:
Post a Comment