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
2
  • And it just even tell me where it fails, nothing appears. Commented Aug 22, 2016 at 12:55
  • 1
    I get the error the file "12739*" can't be found, that's because FOR/F doesn't accept wildcards Commented Aug 22, 2016 at 13:01

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

5 Comments

does this work for more than one file, I want to be able to do 12739*
I downloaded FART and did FART -i -r "C:\Desktop\def\12739*.xml" & AND But it isnt working for me.
@jasmine825 How is FART related with my answer?
@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
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 " "

Comments

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

3 Comments

Nice idea, but that's wrong. As the search expression is always inside quotes, it must not be escaped
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
This removed my whole file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.