Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a DOS Batch pro!

Status
Not open for further replies.

riluve

Technical User
Mar 11, 2005
78
US
So I can search a file with FINDSTR - but now I need to store it to a variable or parse it in-line to get a single value in the result. For example:

FINDSTR "KEYWORD" *.TXT

result:

KEYWORD = "VALUE"

What I need:

SET ENV = VALUE
-or-
1% = VALUE

Anyone know how to do this? Is it possible with DOS (NT) Batch files and commands or will I need a 3rd party program?

Thnx
 
Do you need to do this in a batch file? Can you not use a simple VBS?

Hope this Helps.

Neil J Cotton
njc Information Systems
Systems Consultant
 
Well, I don't know anything about VBS - is it available from the command line? I suppose I can use it to se environment varibles, but would I need to compile anything? Im guessing its just a script language?

It would be best if it would run on Win 2000 or wIN xp at the least.
 
VBS will run in 2000 and XP, and can be run from the command line, even though it runs in the Windows Script Host. Best nipping over to the VB Forum and asking, Mark will help you out (MarkDMac)

forum329

Hope this Helps.

Neil J Cotton
njc Information Systems
Systems Consultant
 
Do you know if it needs compiling and thus a compiler? Or is it interpreted so that I can just write what I need to do from the command line?
 
riluve,
no compiler - vbs is just really beefed up dos. like ncotton said check out the vbscript forum. all you need to do is write the commands to a .txt file rename to .vbs - just like you would do to .bat and then can run from the command line or put in the task scheduler. do a search in the forum and look at the faqs - that should get you going.
regards,
longhair
 
VBS is an interpreted scripting language (Visual Basic SCRIPT) and runs in the WSH VM. (Windows Script Host), and is plain text.

Hope this Helps.

Neil J Cotton
njc Information Systems
Systems Consultant
 
I'm not sure I understand what you're trying to do. Do you already KNOW the "keyword", is it something a user needs to input? And where you have "VALUE", what is value? It looks like your trying to set "Keyword" (which you'll already need to know) and "Value" to the same thing. Is this correct?
 
SET Environment_Value = ""
SET KEYWORD = "some word to be found"
XSET Environment_Value = FINDSTR "%KEYWORD%" *.TXT


 
bcastner:

logically, that looks good, but XSET is not a DOS/NT command. Was this meant to be a VB script?

mpnut:

logically, what bcaster is trying to do is what I need to do, I just need to correct syntax for a batch file. The command FINDSTR takes "Keyword" as input, searches *.txt files and displays the full line of text for every occurance it finds.

And example of the full line of text would include the value I am really looking for based on the keyword. So the output of FINDSTR would be the following line of text:

KEYWORD = "VALUE" REM Other text I don't care about

So, I searched for keyword, I have this entire line of text, but I only want "VALUE". Once I parse value from the line of text, I can save it to some variable like so:

SET ENV_VAR = VALUE

If I could just assign the output from FINDSTR directly to an environment variable (or any variable), I could then parse it for VALUE, but all I can do right now is get the string (full line of text) .I can't save it (except to a file) or manipulate it in any way (I have tried using piping commands and redirection but have had no success).

 
So you are setting "Value" equal to "Keyword". I guess what I don't understand is once you have "Keyword" set as a variable, why would you need "Value" set as a variable if it is the same as "Keyword"? Or am I misunderstanding?
 
mpnut,

VALUE can be a partial string; e.g. "nut" will match on "mpnut"


 
mpnut:

Think of it like this, I am parsing/searching some other file that I do not have control over. However I know whenever there is a line in that file with KEYWORD in it, that line will also have the value I am looking for.

so the line of text might look like this:

The Keyword for today is value.

If I use FINDSTR I can find the item I know (keyword) and thusly discover the item I need to know (value).

Bcaster:
All that does is set the env-var to equal the command I wanted to execute. The result exactly as written:

Environment_Value = FINDSTR "%KEYWORD%" *.TXT

What I need:

Environment_Value = value

.
 
the use of the word value is confusing here,,,,could we call the keyword = "piggies"

and yes, BCasters repsone is VBS.
 
SET Environment_Value=""
SET KEYWORD="some word to be found"

for /f "tokens=*" %%a in (
'findstr /c:"%KEYWORD%" *.TXT'
) do set Environment_Value=%%a

 
Also, can you give a simple example of what the file will look like? My interpretation is a file like
Code:
my dog ran in a circle
my piggies ran in a triangle
my cat ran in a circle
. Are you trying set the variable to something like "triangle" when the word "piggies" is found?
 
Haha - sweet bcastner - you are the mastner - well let me test it a little more and i'll get back to you.

So the files I am parsing/searching areformatted like this:

Example1:

text I don't care about
Build="Build.bat"
BuildAll="BuildA.bat"
more text I don't care about
-------------------------

Example2:

text I don't care about
Build="B.bat"
BuildAll="BA.bat"
more text I don't care about
-------------------------
Example3:

text I don't care about
Build="Build.cmd"
BuildAll="BuildA.cmd"
more text I don't care about
-------------------------

Keyword = Build or BuildA
Value = Build.bat,BuildA.bat,b.bat,ba.bat,Build.cmd,BuildA.cmd

:)

thnx all
 
Well Frack - now how do i keep my environment variables alive for each session? To be truly global?

Such problems.
 
Using the add-on tool Setx.exe

It is not part of the standard Windows XP setup but a command-line tool called setx.exe is included in the Windows XP Service Pack 2 Support Tools. This tool extends the set command so that permanent changes in the environment variables can be made. Download:
Syntax discussion:
Note this: Because SetX writes variables to the master environment in the registry. Edits will only take effect when a new command window is opened - they do not affect the current command session.

SET Environment_Value=""
SET KEYWORD="some word to be found"

CMD /Q /K for /f "tokens=*" %%a in (
'findstr /c:"%KEYWORD%" *.TXT'
) do setx Environment_Value=%%a
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top