Solved batch substitute a string with special charachters

March 22, 2013 at 05:03:24
Specs: Windows XP, 1 Gb
I worked out this batch in order to substitute some strings in a text file;

I think to understand that the general structure of the file is ok but for some reasons it's not performing a correct subsitution of the strings;

I suspect that's because of some special characters inside the strings like : / " and blank spaces; is it necessary to escape somehow those characters? and how?

thank you for the feedback....

'start batch --------------------------------------------------------------------------------------------------

@Echo Off
SetLocal EnableDelayedExpansion

set old=Path: "c:/xxx/
set new=Path: "

for %%B in (*.txt) do (echo %%~nB

for /F "tokens=* delims=" %%A in (%%~nB.txt) Do (
set TxtLine=%%A
set TxtLine=!TxtLine:%old%=%new%!
echo !TxtLine! >> %%~nB_rel_path.txt)
)

'end batch --------------------------------------------------------------------------------------------------------


See More: batch substitute a string with special charachters

Report •


✔ Best Answer
March 23, 2013 at 14:41:42
batch tends to get a serious case of indigestion when trying to process certain characters. with delayedexpansion, any exclams in the text will disappear on output.
Same goes with percents, i believe. Also, some system-critical characters can wreak havoc without careful attention. That said, I would recommend vbscript or powershell or a stream-editor to handle the text. Since I don't know powershell, here's a vbscript:
'==== begin vbscript
set fso=createobject("scripting.filesystemobject")
x=fso.opentextfile("test",1).readall
'-- not sure if these are needed - probably not
x=replace(x,"C:\aaa\","")
x=replace(x,"C:\AAA\","")
fso.opentextfile("test",2,true).write replace(x,"c:\aaa\","")
'=== end
note that the file will be overwritten, so be sure to have backup.


#1
March 22, 2013 at 17:13:38
Hello. Worked fine on my xp. Bare-bones minimal test data however. If there's a particular sequence that giving you problems, you might want to post it.

Report •

#2
March 23, 2013 at 01:08:06
thanks nbrane for testing my batch!

unfortunately the file is really too big to be posted right now;
I might send an amended version later just in case I can't get to a viable solution (in fact I need some time to work out a posted example with some critical issues) ;

anyway, the sequence to be substituted is something like:
Path: "c:/aaa/bbb/ccc ccc.xxx"
i.e. an absolute path to a file name (also with some spaces in it, unfortunately!)
with something like:
Path: "bbb/ccc ccc.xxx""
i.e. a relative path

the very strange thing is that when I did the substitution by hand with notepad everything went fine!: any clue for this?

the final substituted file must be very much of the same structure as the original one (execpt for the changed strings) because it's supposed to be a listing for the execution of a third party programme;

I suspect that the batch is introducing some changes in the structure of the file (i guess....)

just in case you have any hints, thank you again!

bye

max


Report •

#3
March 23, 2013 at 05:07:56
When I was doing lazy testing yesterday(testing it on a couple of random text files in a directory. I too noticed a change in the structure of some of my files. For a simple file it seemed to work fine on xp pro sp3.

:: mike


Report •

Related Solutions

#4
March 23, 2013 at 14:41:42
✔ Best Answer
batch tends to get a serious case of indigestion when trying to process certain characters. with delayedexpansion, any exclams in the text will disappear on output.
Same goes with percents, i believe. Also, some system-critical characters can wreak havoc without careful attention. That said, I would recommend vbscript or powershell or a stream-editor to handle the text. Since I don't know powershell, here's a vbscript:
'==== begin vbscript
set fso=createobject("scripting.filesystemobject")
x=fso.opentextfile("test",1).readall
'-- not sure if these are needed - probably not
x=replace(x,"C:\aaa\","")
x=replace(x,"C:\AAA\","")
fso.opentextfile("test",2,true).write replace(x,"c:\aaa\","")
'=== end
note that the file will be overwritten, so be sure to have backup.

Report •

#5
March 25, 2013 at 01:23:18
thanks nbrane
it works, but just one more question: is it the "search and replace" case sensitive?

Report •

#6
March 25, 2013 at 03:28:17

Report •

#7
March 25, 2013 at 12:08:42
Yes, unfort. they didn't build in a "ignore-case" switch for "replace". Reg exp has that option however:
'==== begin vbscript
set fso=createobject("scripting.filesystemobject")
set regexp=new regexp
regexp.pattern="(c:\\aaa\\)"
regexp.ignorecase=true
regexp.global=true
x=fso.opentextfile("test1",1).readall
fso.opentextfile("test1",2,true).write regexp.replace(x,"")
'==== end

Report •

#8
March 25, 2013 at 13:32:49
You don't need to use RegExp.
x = replace(x, "C:\aaa\", "", 1, -1, 1)

How To Ask Questions The Smart Way


Report •

#9
March 25, 2013 at 13:52:13
hmmm. I looked at that, but didn't understand how "binary" could fix the problem, and so didn't try it. What does "binary" entail? I never did understand that option...

Report •

#10
March 25, 2013 at 15:04:26
You have it backwards. 1 = Text Compare, 0 = Binary Compare (the default).

Binary compare will cause VBScript to compare the data an an opaque blob of data. Text compare will compare the data as text data, so VBScript knows it can ignore case. There might be other, culture specific things text compare does, but I don't know what those would be.

It's not documented well, but there it is.

How To Ask Questions The Smart Way


Report •

#11
March 25, 2013 at 18:23:11
@Razor, Thanks! that's what I like about this forum - learning new things.

Report •

#12
March 26, 2013 at 00:38:09
dear all

thank you for your great feedback to my small problem (I'm learning a lot!)
just let me summarize, this should be the final product (sorry but I'm not confident at all with vbs):

'==== begin vbscript

set fso=createobject("scripting.filesystemobject")

x=fso.opentextfile("test.txt",1).readall

x = replace(x, "c:/radmap/", "", 1, -1, 1)

fso.opentextfile("test_new.txt",2,true).write replace(x,"","")

'==== end

I introduced a slight variation in order to save the output in a new file;

but I have another question now: is it possible to generalise the code so that it's processing all the files present in the same directory of the vbs file?

thanks


Report •

#13
March 27, 2013 at 12:47:02
Sure it is. (Untested and sloppy 'cause I'm lazy):

Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder(".").Files
  If UCase(fso.GetExtensionName(f)) <> "NEW" And _
    UCase(fso.GetExtensionName(f)) <> "VBS" Then _
    DoReplacement fso, f
Next 'f
WScript.Quit

Sub DoReplacement(fso, sPath)
  fso.OpenTextFile(sPath & ".new", 2, True).Write _
    Replace(fso.OpenTextFile(sPath).ReadAll, "c:/radmap/", "", 1, -1, 1)
End Sub

How To Ask Questions The Smart Way


Report •

#14
March 27, 2013 at 17:43:47
Your final script works, but it has an extra replace:
set fso=createobject("scripting.filesystemobject")
x=fso.opentextfile("test.txt",1).readall
x = replace(x, "c:/radmap/", "", 1, -1, 1)
'--the following replace is not needed, since it's done in the previous
'---fso.opentextfile("test_new.txt",2,true).write replace(x,"","")
fso.opentextfile("test_new.txt",2,true).write x

and Razor's script also works, but might need this tweak:

DoReplacement fso, f

To:

DoReplacement fso, fso.getbasename(f)

to keep from making "double-dot" files (like "text.txt.new")


Report •

Ask Question