0

I have many files that are name "12739-5468768.xml", what they have in common is the "12739", inside these files I want to replace every & with " AND " and every </ with nothing. I tried many commands and python command but it doesn't seem to operate on my computer.

@echo off
setlocal 
setlocal enabledelayedexpansion 
set "search=&" 
set "replace= AND " 
set "textfile=12739*" 
set "newfile=Output.txt" 
(
  for /f "delims=" %%i in (%textfile%) do ( 
    set "line=%%i" 
    set "line=!line:%search%=%replace%!" 
    echo(!line!
  )
) > "%newfile%" 
del %textfile% 
rename %newfile% %textfile% 
endlocal
jeb
  • 78,592
  • 17
  • 171
  • 225
jasmine825
  • 144
  • 2
  • 15

3 Answers3

1

Solution for a single file
This handles also !^, empty lines and lines beginning with ; in the file content.

@echo off
setlocal 

call :replaceFile "12739-5468768.xml" 
exit /b

:replaceFile
set "search=&" 
set "replace= AND " 
set "textfile=%~1" 
set "newfile=%~1.replaced" 
setlocal DisableDelayedExpansion 
(
  for /f "usebackq delims=" %%i in (`findstr /N "^" "%textfile%"`) do ( 
    set "line=%%i" 
    setlocal EnableDelayedExpansion 
    set "line=!line:*:=!" 
    if defined line (
      set "line=!line:%search%=%replace%!"
      set "line=!line:</=!"
    )
    (echo(!line!)
    endlocal
  )
) > "%newfile%" 
endlocal
exit /b
jeb
  • 78,592
  • 17
  • 171
  • 225
  • does this work for more than one file, I want to be able to do 12739* – jasmine825 Aug 22 '16 at 13:41
  • Just found with google [Iterate all files in a directory using a for-loop](http://stackoverflow.com/q/138497/463115) – jeb Aug 22 '16 at 13:43
  • I downloaded FART and did FART -i -r "C:\Desktop\def\12739*.xml" & AND But it isnt working for me. – jasmine825 Aug 22 '16 at 13:55
  • @jasmine825 How is FART related with my answer? – jeb Aug 22 '16 at 14:07
  • @jasmine825 You may accept jeb's answer, for is a working solution for you. you only need some homework to make it work with as many files as you want!! `for /r %%x in ("12739*.xml") do call :replaceFile "%%x"` as he suggested – elzooilogico Aug 22 '16 at 14:47
0

You need to escape the & Ampersand using the ^ character.

@echo off
setlocal 
setlocal enabledelayedexpansion 
set "search=^&" 
set "replace= AND " 
set "textfile=12739*" 
set "newfile=Output.txt" 
(
  for /f "delims=" %%i in (%textfile%) do ( 
    set "line=%%i" 
    set "line=!line:%search%=%replace%!" 
    echo(!line!
  )
) > "%newfile%" 
del %textfile% 
rename %newfile% %textfile% 
endlocal
GoodJuJu
  • 1,296
  • 2
  • 16
  • 37
  • 1
    Nice idea, but that's wrong. As the search expression is always inside quotes, it must not be escaped – jeb Aug 22 '16 at 13:09
  • 1
    As said, it can't work. Btw your code will stop at the `for /f "delims=" %%i in (%textfile%)` with the error `the file "12739*" can't be found` – jeb Aug 22 '16 at 13:20
  • This removed my whole file. – jasmine825 Aug 22 '16 at 13:43
0

Download something called "FART" then include it in the folder of the batch then do a bat with this code

"@echo off
FART -i -r "12739*" "&"  " AND " "