Recover a string result from executable and set a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply

Recover string from terminal set variable

Recover string terminal
0
No votes
Recover string from result executable
0
No votes
 
Total votes: 0

Message
Author
jgamezu
Posts: 4
Joined: 03 Sep 2009 16:58
Location: Mexico
Contact:

Recover a string result from executable and set a variable

#1 Post by jgamezu » 03 Sep 2009 17:25

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

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 03 Sep 2009 18:30

Is this what you want?

Code: Select all

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

jgamezu
Posts: 4
Joined: 03 Sep 2009 16:58
Location: Mexico
Contact:

Yeah its works......

#3 Post by jgamezu » 04 Sep 2009 10:59

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

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Yeah its works......

#4 Post by avery_larry » 04 Sep 2009 14:57

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.

jgamezu
Posts: 4
Joined: 03 Sep 2009 16:58
Location: Mexico
Contact:

Ohhh, Yes it works

#5 Post by jgamezu » 07 Sep 2009 10:14

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

Post Reply