0

My text file "new1.txt" contains the following:

1:Q4659A.LDS:4:LNAM=90210;90210
2:Q4159A.LDS:4:LNAM=90232;90210

  1. I have tried to use delayed expansion logic but always end with error as

==,%" was unexpected at this time.

Code:

 set "search=="
 set "replace=,"
 echo %search%
 echo %replace%
 set "textfile=new1.txt"
 set "new1file=new2.txt"
 (for /f "delims=" %%i in (%textfile%) do (
    set "line=%%i"
    set "line=!line:%search%=%replace%!"
    echo(!line!
   endlocal
))>"%new1file%"

What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
user3222101
  • 1,270
  • 2
  • 24
  • 43
  • 1
    Did you see this: http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir ? – harmic Jan 22 '14 at 05:59

2 Answers2

1

If you know the number of elements to replace in each line (1 in your sample), then this can solve the problem

(for /f "tokens=1,2 delims=%search%" %%i in (%textfile%) do (
    echo(%%i%replace%%%j
))>"%new1file%"

Use the equal sign as a delimiter, get the splitted tokens and output them with the desired separator

MC ND
  • 69,615
  • 8
  • 84
  • 126
1

This uses a helper batch file called repl.bat - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place repl.bat in the same folder as the batch file or in a folder that is on the path.

type new1.txt |repl "=" "," >new2.txt
foxidrive
  • 40,353
  • 10
  • 53
  • 68