I'm pretty new to programming, and had a question about DOS. How do you pipe output from a command to a variable?
Printable View
I'm pretty new to programming, and had a question about DOS. How do you pipe output from a command to a variable?
what command in particular are you talking about?
Nothing really in particualr, I was just experimenting with it.
If your talking about bat type programming output can be piped to a file and used from there. The output of most programs is to cluttered to use as a var.
U:\>ping -n 1 yahoo.com
Pinging yahoo.com [66.218.71.198] with 32 bytes of data:
Reply from 66.218.71.198: bytes=32 time=70ms TTL=243
Ping statistics for 66.218.71.198:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 70ms, Maximum = 70ms, Average = 70ms
So you have to trim it.
C:\>ping -n 1 yahoo.com |find "Reply" results.txt
In results.txt you’ll have:
Reply from 66.218.71.198: bytes=32 time=70ms TTL=243
Still more info then you can deal with so:
For /F “tokens=3” %X in (results.txt) do set MyVar = %X
(when used in a bat file %X needs to be written %%X)
On entering “set” at a prompt you’ll see MyVar = 66.218.71.198:
You have to deal with the info that’s output depending on the type of output you have and the type of input you need. That’s why I asked for what proggie.
Thank you! Just one more question. Le'ts just say you wanted to pipe DIR into a var.
dir|set dir1
could you use something like that for the syntax?
If you want to save all the output from a command use a redirect instead of a pipe. Like "dir > listing.txt" this will redirect the output to the file named listing.txt
-Maestr0
Pipes are connections between two programs or two commands or a program and a command. Pipes allow the output of one program to be redirected as the input for a second program.
The DOS symbol for a pipe is the vertical bar ( | ). To redirect the output of one command to another type the name of the first command, followed by a vertical bar, followed by the name of the second command.
for example
dir | find "03"
dir | more
nice info :D 10x i didnt know that
No or at least i dont know of a way to load an array into memory using just DOS commands. you could using c/cpp, perl etc, but you would need the program itself to call the array from the program. Basic can load a listing like that into a var but again it would need to be called from inside the program. i suppose you could parse each line into a env. var of its own and call them all but that seems like more trouble than its worth
For /F “tokens=3” %X in (results.txt) do set MyVar = %X
what does the "/f" and "tokens=3" exactly do?
the /F indicates the vars will be taken from a file which we name in (results.txt).
'for' by default uses a space to delimit items in each line. tokens=3 means we'll be using the third item. (it actually places them in an array).
if we had said "tokens=5" %X
(Reply from 66.218.71.198: bytes=32 time=70ms TTL=243 )
%X would equal "time=70ms"
enter "for /?" at a command prompt for an in depth help file. if youd rather ask, ask away! BTW this does not apply to 9x systems only nt based. the for command in 9x is very weak.
NT based only? Oh...I was wondering why the "/f" and "tokens" weren't working...lol
Is there any other way for you to do this? I apologize if I am being bothersome.
Sorry, i should have asked first.
i dont have any 9x boxen hooked up here so help me out.
Now assuming you have a 9x box, type this into a batch file
(i named mine bt1.bat)
@echo %1
@echo %2
@echo %3
@echo %4
@echo %5
@echo %6
now enter this line at the prompt. making sure your in the same dir as the bat file:
bt1.bat Reply from 66.218.71.198: bytes=32 time=70ms TTL=243
let me know if you get this:
Reply
from
66.218.71.198:
bytes
32
time
if you do we'll work on getting what you want