Go Back   Tech Support Forum > Microsoft Support > Windows XP Support

set variable based on output of seach string in batch

This is a discussion on set variable based on output of seach string in batch within the Windows XP Support forums, part of the Tech Support Forum category. Hello, I am trying to find a certain patter in a file which has the following format key1 ; value1


Closed Thread
 
Thread Tools Search this Thread
Old 05-25-2005, 12:03 PM   #1
Registered Member
 
Join Date: May 2005
Posts: 22
OS: XP



Hello,

I am trying to find a certain patter in a file which has the following format

key1 ; value1
key2 ; value2


and so on.

I intend to use the findstr command as follows

findstr /L key1 input_file

returns the following output

input_file:key1 ; value1

I need to be able to assign the value of "value1" into a variable "VAR1" that i can use in the script later. Any ideas as to how to do this. Or is there a better/easier way to get the same result.
As always your response/feedback is greatly appreciated. Thanks.

__________________
jerardfjay is offline  
Old 05-26-2005, 05:14 AM   #2
Registered Member
 
Join Date: May 2005
Posts: 22
OS: XP


Quote:
Originally Posted by

returns the following output

[B
input_file:key1 ; value1[/B]

I need to be able to assign the value of "value1" into a variable "VAR1" that i can use in the script later. Any ideas as to how to do this. Or is there a better/easier way to get the same result.
One way to get to the value1 is as follows

for /F "tokens=2,3,* delims=;" %i IN ('findstr /L "key1" input_file') do @echo %i

this is a special format of the "for" command, which is splitting the output of the "findstr" command working on the *.ctr file name and returning only one of the tokens presented to the FOR command.

My next step is to be able to assign the result of the "for" command to a variable that I can use later in the script. Any ideas. Thanks.

__________________
jerardfjay is offline  
Old 05-26-2005, 05:30 AM   #3
Registered Member
 
Join Date: May 2005
Posts: 22
OS: XP



Quote:
Originally Posted by jerardfjay
My next step is to be able to assign the result of the "for" command to a variable that I can use later in the script. Any ideas. Thanks.
The following command works from the command line interpreter window, however does not work when executed from a batch file.

for /F "tokens=2,3,* delims=;" %i IN ('findstr /L "key1" input_file') do call set var1=%i

I get the following error message when I execute this command from a batch.

i was unexpected at this time.

Any ideas. as to why this would work on a command line and not a batch file. Thanks.
__________________
jerardfjay is offline  
Old 05-26-2005, 07:33 AM   #4
Registered Member
 
Join Date: May 2005
Posts: 22
OS: XP


Quote:
Originally Posted by jerardfjay
Any ideas. as to why this would work on a command line and not a batch file. Thanks.
The reason as to why the command did not work in batch is becase of the requirement of variables to be preceded by doube % signs in a "for" command when used in batch. Here is a version that will work.

@echo off
for /F "tokens=2,3,* delims=;" %%i IN ('findstr /L "key1" input_file') do call set var1=%%i
echo %var1%


Thanks. Jerardfjay
__________________
jerardfjay is offline  
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is on
Smilies are on
[IMG] code is on
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Post a Question


» Site Navigation
 > FAQ
  > 10.0.0.2


All times are GMT -7. The time now is 07:18 PM.


Copyright 2001 - 2014, Tech Support Forum

Windows 7 - Windows XP - Windows Vista - Trojan Removal - Spyware Removal - Virus Removal - Networking - Security - Top Web Hosts