Finding and replacing in file with special char?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
asparagus
Posts: 4
Joined: 23 May 2012 09:47

Finding and replacing in file with special char?

#1 Post by asparagus » 23 May 2012 10:43

I need to make a .bat file that finds and replaces a few words. I can do this just fine, but the file I am reading contains: "< ............. &# (ignore the periods, just they just separate the two sets of characters) when it reads these two special characters, it cannot process them and simply erases the line (echos out "< was unexpected at this time" or "'#xA' is not recognized as an internal or external command, operable program of batch file." depending on which set it reads and the line is not present in the output). Here is my code:

Code: Select all

@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

set file=oldfile.txt

echo Please wait, this process may take up to 15-20 minutes.
title parsing file, "%file%"

call:Change "%file%" > "newfile.txt"

MOVE /Y "newfile.txt" "oldfile.txt"

goto:EOF
:Change
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %file%|find /n /v """') do (
     set "line=%%B"
     if defined line (
         call:replace "oldstr1" "newstr1"
         call:replace "oldstr2" "newstr2"
         call:replace "oldstr3" "newstr3"
         call:replace "oldstr4" "newstr4"
         call set "line=echo.%%line%%"
         for /f "delims=" %%* in ('"echo."%%line%%""') do (%%~*)
     ) ELSE (echo.))
exit /b 0

:replace
>nul 2>&1 (
call set line |Findstr.exe /RIC:"\<%~1\>" && call set line |Findstr.exe /RIVC:"\<@%~1\>" && call set "line=%%line:%~1=%~2%%")
exit /b 0


This is my first batch program and I've taken most of this code from around the internet. Any help would be greatly appreciated. Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Finding and replacing in file with special char?

#2 Post by foxidrive » 23 May 2012 19:43

What is the task? SED or a VBS script might be better than search/replacing with vanilla batch.

asparagus
Posts: 4
Joined: 23 May 2012 09:47

Re: Finding and replacing in file with special char?

#3 Post by asparagus » 24 May 2012 09:52

Unfortunately that's not an option; is there any way to do it with just the batch file? It works fine for other files but the moment I hit "< or &# it doesn't know what to do. Was there ever a workaround for this?

Thanks

aGerman
Expert
Posts: 4740
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Finding and replacing in file with special char?

#4 Post by aGerman » 24 May 2012 10:48

Check this out:

viewtopic.php?f=3&t=2710

Perhaps much faster than all the CALLs in your code.

Regards
aGerman

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Finding and replacing in file with special char?

#5 Post by Ed Dyreen » 24 May 2012 10:50

'

Code: Select all

:: Strip
:: (
   set "?=!?:/=!"
   set "?=!?:-=!"
   set "?=!?:\=!"
   set "?=!?: =!"
   set "?=!?:,=!"
   set "?=!?:.=!"
   set "?=!?::=!"
   set "?=!?:(=!"
   set "?=!?:)=!"
   set "?=!?:&=!"
   set "?=!?:|=!"
   set "?=!?:>=!"
   set "?=!?:<=!"
   set "?=!?:^%=!"
   set "?=!?:?=!"
   set "?=!?:^!=!"
   set "?=!?:;=!"

   set "?=!?:"=!"
   set ?=!?:^"=!    if followed by a command on same line use above !

   set "?=!?:""="!^"
   set ?=!?:""=^"! if followed by a command on same line use above !
:: )
There are some chars that are a little tricky
How to replace "=","*", ":" in a variable

asparagus
Posts: 4
Joined: 23 May 2012 09:47

Re: Finding and replacing in file with special char?

#6 Post by asparagus » 24 May 2012 11:35

Hi thanks for your replies.
@aGerman Sadly I cannot use the code you suggested since my lines exceed 1021 characters. Thanks anyway.

@Ed I tried putting that code near the top of my program and it had no effect. Am I using it incorrectly? Thanks

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Finding and replacing in file with special char?

#7 Post by Squashman » 24 May 2012 12:06

asparagus wrote:@Ed I tried putting that code near the top of my program and it had no effect. Am I using it incorrectly? Thanks

It might help Ed if you actually re-posted all the code including where you put the new code so he can see how you tried to use it. But I am betting it is not working because you explicitly disabled delayed expansion and never enabled it.

asparagus
Posts: 4
Joined: 23 May 2012 09:47

Re: Finding and replacing in file with special char?

#8 Post by asparagus » 24 May 2012 12:31

Sorry about that here's what I did:

Code: Select all

@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

set file=oldfile.txt
set "?=!?:/=!"
set "?=!?:-=!"
set "?=!?:\=!"
set "?=!?: =!"
set "?=!?:,=!"
set "?=!?:.=!"
set "?=!?::=!"
set "?=!?:(=!"
set "?=!?:)=!"
set "?=!?:&=!"
set "?=!?:|=!"
set "?=!?:>=!"
set "?=!?:<=!"
set "?=!?:^%=!"
set "?=!?:?=!"
set "?=!?:^!=!"
set "?=!?:;=!"
set "?=!?:"=!"
set "?=!?:""="!^"

echo Please wait, this process may take up to 15-20 minutes.
title parsing file, "%file%"

call:Change "%file%" > "newfile.txt"

MOVE /Y "newfile.txt" "oldfile.txt"

goto:EOF
:Change
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %file%|find /n /v """') do (
     set "line=%%B"
     if defined line (
         call:replace "oldstr1" "newstr1"
         call:replace "oldstr2" "newstr2"
         call:replace "oldstr3" "newstr3"
         call:replace "oldstr4" "newstr4"
         call set "line=echo.%%line%%"
         for /f "delims=" %%* in ('"echo."%%line%%""') do (%%~*)
     ) ELSE (echo.))
exit /b 0

:replace
>nul 2>&1 (
call set line |Findstr.exe /RIC:"\<%~1\>" && call set line |Findstr.exe /RIVC:"\<@%~1\>" && call set "line=%%line:%~1=%~2%%")
exit /b 0


I'm not sure if that's the correct way to do it, I'm still learning about this. Also, am I supposed to enable delayed expansion before the code I just added in? I tried playing around with where to put it to no avail. For example I tried:

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION
set "?=!?:/=!"
set "?=!?:-=!"
set "?=!?:\=!"
set "?=!?: =!"
set "?=!?:,=!"
set "?=!?:.=!"
set "?=!?::=!"
set "?=!?:(=!"
set "?=!?:)=!"
set "?=!?:&=!"
set "?=!?:|=!"
set "?=!?:>=!"
set "?=!?:<=!"
set "?=!?:^%=!"
set "?=!?:?=!"
set "?=!?:^!=!"
set "?=!?:;=!"
set "?=!?:"=!"
set "?=!?:""="!^"
ENDLOCAL
I also tried starting with enable delayed expansion.

Thanks again

aGerman
Expert
Posts: 4740
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Finding and replacing in file with special char?

#9 Post by aGerman » 24 May 2012 13:56

Try something like that:
(untested)

Code: Select all

@echo off
>"newfile.txt" (
  for /f "delims=" %%i in ('type "oldfile.txt"^|findstr /n "^"') do (
    set "ln=%%i"
    setlocal EnableDelayedExpansion
      set "ln=!ln:*:=!"
      if not defined ln (
        echo(
      ) else (
        set "ln=!ln: newstr1 = newstr1 !"
        set "ln=!ln: newstr2 = newstr2 !"
        set "ln=!ln: newstr3 = newstr3 !"
        echo(!ln: newstr4 = newstr4 !
      )
    endlocal
  )
)

Perhaps it's not exactly what you require since I simplified the word boundary by a prepended and an appended space (which is not applicable for words on the beginning or the end of a line).

Regards
aGerman

Edit: found 2 mistakes

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Finding and replacing in file with special char?

#10 Post by foxidrive » 24 May 2012 15:29

asparagus wrote:Unfortunately that's not an option; is there any way to do it with just the batch file?


How come? VBS/WSH is standard in every system since XP and an optional Microsoft update in Win9x and NT.

Post Reply