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" <aa...@chebucto.ns.ca> 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]).
>
>
>Thanks,
>
>Greg

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
2000.

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
%PATH:str1=%
%PATH:*str1=str2%

%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
%test:~0,5%
12345

Skip 7 characters and then grab the next 5
%test:~7,5%
89abc

Skip 7 characters and then grab everything else
%test:~7%
89abcdef0

Grab only the last 7 characters #
%test:~-7%
abcdef0

Grabs everything BUT the last 7 characters #
%test:~0,-7%
123456789

Grab between 7 from the front and 5 from the back #
%test:~7,-5%
89ab

Go back 7 from the end then grab 5 towards the end #
%test:~-7,5%
abcde

Grab between 7 from the end and 5 from the end #
%test:~-7,-5%
ab


[%~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
CCalvert@Wanguru.com
Replace "W" with "L"