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?