Unable to modify more than one string in bat file
I am trying to read all files and replace substrings in it.
when I execute bat file only first command executes
i.e. SET modified=!string:%oldstr1%=%newstr1%!
rest 2 commands dont execute
SET modified=!string:%oldstr2%=%newstr2%!
SET modified=!string:%oldstr3%=%newstr3%!
CODE IS AS FOLLOWS
@echo off
setlocal enabledelayedexpansion
set LOCATION=D:\CODE_开发者_高级运维temp\RUNTIME_DATA\
set OUTTEXTFILE=test_out.txt
set oldstr1=workflow.actions
set newstr1=process.activities
set oldstr2=CallWorkflow
set newstr2=CallProcess
set oldstr3=SetWorkflowVariable
set newstr3=SetProcessVariable
FOR /r %LOCATION% %%x IN (*.txt) do (
FOR /f "tokens=1,* delims=¶" %%A in ('"type %%x"') do (
SET string=%%A
SET modified=!string:%oldstr1%=%newstr1%!
SET modified=!string:%oldstr2%=%newstr2%!
SET modified=!string:%oldstr3%=%newstr3%!
echo !modified! >> %OUTTEXTFILE%
)
del %%x
copy %OUTTEXTFILE% %%x
del %OUTTEXTFILE%
echo location %%x >> Enosh_log.txt
)
Hmm, what do you expect?
You always replace a string in the original string
and set it to the modified
variable.
So you always overwrite the same variable.
Better you should use something like this
FOR /f "tokens=1,* delims=¶" %%A in ('"type %%x"') do (
SET "string=%%A"
SET "modified=!string:%oldstr1%=%newstr1%!"
SET "modified=!modified:%oldstr2%=%newstr2%!"
SET "modified=!modified:%oldstr3%=%newstr3%!"
>> %OUTTEXTFILE% echo !modified!
)
精彩评论