Page 1 of 1

Recover a string result from executable and set a variable

Posted: 03 Sep 2009 17:25
by jgamezu
some executable, returns a string. Any idea how to set this string in to variable?. or recover string from terminal window??
:shock:

I execute

Net time \\{ip for any computer}
Return:
Current time at \\{ip for any computer} is {date and time}
:cry:

and set Date and time a variable this no promblem

Posted: 03 Sep 2009 18:30
by avery_larry
Is this what you want?

Code: Select all

for /f %%a in ('Net time \\{ip for any computer}') do set your_var=%%a

Yeah its works......

Posted: 04 Sep 2009 10:59
by jgamezu
Yeah Men, I luv u!!!!!!! :lol:


i got my data with:

example
-----------------
FOR /F "tokens=6*" %%A IN ('Net time \\{ip any computer}') DO set my_var=%%A %%B

Echo %my_var%

-----------------
today this resolved my problem :wink:

but if the execute had a result with many lines you can see only the last line.
if the last line had a less tokens than penultimate you can see the last words or tokens of penultimate lines.

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

for other poeple to wants resolve that

FOR /F

Syntax
FOR /F ["options"] %%parameter IN (filenameset) DO command

FOR /F ["options"] %%parameter IN ("Text string to process") DO command

Key
options:
delims=xxx The delimiter character(s) (default = a space)

skip=n A number of lines to skip at the beginning of the file.
(default = 0)

eol=; Character at the start of each line to indicate a comment
The default is a semicolon ;

tokens=n Specifies which numbered items to read from each line
(default = 1)

usebackq Specify `back quotes`:
- Use double quotes to quote long file names in filenameset.
- Use single quotes for 'Text string to process'
(useful if the text string contains double quotes)

Filenameset : A set of one or more files. Wildcards may be used.
If (filenameset) is a period character (.) then FOR will
loop through every file in the folder.
command : The command to carry out, including any
command-line parameters.

%%parameter : A replaceable parameter:
in a batch file use %%G (on the command line %G)


From: http://ss64.com/nt/for_f.html

Re: Yeah its works......

Posted: 04 Sep 2009 14:57
by avery_larry
jgamezu wrote:Yeah Men, I luv u!!!!!!! :lol:


i got my data with:

example
-----------------
FOR /F "tokens=6*" %%A IN ('Net time \\{ip any computer}') DO set my_var=%%A %%B

Echo %my_var%

-----------------
today this resolved my problem :wink:

but if the execute had a result with many lines you can see only the last line.
if the last line had a less tokens than penultimate you can see the last words or tokens of penultimate lines.
You can use a combination of "skip" and/or a goto statement to eliminate extra lines:

Code: Select all

for /f "tokens=6* skip=2" %%a in ('somecommand.cmd') do (
  set something=%%a
)

The above will skip the first 2 lines of output and run the command on the rest of the lines of output.

Code: Select all

for /f "tokens=6*" %%a in ('someothercommand.cmd') do (
   if %%a=="what we're looking for" (
      set myvar=%%a
      goto :continue
   )
)
:continue
echo We broke out of the for loop
The above will test for what you want and then effectively exit the for loop using a goto command. Note that the GOTO trick does NOT work fully with a "for /l" loop.

Ohhh, Yes it works

Posted: 07 Sep 2009 10:14
by jgamezu
avery larry, I did my homework 8)

thanks men with the help


for other people I have this code

Code: Select all

@echo off
SET count=0
FOR /f "tokens=*" %%G IN ('{your command}') DO (call :do_sums "%%G")
Echo %line_str%
pause
GOTO :eof

:do_sums
set /a count+=1
Rem ****compare with number of line you want***
IF !%count%!==!1! SET line_str=%1
GOTO :eof