Extract legacy script to infinitely more complicated processes.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dfefs
Posts: 3
Joined: 15 Jul 2016 11:16

Extract legacy script to infinitely more complicated processes.

#1 Post by dfefs » 15 Jul 2016 12:46

I have a baseline script on multiple systems. The basic concept is the same
but the number of entries is different per system. I have to try to get this working
in a bit less than a week, or I'll have to spend hours manually going through a
process that shouldn't be manual.
Sample baseline:

Code: Select all

cd c:\rundir
del oldlog.txt
pq.exe -n 192.168.20.12 -e 1234 -p TCP > oldlog.txt
pq.exe -n 192.168.20.13 -e 1234 -p TCP >> oldlog.txt
pq.exe -n 192.168.20.14 -e 1234,1235,1236 -p TCP >> oldlog.txt
pq.exe -n 192.168.20.15 -e 1234,1236 -p TCP >> oldlog.txt

---------------------------------
So basically, -n is an ip address. -e may have several 3 or 4 digit port numbers, at least one, but as many as three.
This script gets the baseline data. I need to modify it so each of the different server lines (with the IPs)
sends their output to a different file (ideally %IP_ADDRESS%.txt). AND here comes the fun part. Due to the
scripts being different on each system, and the fact that there are 3 or 4 other scripts that end up calling
this one, I need to pull the relevant lines (pq.exe) from the baseline script , extract the host IP and the ports,
and use that to send data for comparison to output files like %sSERVER_IP%.txt
I've spent quite a few hours communing with Prof Google on this, but it's been 20 years since I worked with
dos/cmd batch files. I'm also confirming my theory that code/technical data found via professor google is only as
good as the keyboard kommando typing it, and many people think they're experts but they aren't, which is why I'm here.
Since I havent had any luck setting variables in a for loops because I dont yet fully understand delayed expansion,
I'm trying a label/function call in my script. Here's an example of how I'd LIKE it to work:
-----------------------

Code: Select all

@echo off
setlocal
REM  pull the strings from the legacy bat to get IPs and ports
for /f "USEBACKQ tokens=* delims= " %%i in (`findstr pq.exe getport.bat`) do (
call :runit
)

:runit
REM Set server variable to the IP address
set server=%3
REM set ports variable to all of the space-delimitted entries after -e (but it's not working).
set ports=%5
pq.exe -n %server% -e %ports% -p TCP >> %server%.txt

REM AFTER THIS PART I GO TO PARSE THE OUTPUT

-------------------------------------------------

Another method would be to set the variables in the for loops, which I cant get to work.

Code: Select all

for /f "USEBACKQ tokens=* delims= " %%i in (`findstr pq.exe getport.bat`) do (
for /f "USEBACKQ tokens=3 delims= " %%s in (`echo %%i`) do set server=%s
for /f "USEBACKQ tokens=5 delims= " %%p in (`echo %%i`) do set ports=%p
call :runit
)

:runit
pq.exe -n %server% -e %ports% -p TCP  >> %server%.txt

--------------------------
On the occasions where I have been able to get variables set, the port variable only takes the first
entry, stopping at the comma delimitter. Despite the many hours I've spent so far, I haven't figured
out the delayed expansion. I've been on a completely different OS for quite a while, so I've forgotten
what I used to know about batch programming.
OK so. Assuming anyone has made it this far, I'm hoping I can get some input on how to set the variables
needed so I can get runit to work.
Any help would be greatly appreciated (NOTE: I am not liable for physical injuries due to excessive
laughter caused by my crapplication coding.)

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Extract legacy script to infinitely more complicated processes.

#2 Post by penpen » 15 Jul 2016 16:05

I'm not sure if i've understood well enough, but i hope this may help you:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion

for /f "tokens=3,5 delims= " %%i in ('findstr "pq.exe" "getport.bat"') do (
   >> "%%~i.txt" pq.exe -n %%~i -e %%~j -p TCP 
)

endlocal
goto :eof


penpen

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

Re: Extract legacy script to infinitely more complicated processes.

#3 Post by foxidrive » 16 Jul 2016 05:04

dfefs wrote:I have a baseline script on multiple systems. The basic concept is the same
but the number of entries is different per system. I have to try to get this working
in a bit less than a week, or I'll have to spend hours manually going through a
process that shouldn't be manual.
Sample baseline:

Code: Select all

cd c:\rundir
del oldlog.txt
pq.exe -n 192.168.20.12 -e 1234 -p TCP > oldlog.txt
pq.exe -n 192.168.20.13 -e 1234 -p TCP >> oldlog.txt
pq.exe -n 192.168.20.14 -e 1234,1235,1236 -p TCP >> oldlog.txt
pq.exe -n 192.168.20.15 -e 1234,1236 -p TCP >> oldlog.txt


You've provided a fair bit of info but I didn't see a clear statement of what you want to get from that text you have shown above.

I noted you're looking for pq.exe from a batch script and filtering that with findstr and by that stage there no description about the actual task

IE: "I get this text. and I want it to look like this afterwards."

It helps to clarify your aim in this way before speaking about a number of items that haven't yet got a purpose in your description.

dfefs
Posts: 3
Joined: 15 Jul 2016 11:16

Re: Extract legacy script to infinitely more complicated processes.

#4 Post by dfefs » 16 Jul 2016 11:15

foxidrive wrote:
dfefs wrote:I have a baseline script on multiple systems. The basic concept is the same
but the number of entries is different per system. I have to try to get this working
in a bit less than a week, or I'll have to spend hours manually going through a
process that shouldn't be manual.
Sample baseline:

Code: Select all

cd c:\rundir
del oldlog.txt
pq.exe -n 192.168.20.12 -e 1234 -p TCP > oldlog.txt
pq.exe -n 192.168.20.13 -e 1234 -p TCP >> oldlog.txt
pq.exe -n 192.168.20.14 -e 1234,1235,1236 -p TCP >> oldlog.txt
pq.exe -n 192.168.20.15 -e 1234,1236 -p TCP >> oldlog.txt


You've provided a fair bit of info but I didn't see a clear statement of what you want to get from that text you have shown above.

I noted you're looking for pq.exe from a batch script and filtering that with findstr and by that stage there no description about the actual task

IE: "I get this text. and I want it to look like this afterwards."

It helps to clarify your aim in this way before speaking about a number of items that haven't yet got a purpose in your description.

Sorry, in this instance, my main question was HOW TO DO what I stated I need. The output, while critical later on, really is irrelevant to the question at hand, which was how to extract the data from the run file and how to pass that data to the binary. The redirection in the sample code should have indicated no manipulation of the text is needed at that point. Thanks for your response.

dfefs
Posts: 3
Joined: 15 Jul 2016 11:16

Re: Extract legacy script to infinitely more complicated processes.

#5 Post by dfefs » 16 Jul 2016 11:25

penpen wrote:I'm not sure if i've understood well enough, but i hope this may help you:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion

for /f "tokens=3,5 delims= " %%i in ('findstr "pq.exe" "getport.bat"') do (
   >> "%%~i.txt" pq.exe -n %%~i -e %%~j -p TCP 
)

endlocal
goto :eof


penpen

THANK YOU VERY MUCH! I'm very close to having a finished product. I spent far too many hours bashing my head against the dos/cmd wall until today. I'm not completely finished with what I needed, but you've gotten me much much further than I was yesterday. Seriously, I'm very grateful. I'd name my firstborn child after you, unfortunately he probably wouldn't like it if I changed his name now! :lol:

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

Re: Extract legacy script to infinitely more complicated processes.

#6 Post by foxidrive » 17 Jul 2016 11:15

dfefs wrote:Sorry, in this instance, my main question was HOW TO DO what I stated I need. The output, while critical later on, really is irrelevant to the question at hand, which was how to extract the data from the run file and how to pass that data to the binary. The redirection in the sample code should have indicated no manipulation of the text is needed at that point. Thanks for your response.


The output helps to understand your question, and that is essentially what I was trying to say. My terms were a little unclear too.

I had no idea what you wanted and soon gave up reading as the term baseline has no meaning in batch scripting.

These terms only have meaning to you.

Sample baseline:
baseline script
baseline data

Post Reply