Computing.Net > Forums > Windows XP > Batch file help needed...

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Batch file help needed...

Reply to Message Icon

Original Message
Name: dcyoung
Date: June 6, 2007 at 08:56:03 Pacific
Subject: Batch file help needed...
OS: XP, W2K
CPU/Ram: Core Duo, 1GB
Model/Manufacturer: Lenovo, T60
Comment:

Hi there

This forum has been a real help, I had to create a really complicated batch file that would search for and add specific lines of text within the firewall.ini of Blackice on W2K and XP machines, then make a bunch of registry mods on XP machines only, and then prompt the user to either accept or undo those changes...whew...

The problem I'm having is that there is a line of text I need to add to the firewall.ini file under TCP settings, that also pre-exists under a UDP heading. The dilemna is that when I ask the batch to check for the new line before adding it, and exit the called batch if it finds it, it will always find the one under the UDP settings and it exits without adding the line to the file. My work around is crude, I just have it add the line anyway...problem is every time this batch is run, it will add another instance of that line into the file...

For Example I want to add the text "ACCEPT, 1024 - 65535" below a heading to make it look like this...

[MANUAL OUTBOUND TCP HIGH ACCEPT]
ACCEPT, 1024 - 65535

But...This line also exists elsewhere in the ini file like this

[MANUAL UDP HIGH ACCEPT]
ACCEPT, 1024 - 65535

The current code I have for this portion is this...

:Addtxt5
For /F "eol= tokens=* delims= " %%A in (%1) Do (
Echo %%A>> %1.tmp
If "%%A"=="ACCEPT, 1024 - 65535" (
Goto Nupd5)
If "%%A"=="[MANUAL OUTBOUND TCP high ACCEPT]" (
Echo ACCEPT, 1024 - 65535>> %1.tmp))
Move %1.tmp %1
Exit /b

You can see that it looks first so that the line isn't added a second time, but since the line exists under the "[MANUAL UDP high ACCEPT]" heading the 'Goto' cmd sends it on to the :Nupd5 section and exits.

Is there any way within the 'IF' statement I've provided to have it look for two lines of text that follow each other, for example, have it look for these two lines in this order

[MANUAL OUTBOUND TCP high ACCEPT]
ACCEPT, 1024 - 65535

And then Goto Nupd5 should it find them, or continue on with the script should it not find them.

Sorry for the long post...I just wanted it to be clear.

Thanks


Report Offensive Message For Removal


Response Number 1
Name: IVO
Date: June 6, 2007 at 09:54:05 Pacific
Reply: (edit)

Your script is wrong, replace what you posted with

:Addtxt5
For /F "tokens=* delims=" %%A in (%1) Do (
Echo %%A>> %1.tmp
Echo %%A | Find "ACCEPT, 1024 - 65535" > Nul
If not ErrorLevel 1 Goto :Nupd5
If "%%A"=="[MANUAL OUTBOUND TCP HIGH ACCEPT]" (
Echo ACCEPT, 1024 - 65535>> %1.tmp))
Move %1.tmp %1
Exit /b

And try (the error is the eol= parameter).


Report Offensive Follow Up For Removal

Response Number 2
Name: dcyoung
Date: June 6, 2007 at 10:08:06 Pacific
Reply: (edit)

The rest of the batch file works perfect with the eol=parameter. There are a series of :Addtxt# sections with that parameter in the batch that all get called from main script. Without that eol= parameter the batch does not copy everything from the original ini file to memory. There are a number of instruction lines that all begin with ; that need to remain the in the finished ini...I had the script written as you have provided originally, and it ignored the lines preceded by a ;...after I added that eol= parameter...it worked perfect. Regardless, the problem I am facing is that I do not know how to create an 'IF' statement that searches for two lines in sequence...removing the eol= doesn't solve that problem.

Thanks anyways though.


Report Offensive Follow Up For Removal

Response Number 3
Name: IVO
Date: June 6, 2007 at 10:15:05 Pacific
Reply: (edit)

Hmm...restore your For /F line in what I suggested leaving unchanged the remaining code and it should work.


Report Offensive Follow Up For Removal

Response Number 4
Name: dcyoung
Date: June 6, 2007 at 10:59:41 Pacific
Reply: (edit)

Nope...doesn't work, it's still just looking for that one single line ... "ACCEPT, 1024 - 65535" ... which it will always find under other headings in the ini file.

I need it to look for the two lines in this specific sequence

[MANUAL OUTBOUND TCP high ACCEPT]
ACCEPT, 1024 - 65535

and then 'Goto' the appropriate line when that argument is true...

Sorry to be a pain.

Thanks


Report Offensive Follow Up For Removal

Response Number 5
Name: dcyoung
Date: June 6, 2007 at 11:29:49 Pacific
Reply: (edit)

I guess I need to make this clearer..

The line of text I want to add to the ini file is:

ACCEPT, 1024 - 65535

I want that line of text to go under a the following Heading in the ini file like this:

[MANUAL OUTBOUND TCP high ACCEPT]
ACCEPT, 1024 - 65535


The problem is that the line of text I want to add already exists under the following heading like this:

[MANUAL UDP HIGH ACCEPT]
ACCEPT, 1024 - 65535

I want the batch file to look for the line of text within a sequence of two lines like this:

[MANUAL OUTBOUND TCP high ACCEPT]
ACCEPT, 1024 - 65535

If these two lines already exist together, then I want it to 'Goto' another portion of the batch file. If they do not exist together , I want the batch file to continue.

I hope that clarifies my problem...


Report Offensive Follow Up For Removal


Response Number 6
Name: dcyoung
Date: June 6, 2007 at 12:44:58 Pacific
Reply: (edit)

Alternatively, I could just delete all instances of the line of text that pre-exist in the ini file, and then just create and additional :Addtxt# sections to add them back in one by one...a little crude, but effective.

Anybody know how to delete a specific line of text from a file?


Report Offensive Follow Up For Removal

Response Number 7
Name: IVO
Date: June 6, 2007 at 12:59:35 Pacific
Reply: (edit)

Addtxt5
SetLocal EnableDelayedExpansion
Set RowFL=FALSE
For /F "eol= tokens=* delims= " %%A in (%1) Do (
Echo %%A>> %1.tmp
If "%%A"=="ACCEPT, 1024 - 65535" (
If "!RowFL!"=="TRUE" (GoTo :Nupd5)
) else (
If "!RowFL!"=="TRUE" (
Echo ACCEPT, 1024 - 65535>> %1.tmp
Set RowFL=FALSE))
If "%%A"=="[MANUAL OUTBOUND TCP high ACCEPT]" (
Set RowFL=TRUE)
)
EndLocal
Move %1.tmp %1
Exit /b


Report Offensive Follow Up For Removal

Response Number 8
Name: IVO
Date: June 6, 2007 at 13:11:44 Pacific
Reply: (edit)

I hope the code in the post #7 is correct as I just corrected it twice, so if it doesn't work re-read the post to see if some line is changed.


Report Offensive Follow Up For Removal

Response Number 9
Name: dcyoung
Date: June 6, 2007 at 14:25:19 Pacific
Reply: (edit)

No...sorry it still didn't work, but my work around is to delete the other pre-existing line with

Find /v /i "ACCEPT, 1024 - 65535" <%1> %1.tmp
Move %1.tmp %1

and then just re-add that line back in with the others...not slick, but when it all happens at the speed of light, who really cares...

Thanks


Report Offensive Follow Up For Removal

Response Number 10
Name: IVO
Date: June 6, 2007 at 14:46:21 Pacific
Reply: (edit)

Sorry I was not able to solve your trouble; when I catch a problem I do not leave it untill the solution is found (if a solution exists). In this case I had no enough time and quiet to examine the code.

I hope I didn't waste your time. Good work and ever glad to support (maybe better next time) if you need help.


Report Offensive Follow Up For Removal

Response Number 11
Name: Razor2.3
Date: June 8, 2007 at 02:46:19 Pacific
Reply: (edit)

Man that's hard to read.


set count=-1
for /f %%a in ('find "ACCEPT, 1024 - 65535" %1') do set /a count+=1

If /i %count% LSS 2 @Echo ACCEPT, 1024 - 65535>> %1


Report Offensive Follow Up For Removal

Response Number 12
Name: dcyoung
Date: June 8, 2007 at 09:12:23 Pacific
Reply: (edit)

None of it worked in the end. All was glorious on my XP box, but as soon as I ran it on another W2k or XP box it started ignoring lines randomly. In the end all I did was tell it to replace the existing ini file with one of my choosing and created a exe file that contained it and the new ini.

Thanks


Report Offensive Follow Up For Removal






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Windows XP Forum Home








Do you have a Desktop Computer anymore?

No
Yes, but only at work
Yes, but its rarely used
Yes, and its a workhorse


View Results

Poll Finishes Today.
Discuss in The Lounge
Poll History




Data Recovery Software