How use variables in a Batch File

Microsoft Windows xp professional w/serv...
September 7, 2010 at 08:16:34
Specs: Windows XP, 1gb
I have a problem, when I execute a function in BATCH file, then I have a result, I can't use this result, I'd like stocker it in a variable par example. However when I execute my Batch file, I see the command line of my command and just in the next line I see the result I want manipulate.

Ex :

Batch File :

snmpget -b HCLOS-DOWN trapPort.0

Result in the CMD shell :

C:\>snmpget -b HCLOS-DOWN trapPort.0

Is the result 160, I want manipulate, but I'm not able. I tried also with the SET command like :

set var=snmpget -b HCLOS-DOWN trapPort.0

But, If I want to show the variable "var" with the % operator, then the result is all the String expression, then: var=snmpget -b HCLOS-DOWN trapPort.0.

Note that snmpget -b HCLOS-DOWN trapPort.0 , is a command interne of my job, the idea is to be able to MANIPULATE THE RESULT of whatever function or command I have.


See More: How use variables in a Batch File

Report •

September 7, 2010 at 15:09:24
You could try sending the result to a file and recalling from there:

snmpget -b HCLOS-DOWN trapPort.0 > %temp%\filename.ext
set /p var=<%temp%\filename.ext
del %temp%\filename.ext

Variable var should now contain the output of snmpget

Report •

September 7, 2010 at 18:48:20
For /f is another viable option.

for /f "delims=" %%a in (' snmpget -b HCLOS-DOWN trapPort.0 ') do set var=%%a

Report •

September 14, 2010 at 07:57:35
I wrote your proposition but It does not work,

for /f "delims=" %%a in (' snmpget -b HCLOS-DOWN trapPort.0 ') do set var=%%a

I just change var to var1, it's "delims=" a internal command DOS?, maybe the error is there.

Thanks a lot !!!


In fact is "delims=" the error, I tried without that and that works very well ....

Thanks a lot !!!

Report •
Related Solutions

Ask Question