Replace text through batch file

Dell / OPTIPLEX 330
October 14, 2009 at 11:16:32
Specs: Microsoft Windows XP Professional, 1.595 GHz / 1012 MB
Hi,

I want to replace the following string through a bacth file :

&& !plantCode.equals("1050")

Below is the code that was suggested in one of the forums:

@echo off > newfile & setLocal enableDELAYedexpansion

set /p old=old string ?
set /p new=new string ?

for /f "tokens=* delims= " %%a in (myfile.txt) do (
set str=%%a
echo %%str
set str=!str:%old%=%new%!
>> myfile1.txt echo !str!
)
pause

However when I try to replace it with :

&& !plantCode.equals("1050") && !plantCode.equals("1051")

I get the below error message. Could anyone please advise. I think it is happening because of the && signs. have tried but I am not able to escape it.


old string ?&& !plantCode.equals("1050")
new string ?&& !plantCode.equals("1050")&& !plantCode.equals("1050")
%str
'plantCode.equals' is not recognized as an internal or external command,
operable program or batch file.
%str


See More: Replace text through batch file

Report •


#1
October 14, 2009 at 15:33:32
This probably is a little closer to what you are looking for.


@echo off
setlocal enabledelayedexpansion

set /p old=old string ?
set /p new=new string ?

For /f "tokens=*" %%a in (Test.txt) do (
if /i "%%a" EQU "%Old%" (set output=!new!) else set output=%%a
echo !output! >> NewFile.txt
)
pause


Report •

#2
October 14, 2009 at 15:50:43
Hi

Thanks a lot for your reply.

I tried this and it did not work.

old string= && !plantCode.equals("1050")
new string= && !plantCode.equals("1050") && !plantCode.equals("1051")

It created a new file with the olde string. Could you please advise.

What I am trying to do is append the new string to the old one.

Also could you please let me know why it was not working with the old code and how you are making it work by using EQU. I guess it means equal.

I tried to put in echo after the value for old is set and got the same error. However after removing the value it works but the files created contains the old string.


Report •

#3
October 14, 2009 at 16:21:18
Okay, so read through the text file, If a line of text equals what you inputted as the "Old" variable append the "New" variable to the end of it else just re-write the lines?

Your origanal post should have worked but I think you were right, the double & was screwing something up.


@echo off
setlocal enabledelayedexpansion

set /p old=old string ?
set /p new=new string ?

For /f "tokens=*" %%a in (Test.txt) do (
if /i "%%a" EQU "%Old%" (set output=!old! !new!) else set output=%%a
echo !output! >> NewFile.txt
)
pause



Report •

Related Solutions

#4
October 14, 2009 at 16:31:04
So even with the new code it is just rewriting the old lines right?

Is there no way to have this done?


Report •

#5
October 14, 2009 at 16:48:43
If you run the batch in response #3 it should do what you were saying. If you look at what is going on.

The for /f command goes through each line in the text file and saves one line at a time as a variable %%a

Then For each line saved as %%a we pass it through an if statement saying. If the line stored in the variable %%a is equal to what you entered for the old text do set a variable called Output to equal the variable stored as Old and the variable stored as new.
If the string stored as %%a isn't equal to what you entered we use the else command to just keep the line the same by setting output to the contents of %%a.
Then we send the output variable to a new text file since batch files don't like you writing to them while you are reading from them.
Then once we get to the end of the for command for the current line, it goes back and grabs a new line from the text file.

I hope that sheds some light on what is going on.
Also as far as the Compare-op of EQU goes, you are right it does mean equal, and is used to compare strings, you could use == instead of EQU, personal choice.


Report •

#6
October 14, 2009 at 16:54:12
Thanks that is more clear now. Appreciate the detailed explanation. Will play with this for a while and see.

Thanks once again.


Report •

#7
October 14, 2009 at 16:57:31
Reading response #2 again, are you editing the batch file to contain the strings that you want to write?

The Set command with the /P is used to prompt for user input.

So the way to use this batch you would run it as is, and it should prompt you for the input.
The part that says "Old String ?" and "New String ?" after the equal is just what is desplayed in the prompt asking for the input.

if you want to make the batch run without prompted for the input so that you can set what is in there you would take the /p out and make it look something like the following:

Use response #1 but change the set commands to the following.

set old= && !plantCode.equals("1050")
set new= && !plantCode.equals("1050") && !plantCode.equals("1051")


Report •

#8
October 14, 2009 at 20:01:00
I was just playing around with this and it doesn't seem like DOS likes your strings being set as variables. Which leaves you this choice, run the Batch file how it is written in response #1, and when it prompts you for Old enter the string you want it to search for, and then when it prompts for New enter what you want. Or I was thinking you could do it in VBScript but I can't seem to get it to except your Strings as Variables either. So the following VBS will do exactly what the batch does (Prompt for the strings) Except it wont create a new file it will use the existing text file to write in. The only thing you have to change is the first line where it is the location of your text file. And save it just like a batch file except that the file extension needs to be .VBS instead of .BAT
Sorry thats the best I can come up with, right now. But I will still try to figure out how to declare your strings as a variable without having to have it filled in through a prompt.


strFileName = "c:\text.txt"

strCheckForString = UCase(InputBox("Please enter string to search for"))
strNewLine=InputBox("Please Enter the new line")
Set objFS = CreateObject("Scripting.FileSystemObject")
StrContents = Split(objFS.OpenTextFile(strFileName).ReadAll, vbNewLine)
Set objTS = objFS.OpenTextFile(strFileName, 2)
For Each strLine In StrContents
If (Left(UCase(LTrim(strLine)),Len(strCheckForString)) = strCheckForString) Then
objTS.WriteLine strNewLine
else
objTS.WriteLine strLine
End If
Next
Wscript.quit



Report •

#9
October 15, 2009 at 11:29:34
Hi,

Thank you for the response. I tried the VB script and am facing a issue.

I am fine with the fact that it is trying to replace the string in the same file. Actually that is what I want to do.

When I use the VBS code above in Response #8 it is not appending to the file.

The content of the file is :

601
hjhgjghjh
hjgj 500 && !plantCode.equals("1050")
hjhgjghjh
hjgj


However I tested a little more and if I change the content to :

601
hjhgjghjh
&& !plantCode.equals("1050")
hjhgjghjh
hjgj


It is appending the new string i.e && !plantCode.equals("1051"). I dont understand why it is not working in the file where there is hjgj 500 && !plantCode.equals("1050") in the line.

What I am trying to do is take the input from the user i.e 1051 and add it to the string in the middle of the line with the && !plantCode.equals

This is actually a java code file and I wanted to know a way through a batch file to do it. However if it is possible through VBS then thats great too.


Report •

#10
October 15, 2009 at 12:47:19
Okay so here we go, The following VBScript will look for the pattern that limits it down to && !plantCode.equals..1050.. The periods are like wild cards in the search string. VBScript didn't like having the ("")'s so I had to replace them with the periods, also that is why I couldn't hard code it so that it would append the next plant code so it prompts you for it. So again you will have to Change that file name to point at your file. and then it will prompt you for what you want to append, enter what you want to append to the end of the line
&& !plantCode.equals("1051").

Let me know if I'm getting closer


strFileName="C:\Test.txt"

strNewLine = InputBox("What would you like to append?")
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "&& !plantCode.equals..1050.."
Set objFS = CreateObject("Scripting.FileSystemObject")
StrContents = Split(objFS.OpenTextFile(strFileName).ReadAll, vbNewLine)
Set objTS = objFS.OpenTextFile(strFileName, 2)

For Each strLine In StrContents
If objRegEx.Execute(strLine).Count > 0 Then
objTS.WriteLine strLine & strNewLine
Else
objTS.WriteLine strLine
End If
Next


Report •

#11
October 15, 2009 at 13:38:41
Hi,

This seems to be working great. However just a small issue.

Actual line is if(!plantCode.equals("1160") && !plantCode.equals("1050")){

After using the script you gave in response #10 the line becomes if(!plantCode.equals("1160") && !plantCode.equals("1050")){&& !plantCode.equals("1051")

The only issues is it should append it before the { bracket at the end of the line.

Is this possible? Also if i understand your previous post (response # 10) correctly then we cant just take the number i.e 1051 have the rest && !plantCode.equals("variable")hard coded and the input as a variable?

Thanks a lot for your help on this. Am extremely new to VBS and was hoping to do this through dos rather than JAVA etc. However if this is possible through VBS would like to try it out.

I guess its very easy through Java? Any suggestions?


Report •

#12
October 15, 2009 at 15:20:29
So we add another line to the script using the len command which will tell you the length of characters in a string and then we subtract one from that and set it as "x" , then we use the left command to diplay the "x" amount of the characters from the left, which since we set it minus one. it is dropping the last character off of the string, then we write the new line that was prompted and then we hard coded it to add the "{" back in, which we could have set as a variable but for simplicity we just hard coded it. I hope that makes sense to you, so that you kind of understand what is going on here.


strFileName="C:\Test.txt"

strNewLine = InputBox("What would you like to append?")
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "&& !plantCode.equals..1050.."
Set objFS = CreateObject("Scripting.FileSystemObject")
StrContents = Split(objFS.OpenTextFile(strFileName).ReadAll, vbNewLine)
Set objTS = objFS.OpenTextFile(strFileName, 2)

For Each strLine In StrContents
If objRegEx.Execute(strLine).Count > 0 Then
x=(Len(strLine))-1
objTS.WriteLine (Left(strLine,x)) & strNewLine & "{"
Else
objTS.WriteLine strLine
End If
Next

So your last question above, you would like the user to be prompted for a Plant code like below?


strFileName="C:\Test.txt"

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "&& !plantCode.equals.."&InputBox("Which Plant code?")&".."
strNewLine = InputBox("What would you like to append?")
Set objFS = CreateObject("Scripting.FileSystemObject")
StrContents = Split(objFS.OpenTextFile(strFileName).ReadAll, vbNewLine)
Set objTS = objFS.OpenTextFile(strFileName, 2)

For Each strLine In StrContents
If objRegEx.Execute(strLine).Count > 0 Then
x=(Len(strLine))-1
objTS.WriteLine (Left(strLine,x)) & strNewLine & "{"
Else
objTS.WriteLine strLine
End If
Next


Report •

#13
October 15, 2009 at 15:54:09
Hi,

The issue with the { bracket is solved. It works great. However I think there is a slight confusion.

The old string && !plantCode.equals("1050") can/should be hard coded as that would never change and the new string will always be appended to it.

The new string && !plantCode.equals("anything") should be hard coded except the value of 1050 or anything which we would take as a input from the user.

If thats possible it would be the best thing. I think you have already accomplished that in the most recent code in a way. I tried to tweak it a little. Below is my tweaked version:

strFileName="C:\Test.txt"

strNewLine = "&& !plantCode.equals.."&InputBox("Which Plant code?")&".."

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "&& !plantCode.equals..1050.."
Set objFS = CreateObject("Scripting.FileSystemObject")
StrContents = Split(objFS.OpenTextFile(strFileName).ReadAll, vbNewLine)
Set objTS = objFS.OpenTextFile(strFileName, 2)

For Each strLine In StrContents
If objRegEx.Execute(strLine).Count > 0 Then
x=(Len(strLine))-1
objTS.WriteLine (Left(strLine,x)) & strNewLine & "{"
Else
objTS.WriteLine strLine
End If
Next

However I am wondering that for strNewLine how can i have the ("") hardcoded. If its not possible then we can have the user input them.


Report •

#14
October 15, 2009 at 15:57:42
Tried to run this and got a error at line 3. Guess I have not set the value for strNewLine correctly.

Report •

#15
October 15, 2009 at 17:24:56
So you are running into the error becuase I don't know how to set a variable to contain a " without it being entered through a prompt. I'm hoping somebody stumbles upon this and lets us know how to do such a task. The only way I can think of right off the top of my head is, the user needs to enter the double quotes when they enter the Plant code. So you pretty much had it besides the " giving you the error. And then I would suggest adding some more instructions to the inputbox to help instruct the user doing the inputing, like the following.


strFileName="C:\Test.txt"

strNewLine = "&& !plantCode.equals("_
&InputBox("Enter Plant Code"_
&vbCRLF _
&vbCRLF&"Enlosed in double Quotes ''"_
&vbCRLF _
&vbCRLF&"Like ''1052''","Enter Search criteria")&")"
if strNewLine = "&& !plantCode.equals()" then Wscript.quit
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "&& !plantCode.equals..1050.."
Set objFS = CreateObject("Scripting.FileSystemObject")
StrContents = Split(objFS.OpenTextFile(strFileName).ReadAll, vbNewLine)
Set objTS = objFS.OpenTextFile(strFileName, 2)

For Each strLine In StrContents
If objRegEx.Execute(strLine).Count > 0 Then
x=(Len(strLine))-1
objTS.WriteLine (Left(strLine,x)) & strNewLine & "{"
Else
objTS.WriteLine strLine
End If
Next


Report •

#16
October 16, 2009 at 06:35:21
Hi gtaion,

I checked and it is working like a charm. The "" should not be a big problem.

Thanks a lot for all your help. Appreciate it.


Report •

#17
October 16, 2009 at 07:00:18
Yeah hey no problem, always like a challenge. Hey if along your adventures in scripting (If you take up the hobby) If you find a way to get the "" to work let me know. It has got to be something relatively simple, but for the life of me I can't seem to figure out what it is.

Report •

#18
October 16, 2009 at 07:08:56
I am very found of scripting and am learning. Am average at batch files and shell scripts. Am planning to learn Java.

Will surely let you know if I figure the "" issue.

Thanks a lot once again. You were a great help.


Report •


Ask Question