Computing.Net > Forums > Programming > Batch script variables

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch script variables

Reply to Message Icon

Name: Goblinss
Date: December 26, 2008 at 04:23:23 Pacific
OS: Windows Vista
CPU/Ram: P4 2.4Ghz
Product: Intel / ?
Comment:

Hi there,
i have small script that reads lines of a txt file:
@echo off
echo start
for /f "tokens=1,2* delims= " %%a in (text.txt) do (
echo 1ST--%%a
echo 2ND--%%b
echo 3RD--%%c
set var=%%b
echo new var=%var%
)

how do i save those %%a,%%b,.. into new vars? the above code returns me the first word in the last line of the text file.. please help. thanks in advance!



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: December 26, 2008 at 08:24:11 Pacific
Reply:

"above code returns me the first word in the last line"

Cal me crazy, but it returns the SECOND word in the last line.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: reno
Date: December 26, 2008 at 08:46:49 Pacific
Reply:

lol, try this. use exclamation mark inside a for loop rather than percentage.
set var=%%b
echo new var=!var!

add this line in the top of code:
SetLocal EnableDelayedExpansion

yes, the above code does return the SECOND word of LAST Line after the loop.


0

Response Number 3
Name: cybpsych
Date: December 28, 2008 at 07:59:06 Pacific
Reply:

i have another variant of question to this post...

If each line in text.txt is long, passing this line into command is not pretty.

So, I'm thinking of passing each line into a variable, then use this variable in a command line. I can use this variable into filename as well.

if i modified the code to this:

@echo off
SetLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (text.txt) do (
set var=%%a
echo !var!
)

but, how do i make each var different for each line parsed from text.txt?

e.g. var1=[line1], var2=[line2], var3=[line3]

assumption is that the qty of lines in text.txt changes, that's why i put token=*.


0

Response Number 4
Name: reno
Date: December 28, 2008 at 09:36:29 Pacific
Reply:

::filenum.bat
@echo off
SetLocal EnableDelayedExpansion

find /v /n "" text.txt > ~tmp
set /a count=0

::store line to array of variables
for /f "skip=2 tokens=1* delims=[]" %%a in (~tmp) do (set var%%a=%%b & set/a count+=1)

::display the variable to screen
for /l %%i in (1,1,!count!) do echo var%%i=!var%%i!
del ~tmp > nul


0

Response Number 5
Name: cybpsych
Date: December 28, 2008 at 16:56:04 Pacific
Reply:

that's great reno ..

one last question: can the variable number starts from var0 instead of var1?


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: December 29, 2008 at 04:55:56 Pacific
Reply:

More simply:

========================================
@echo off & setLocal EnableDelayedExpansion

set /a N=0

for /f "tokens=* delims= " %%a in (myfile) do (
set v!N!=%%a
set /a N+=1
)

set v


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 7
Name: reno
Date: December 29, 2008 at 05:49:02 Pacific
Reply:

that's "set v" is a nice trick. everyday i read this forum, i am learning new stuff. i am glad i found this forum.

the difference with using find and direct parsing is in "empty line".


0

Response Number 8
Name: Mechanix2Go
Date: December 29, 2008 at 06:24:21 Pacific
Reply:

Hi reno,

I learn something almost every day. You would do well to study the code that IVO writes.

Happy New Year gang!


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 9
Name: cybpsych
Date: December 29, 2008 at 06:46:40 Pacific
Reply:

thanks M2,

ur script is quite simple and elegant ;)

a good complement to reno's as well.

as reno mentioned, it doesn't parse the myfile nicely if there's empty line anywhere.

thanks guys! and happy new year to all!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch script variables

batch script to parse filenames www.computing.net/answers/programming/batch-script-to-parse-filenames/15286.html

Batch Scripting Help!!! www.computing.net/answers/programming/batch-scripting-help/2980.html

Batch script for file copy www.computing.net/answers/programming/batch-script-for-file-copy/14874.html