Page 1 of 1

can Variables value altered mltiple times in 1 .bat file?

Posted: 07 May 2012 02:05
by prash11
hi
i m having one .txt file that contains the different name of the files.
so using for loop i m displaying the different file names contained in that file.
FOR /F "tokens=* delims= " %%G IN (file_names.txt) DO (
set k= %%G
echo %k%
)

the .txt contains 7 file names.
but every time only one file name is getting displayed for 7 times.
because k is getting set only once.
i want all 7 names to be displayed.

how can i do this any suggestions....????

Re: can Variables value altered mltiple times in 1 .bat file

Posted: 07 May 2012 02:22
by !k

Code: Select all

FOR /F "delims=" %%G IN (file_names.txt) DO (
set "k=%%G"
setlocal enabledelayedexpansion
echo "!k!"
endlocal
)

Re: can Variables value altered mltiple times in 1 .bat file

Posted: 07 May 2012 05:23
by prash11
thnks !k
this worked for me...!
cheers....!

Re: can Variables value altered mltiple times in 1 .bat file

Posted: 07 May 2012 05:49
by Squashman
prash11 wrote:hi
i m having one .txt file that contains the different name of the files.
so using for loop i m displaying the different file names contained in that file.
FOR /F "tokens=* delims= " %%G IN (file_names.txt) DO (
set k= %%G
echo %k%
)

the .txt contains 7 file names.
but every time only one file name is getting displayed for 7 times.
because k is getting set only once.
i want all 7 names to be displayed.

how can i do this any suggestions....????

Then why not just echo %%G instead of assigning it to a variable. Then you don't need delayed expansion and everything would work just fine.

Re: can Variables value altered mltiple times in 1 .bat file

Posted: 07 May 2012 23:42
by prash11
hi Squashman
thnks for suggestion but i need to use that value at almost 30 places.
so i cant use %%G thirty times because it makes my code so unreadable and also makes it difficult to understand.
that's why i need variable.

Re: can Variables value altered mltiple times in 1 .bat file

Posted: 08 May 2012 10:02
by Fawers
prash11 wrote:hi Squashman
thnks for suggestion but i need to use that value at almost 30 places.
so i cant use %%G thirty times because it makes my code so unreadable and also makes it difficult to understand.
that's why i need variable.

Actually, using %%G or the variable (with delayed expansion) inside a loop is pretty much the same thing.
Remember the FOR loop will process each single line separately; in this code,

Code: Select all

echo %%G

is the same thing as

Code: Select all

set var=%%G
echo !var!

Re: can Variables value altered mltiple times in 1 .bat file

Posted: 09 May 2012 00:48
by foxidrive
Fawers wrote:
prash11 wrote:i need to use that value at almost 30 places.
so i cant use %%G thirty times because it makes my code so unreadable and also makes it difficult to understand.

Actually, using %%G or the variable (with delayed expansion) inside a loop is pretty much the same thing.
Remember the FOR loop will process each single line separately; in this code,

Code: Select all

echo %%G

is the same thing as

Code: Select all

set var=%%G
echo !var!


I agree. Once you are familiar with using for loop variables then it's normal to read them in code.

Using delayed expansion also stops you using ! characters in your strings.