Need quick help with Dos Batch file...

Namuna

Platinum Member
Jun 20, 2000
2,435
1
0
Sorry to put this here in Off-Topic, but I need an answer ASAP.

Any help or pointers in the right direction are much appreicated! I've tried google but for things like this if you don't search with just the right wording you'll come up nada...

Anyway here's what I'm trying to do...

I have a regular text file, let's say it's called version.txt and within this text file it has a single line of text like v5.9.14

Now, I'm trying to create a batch file that will look inside the version.txt file, extract that line of text (being v5.9.14) and finally assigning that info to a variable.

I've been trying to figure out how I can do this with the set command, but haven't had any luck.

Again, thanks for any and all help!
 

sygyzy

Lifer
Oct 21, 2000
14,001
4
76
You want to do that with just a plain batchfile? It might be possible if you got third party dos tools, but I doubt you can with a regular batch file.
 

djheater

Lifer
Mar 19, 2001
14,637
2
0
Here are some examples of FOR statements:
:MAILREG
FOR /F "delims= , tokens=1-3" %%i IN ('REG QUERY HKLM\software\bfusa\bool\outgoing_mail_flg') do Set MAILEVAL=%%k

IF %MAILEVAL%==0 (set KPMAILFLG=NO) Else (SET KPMAILFLG=YES)

:SKIPMAILREG


REM ************* Capture date for ini ******************
:LOGDATETIME
set DATETIMEFILE=%~n1
for /F %%x in ('dtstmp.exe -t -f %DATETIMEFILE%.') do (set DATETIMEFILE=%%x)
set DATE=%DATE:.=%
for /F %%x in ('dtstmp.exe -t -f.') do (set DATETIME=%%x)
set DATETIME=%DATETIME:.=%


REM **************Capture readable Date\Time******************

FOR /f "tokens=*" %%a in ('DATE/T') do SET sdate=%%a
FOR /f "tokens=*" %%j in ('TIME/T') do SET stime=%%j

What's you're looking for is something like:

FOR /f %%j in (c:\directory\version.txt) do (SET ver=%%j)

I'm guessing you'll have to enclose the version in quotes in your text file?

Try that and let me know if it works.
 

djheater

Lifer
Mar 19, 2001
14,637
2
0
actually I just tried it and no quotes are necessary.

If you're testing from the command line remember that only a single percent is necessary, in a batch file, you need two.
 

Jzero

Lifer
Oct 10, 1999
18,834
1
0
I didn't want to just give him the answer, but for what it's worth, FOR /F takes a command as opposed to a file name. You would have to use FOR /F %%i in ('type filename.txt') DO ...

You won't need quotes in the target file.

Edit: Actually, looking at the "man page" it seems file names do work with /F. Weird, I could never get them to work.
 

djheater

Lifer
Mar 19, 2001
14,637
2
0
Originally posted by: Jzero
I didn't want to just give him the answer, but for what it's worth, FOR /F takes a command as opposed to a file name. You would have to use FOR /F %%i in ('type filename.txt') DO ...

You won't need quotes in the target file.

The For /f will take a filename\location as a set, without the necessary tics for commands.

Check Tim Hills Shell Scripting book.
 

Jzero

Lifer
Oct 10, 1999
18,834
1
0
Originally posted by: djheater
Originally posted by: Jzero
I didn't want to just give him the answer, but for what it's worth, FOR /F takes a command as opposed to a file name. You would have to use FOR /F %%i in ('type filename.txt') DO ...

You won't need quotes in the target file.

The For /f will take a filename\location as a set, without the necessary tics for commands.

Check Tim Hills Shell Scripting book.

It has never worked for me giving it just a filename. Dunno why...
 

Jzero

Lifer
Oct 10, 1999
18,834
1
0
Originally posted by: djheater
The command shell is a fickle mistress :(

True, but if you tickle it the right way, you can really write some crazy stuff.

These days, we've got batch files scanning the network for all sorts of stuff and generating output in XML for easy analysis through a web interface.
 

Namuna

Platinum Member
Jun 20, 2000
2,435
1
0
Got it to work! thanks all.

I used:
for /F "tokens=1-3 delims=." %%A in ('type c:\blah.txt') do set version=%%A.%%B.%%C

Worked like a charm.
 

Jzero

Lifer
Oct 10, 1999
18,834
1
0
Originally posted by: Namuna
Got it to work! thanks all.

I used:
for /F "tokens=1-3 delims=." %%A in ('type c:\blah.txt') do set version=%%A.%%B.%%C

Worked like a charm.

Try it with just the filename. Just for kicks.

Also, you should be able to use "tokens=2 delims=v" and just set version to %%A...
 

djheater

Lifer
Mar 19, 2001
14,637
2
0
It should work with just the filename and if the version is the ONLY entry in the file no delims should be necessary. Space, and Tab are the default delimiters so as long as there are none of those it will take everything in the file as the first token.
 

djheater

Lifer
Mar 19, 2001
14,637
2
0
The caveat is you can't use a for/f to set a variable from the result of an action precipitated by a nested IF.
 

Jzero

Lifer
Oct 10, 1999
18,834
1
0
Originally posted by: djheater
Why am I so gung ho about this?

It must be the coffee.

Link to example

That will work, too. I stripped the "v" but I don't know why I would assume you would want to strip the "v."

I think batch scripting is fun, mainly because everyone thinks it isn't powerful enough to be useful. :D
 

djheater

Lifer
Mar 19, 2001
14,637
2
0
Originally posted by: Jzero
Originally posted by: djheater
Why am I so gung ho about this?

It must be the coffee.

Link to example

That will work, too. I stripped the "v" but I don't know why I would assume you would want to strip the "v."

I think batch scripting is fun, mainly because everyone thinks it isn't powerful enough to be useful. :D

What's fun about it, is having these ridiculously complicated scripts with multiple contingencies. I recently took a WSH class and was like "OMG, this is why Shell scripting sucks ass." It's quick and dirty but pretty emasculated when it comes down to it.

<--- needs to start converting the 17 billion bat's to vbs at some point this year :(
 

Namuna

Platinum Member
Jun 20, 2000
2,435
1
0
In Linux (bash shell):
version=`cat blah.txt`

Ever since I switched over to Linux, I never wanted to look back on the DOS/Batch...But unfortunately Microsoft Borg still holds strong in the corporate world so every so often I gotta retool the dusty batch tools and automate some things.

thanks again all.