Friday, October 21, 2005

Command Line Trick

Command Line Trick

Here's a neat trick for you BAT file or CMD file. Searching and replacing inside environment variables. Here's an example:

@echo off
set fred="This is a test. This is only a test."
echo %fred%
rem Remove the quotes.
set fred=%fred:"=%
echo %fred%
rem change This to That.
set fred=%fred:This=That%
echo %fred%
rem Change . to !
set fred=%fred:.=!%
echo %fred%

Produces:

"This is a test. This is only a test."
This is a test. This is only a test.
That is a test. That is only a test.
That is a test! That is only a test!

I tried to find this in Microsoft's documentation but was unsuccessful. Here's a few good links for more command line syntax:
    

0 Comments:

Post a Comment

<< Home