Batch symbol is not passing corectly

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Batch symbol is not passing corectly

#1 Post by BoQsc » 30 Jun 2014 04:30

Hey guys, i need help with script that i was searched for a long time:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set INTEXTFILE=file.txt
set OUTTEXTFILE=new.txt
set SEARCHTEXT=Target = TESTCore/TEST.exe
set REPLACETEXT=Target =TEST.exe
set OUTPUTLINE=

for /f "tokens=1,* delims=¶" %%A in ( '"findstr /n ^^ %INTEXTFILE%"') do (
   SET string=%%A
   for /f "delims=: tokens=1,*" %%a in ("!string!") do set "string=%%b"
   if  "!string!" == "" (
       echo.>>%OUTTEXTFILE%
   ) else (
      SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
      echo !modified! >> %OUTTEXTFILE%
  )
)

::del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%


As everyone see - it suppose to find and replace string in txt file.
The problem is that i get incorrect replacement when i start to use = symbol.

So instead of this output.

Code: Select all

Target = TEST.exe

I get something like this:

Code: Select all

 TESTCore/TEST.exe=Target = TEST.exe= TESTCore/TEST.exe 


I have no idea how to solve it, i tried many hours to do that.
Hope someone can help me, i will attach files that i use if anyone need it to make al this thng more clear.

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

Re: Batch symbol is not passing corectly

#2 Post by foxidrive » 30 Jun 2014 05:14

Give an example of the text you have and what it needs to be changed to.

The makeup of the characters is important if it is not basic Latin characters.

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Batch symbol is not passing corectly

#3 Post by BoQsc » 30 Jun 2014 05:30

foxidrive wrote:Give an example of the text you have and what it needs to be changed to.

The makeup of the characters is important if it is not basic Latin characters.


Here you go: https://www.dropbox.com/s/syl6up08fsla811/Replacetest.zip
There is everyhing that is needed.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Batch symbol is not passing corectly

#4 Post by Yury » 30 Jun 2014 06:15

Code: Select all

@echo off
setlocal enabledelayedexpansion
set INTEXTFILE=file.txt
set OUTTEXTFILE=new.txt
set SEARCHTEXT=Target = TESTCore/TEST.exe
set REPLACETEXT=Target =TEST.exe
set OUTPUTLINE=

for /f "tokens=1,* delims=¶" %%A in ( '"findstr /n ^^ %INTEXTFILE%"') do (
   set string=%%A
   for /f "delims=: tokens=1,*" %%a in ("!string!") do set "string=%%b"
   if  "!string!" == "" (
       echo.>>%OUTTEXTFILE%
   ) else (
       call set modified=!string:%%SEARCHTEXT%%=%%REPLACETEXT%%!
       echo.!modified!>>%OUTTEXTFILE%
   )
)

::move %OUTTEXTFILE% %INTEXTFILE%>nul

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Batch symbol is not passing corectly

#5 Post by BoQsc » 30 Jun 2014 06:39

Yury wrote:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set INTEXTFILE=file.txt
set OUTTEXTFILE=new.txt
set SEARCHTEXT=Target = TESTCore/TEST.exe
set REPLACETEXT=Target =TEST.exe
set OUTPUTLINE=

for /f "tokens=1,* delims=¶" %%A in ( '"findstr /n ^^ %INTEXTFILE%"') do (
   set string=%%A
   for /f "delims=: tokens=1,*" %%a in ("!string!") do set "string=%%b"
   if  "!string!" == "" (
       echo.>>%OUTTEXTFILE%
   ) else (
       call set modified=!string:%%SEARCHTEXT%%=%%REPLACETEXT%%!
       echo.!modified!>>%OUTTEXTFILE%
   )
)

::move %OUTTEXTFILE% %INTEXTFILE%>nul


Hmm, i tried that. It looks like still not working. I cleary don't get it.
The output is the same as original file, strange.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Batch symbol is not passing corectly

#6 Post by Compo » 30 Jun 2014 11:29

Try utilising another scripting language to bypass the awkward symbol!

ReplaceTest.cmd

Code: Select all

<!-- : Begin batch script
@Echo Off & SetLocal
Set _PRE=file.txt
Set _PRESTR=Target = TESTCore/TEST.exe
Set _POST=new.txt
Set _POSTSTR=Target = TEST.exe
CScript //NoLogo "%~f0?.wsf" "%_PRE%" "%_PRESTR%" "%_POST%" "%_POSTSTR%"
Exit/b

----- Begin wsf script --->
<job><script language="VBScript">
Set objFS = CreateObject("Scripting.FileSystemObject")
strInFile = WScript.Arguments.Item(0)
strOld = WScript.Arguments.Item(1)
strOutFile = WScript.Arguments.Item(2)
strNew = WScript.Arguments.Item(3)
Set objInFile = objFS.OpenTextFile(strInFile)
Set objOutFile = objFS.CreateTextFile(strOutFile,True)
Do Until objInFile.AtEndOfStream
   strLine = objInFile.ReadLine
   if Instr(strLine,strOld)> 0 Then
      strLine = Replace(strLine,strOld,strNew)
   End If
   objOutFile.Write(strLine)
Loop
objOutFile.Close
objInFile.Close
'objFS.DeleteFile(strInFile)
'objFS.MoveFile strOutFile,strInFile
</script></job>
If you're happy with the result of new.txt remove the first character from both line 28 and 29 and your original file.txt will be replaced by the new.txt on future runs.

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Batch symbol is not passing corectly

#7 Post by BoQsc » 01 Jul 2014 03:56

Compo wrote:Try utilising another scripting language to bypass the awkward symbol!
...

The thing is, yours version of script - it didn't save "NewLine" but the spaces are ok.

Instead of

Code: Select all

asd
Target = TESTCore/TEST.exe
asd test das

What i get:

Code: Select all

asdTarget = TEST.exeasd test das


I'm still more interested in solution of first one script i posted (it have everythng i need, instead that "equal mark" that didn't pass correctly). However if there is any other way to do this with batch correctly - would be nice and helpful if anyone share it.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Batch symbol is not passing corectly

#8 Post by Compo » 01 Jul 2014 08:53

Well that was deliberate, because you posted a zip file which contained everything I needed, the file.txt file did not contain those three lines!
The version below will do the replacement without writing a new.txt first.

Code: Select all

<!-- : Begin batch script
@Echo Off & SetLocal
Set _PRE=file.txt
Set _PRESTR=Target = TESTCore/TEST.exe
Set _POSTSTR=Target = TEST.exe
CScript //NoLogo "%~f0?.wsf" "%_PRE%" "%_PRESTR%" "%_POSTSTR%"
Exit/b

----- Begin wsf script --->
<job><script language="VBScript">
Set oArgs = WScript.Arguments
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not oFSO.FileExists(oArgs(0)) Then
   WScript.Echo "Specified file does not exist."
Else
   Set oFile = oFSO.OpenTextFile(oArgs(0), 1)
   strText = oFile.ReadAll
   oFile.Close
   strText = Replace(strText, oArgs(1), oArgs(2), 1, -1, 1)
   Set oFile = oFSO.OpenTextFile(oArgs(0), 2)
   oFile.WriteLine strText
   oFile.Close
End If
</script></job>
The solution I have presented here is not generic and is a standalone .cmd script which should in most circumstances meet your requirements.
The problem is that you are looking for a specific answer for a generic question. The example you are pretending to be your real world question really only needs to replace "TESTCore/" with "". That itself is simple enough to do in batch scripts but you and I, (and I'm guessing by the lack of responses most people here), know that is not what you want. Presenting a specific problem will often get specific answers but generic ones will often be given a wide berth.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Batch symbol is not passing corectly

#9 Post by Yury » 01 Jul 2014 18:53

Code: Select all

@echo off
setlocal enabledelayedexpansion
set INTEXTFILE=file.txt
set OUTTEXTFILE=new.txt
set SEARCHTEXT=Target = TESTCore/TEST.exe
set REPLACETEXT=Target = TEST.exe

set SEARCHTEXT=%SEARCHTEXT: =$%
set REPLACETEXT=%REPLACETEXT: =$%
(
for /f "delims=¶" %%A in ( '"findstr /n ^^ %INTEXTFILE%"') do (
 for /f "tokens=1* delims=:" %%a in ("%%A") do set string=%%b
 if  "!string!"=="" (
  echo.
  ) else (
  mshta "vbscript:CreateObject("Scripting.FileSystemObject").GetStandardStream(1).Write(replace(replace("!string: =$!","%SEARCHTEXT%","%REPLACETEXT%"),"$",chr(32)))&close()"
  echo.
  )
 )
)>%OUTTEXTFILE%

::move %OUTTEXTFILE% %INTEXTFILE%>nul

Post Reply