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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
prash11
Posts: 21
Joined: 27 Apr 2012 01:38

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

#1 Post by prash11 » 07 May 2012 02:05

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....????

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

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

#2 Post by !k » 07 May 2012 02:22

Code: Select all

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

prash11
Posts: 21
Joined: 27 Apr 2012 01:38

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

#3 Post by prash11 » 07 May 2012 05:23

thnks !k
this worked for me...!
cheers....!

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

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

#4 Post by Squashman » 07 May 2012 05:49

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.

prash11
Posts: 21
Joined: 27 Apr 2012 01:38

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

#5 Post by prash11 » 07 May 2012 23:42

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.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

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

#6 Post by Fawers » 08 May 2012 10:02

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!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#7 Post by foxidrive » 09 May 2012 00:48

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.

Post Reply