A Batch file to read a file and replace a string with a new one

22,676

Solution 1

Try out this small script:

@echo off
set val=50
echo %val%
for /l %%i in (1,1,1) do (
    set val=%%i
    echo %val%
)
echo %val%
pause>nul

The output is:

50
50
1

Not what you expected, right?

That's because in a for loop, variables aren't updated until the loop has finished. To combat this, you can use setlocal enabledelayedexpansion, and replace the percent signs (%) with an exclamation mark (!):

@echo off
setlocal enabledelayedexpansion
set val=50
echo %val%
for /l %%i in (1,1,1,) do (
    set val=%%i
    echo !val!
)
echo %val%
pause>nul

The output:

50
1
1

The reason the str variable holds no value (during the for loop) is because it hasn't been set beforehand.

So, with these quick modifications, your script will work...

setlocal enabledelayedexpansion
for /f "tokens=*" %%i in (myfile) do (
    set str=%%i
    set str=!str: %oldstring% = %newstring%!
    echo !str! >> newfile
)

By the way, this snippet is assuming that oldstring and newstring won't be set within the forloop, otherwise things will get messy.

Have fun.

Solution 2

having spent some time at this I got the correct way:

@echo off
setlocal enabledelayedexpansion

set oldstring=AF-07295
set /a count=1000

for %%f in (*.*) do (
  set /a count=!count!+1
  for /f "tokens=*" %%i in (%%f) do (
    set str=%%i

    call set str=%%str:!oldstring!=!count!%%
    echo !str! >> %%~nf.ordnew

  )
)

endlocal

Solution 3

setlocal ENABLEDELAYEDEXPANSION
set filein="c:\program files\test1.txt"
set fileout="c:\program files\test2.txt"
set old=@VERSION@
set new=2.0.3
for /f "tokens=* delims=¶" %%i in ( '"type %filein%"') do (
    set str=%%i
    set str=!str:%old%=%new%!
    echo !str! >> %fileout% 
)

working perfect and isn't removing white spaces at the begining of the lines file

Share:
22,676
Joyel
Author by

Joyel

Updated on July 09, 2022

Comments

  • Joyel
    Joyel almost 2 years

    I want to create a batch file to read every line of a file in a loop and replace a string with another one. Following is my code snippet:

    for /F "tokens=*" %%i in (myfile) do (
      set str=%%i
      set str=%str: %oldstring% = %newstring%%
    echo %str% >> newfile
    )
    

    This results in a newfile with 'Echo is off' as many lines as there are in myfile. Seems like str variable holds no value at all when assigned to %%i. Can someone help me?

  • Joyel
    Joyel about 11 years
    Thanks a trillion!! I'm absolutely new in batch programming. I've seen 'setlocal enabledelayedexpansion' before but dint know its function. Hence never used it. Thank you for the explanation. And yes, oldstring & newstring were set outside the loop and my code works fine now! Thanks again..:)
  • BDM
    BDM about 11 years
    @user2147904 Did it answer your question, if it did, click accept. If it only helped, upvote.
  • Joyel
    Joyel about 11 years
    Hi Prof Pickle, I had another question. I think you cannot give space after str:. Now, suppose oldstring is a variable assigned (and changed) inside the for loop. Then we have to use set str=!str:!oldstring!=%newstring%!. But it doesnt work as it when it encounters 2nd !, it terminates the purpose. What is the solution? Please help.
  • BDM
    BDM about 11 years
    Try set str=%str:!oldstring!=!newstring!% i'm on my phone so I can't test it
  • BDM
    BDM about 11 years
    Can you avoid setting %oldstring% in the for loop?
  • Joyel
    Joyel about 11 years
    Actually I need to read all the text files from a folder and then replace a certain type of string with another in each of them. So, the oldstring keeps changing with every file. Thats y I have to keep it in the for loop. Following is the code snippet: for /r %%g in (.txt) do ( set filename=%%~nxg echo !filename! for /F "tokens=3 delims=<>" %%i in ('findstr "volumelabel" !filename!') do ( set oldstring=%%i ) for /F "tokens=" %%a in (!filename!) do ( set str=%%a set str=%str:!oldstring!=%newstring%% echo !str!>>new!filename! ) )
  • BDM
    BDM about 11 years
    I guess the only thing to do now is to post another question.
  • Joyel
    Joyel about 11 years
    What folder? It doesnt create any folder. It prints only 'oldstring' (literally) in every line of the new files when set str=!str:!oldstring!=%newstring%! echo !str!>>new!filename!is used and prints oldstring=newstring (values) when set str=%str:!oldstring!=%newstring%% echo !str!>>new!filename! is used.
  • BDM
    BDM about 11 years
    Wrong question (it's hard balancing two discussions). I deleted the comment.