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....????
can Variables value altered mltiple times in 1 .bat file?
Moderator: DosItHelp
Re: can Variables value altered mltiple times in 1 .bat file
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
thnks !k
this worked for me...!
cheers....!
this worked for me...!
cheers....!
Re: can Variables value altered mltiple times in 1 .bat file
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
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.
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
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
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 asCode: 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.