Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to substitute variable contents in a Windows batch file

I'm writing a simple script to substitute text in an environment variable with other text. The trouble I get is with the substituted or substituted text being pulled from other variables

SET a=The fat cat
ECHO %a%
REM Results in 'The fat cat'
ECHO %a:fat=thin%
REM Results in 'The thin cat'

Works fine (output is 'The fat cat' and 'The thin cat'

However, if 'fat' or 'thin' are in variables, it doesn't work

SET b=fat
ECHO %a:%c%=thin%
REM _Should_ give 'The thin cat'.
REM _Actually_ gives '%a:fat=thin%' (the %c% is evaluated, but no further).

REM using delayed evaluation doesn't make any difference either
ECHO !a:%c%=thin!
REM Actual output is now '!a:fat=thin!'

I know this can be done as I've seen it in blogs before, but I never saved the link to the blogs.

Anyone have any ideas?

PS. I'm running the scripts on Windows 7

PPS. I know this is easier in Perl/Python/other script language of choice, but I just want to know why something that should be easy is not immediately obvious.

PPPS. I've also tried the scripts with delayed expansion explicitly turned on

SETLOCAL enabledelayedexpansion

This makes no difference.

like image 835
GKelly Avatar asked Dec 06 '10 14:12

GKelly


People also ask

What does %% do in batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is %1 in a batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

Why we use @echo off in batch file?

If used in a batch file, echo on and echo off don't affect the setting at the command prompt. To prevent echoing a particular command in a batch file, insert an @ sign in front of the command. To prevent echoing all commands in a batch file, include the echo off command at the beginning of the file.

How do I modify a batch file?

Edit a batch file from within Windows Batch files are plain-text files, which means they can be edited as a text file by right-clicking the file and clicking Edit as shown in the picture. Once you've clicked edit, your default text editor opens the file and allows it to be modified.


2 Answers

Please try the following:

Copy and paste the code into Notepad and save it as a batch file.

   @echo off
   setlocal enabledelayedexpansion

   set str=The fat cat
   set f=fat

   echo.
   echo          f = [%f%]

   echo.
   echo        str = [%str%]

   set str=!str:%f%=thin!

   echo str:f=thin = [%str%]

I hope you're convinced!

like image 66
Paul Tomasi Avatar answered Sep 21 '22 12:09

Paul Tomasi


Use CALL. Put the following in a batch script and run it:

set a=The fat cat
set b=fat
set c=thin

REM To replace "%b%" with "%c%" in "%a%", we can do:
call set a=%%a:%b%^=%c%%%
echo %a%
pause

As stated here, we use the fact that:

CALL internal_cmd

...

internal_cmd Run an internal command, first expanding any variables in the argument.

In our case internal_cmd is initially set a=%%a:%b%^=%c%%%.

After expansion internal_cmd becomes set a=%a:fat=thin%.

Thus, in our case running

call set a=%%a:%b%^=%c%%%

is equivalent to running:

set a=%a:fat=thin%.

like image 39
Zuzu Corneliu Avatar answered Sep 25 '22 12:09

Zuzu Corneliu