Google Groups

string manipulation in a batch file...

Clay Calvert Mar 25, 2001 7:50 AM
Posted in group: alt.msdos.batch.nt
On Sun, 25 Mar 2001 09:26:14 GMT, "D'oh" <> wrote:

>It is my understanding that Windows 2000 (possibly NT?) has finally
>introduced some string manipulating capabilities.  Do any of you know of any
>good sites that give an overview/examples of this?  I've searched the net
>but have come up empty (which doesn't surprise me....I can't seem to find
>anything anymore :).
>Right now what I want to do is pretty simple (search for a substring [which
>I could do right now by piping the variable to file and using find] and
>possibly character/substring replacement [which I gather can be done by the
>set command from reading one of the earlier posts]).

SET %Var%=%Var:str1=str2%  is the syntax for string replacement in NT
and Win2K.

Set WeekDay=Monday
Set WeekDay=%WeekDay:Mon=Satur%
Echo %WeekDay%

The following are examples of variable substitution and parsing in
Windows NT and Windows 2000.

A "#" character will indicate functionality only available in Windows

More information can be found from running "Set /?" and "For /?".

A "one-sided" variable is a single character variable that has a
single percent sign preceding the character.  

Examples:  %1  and  %a

"Two-sided" variables can have more than one character and have a
percentage sign on both sides.
Examples:  %temp%  and  %path%

%PATH:str1=str2%   Substitution [: and =] Two sided only

%Test:ab=xy%   123456789xycdef0
%Test:ab=%     123456789cdef0
%Test:*ab=%    cdef0
%Test:*ab=XY%  XYcdef0

[%:~X and %:~X,Y]        Two sided variables only

The following string will be used for these examples:
set test=123456789abcdef0

Grab only the first 5 characters

Skip 7 characters and then grab the next 5

Skip 7 characters and then grab everything else

Grab only the last 7 characters #

Grabs everything BUT the last 7 characters #

Grab between 7 from the front and 5 from the back #

Go back 7 from the end then grab 5 towards the end #

Grab between 7 from the end and 5 from the end #

[%~letter]        One sided variables only

%~I   removes any surrounding quotes (")
%~fI  Fully qualified path name
%~dI  Drive letter only
%~pI  Path only
%~nI  file Name only
%~xI  eXtension only
%~sI  Short names only
%~aI  Attributes #
%~tI  Time and date of file #
%~zI  siZe of file #

[%~$string]  Two-sided
%~$PATH:I    searches the PATH and expands to the full name of the
first found

The modifiers can be combined to get compound results:

%~dpI       - expands %I to a drive letter and path only
%~nxI       - expands %I to a file name and extension only
%~fsI       - expands %I to a full path name with short names only
%~dp$PATH:i - searches the PATH and expands to the drive letter and
path of the first found
%~ftzaI     - expands %I to a DIR like output line #

In the above examples %I and PATH can be replaced by other valid
values.  The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.

Clay Calvert
Replace "W" with "L"