Solved

Batch file problem

Posted on 2004-10-18
4
399 Views
Last Modified: 2010-04-14
The following batch file does not execute as I thought it would.

REM *** START OF BATCH FILE ***
@echo off
for /f %%a in ('dir /b') do (
echo a= %%a
set abc=%%a
echo abc= %abc%
)
pause
REM *** END OF BATCH FILE ***

Let's say the output of the "dir /b" command is:

file1.txt
file2.txt
file3.txt

The output of my batch file should be:

a= file1.txt
abc= file1.txt
a= file2.txt
abc= file2.txt
a= file3.txt
abc= file3.txt

Instead, I get:

a= file1.txt
abc=
a= file2.txt
abc=
a= file3.txt
abc=

It seems that the line "set abc=%%a" in my batch file fails to properly set the variable.  Any idea why this does not work?
0
Comment
Question by:EddieF
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 250 total points
ID: 12341929
Yes; for the shell interpreter, the "for /f" construct is basically one long line. As such, variables in it will only be expanded once, when the line is read. Enter
help set
in a command window, and you'll find a detailed explanation.
There are two ways around this; use the delayed expansion that's available from W2k on; this will give you the "!" to expand variables during run-time:

REM *** START OF BATCH FILE ***
setlocal enabledelayedexpansion
@echo off
for /f "delims=" %%a in ('dir /b') do (
echo a= %%a
set abc=%%a
echo abc= !abc!
)
pause
REM *** END OF BATCH FILE ***

will produce the correct output (note that i added "delims=", otherwise you'll have trouble with filenames containing spaces).
An NT4 compatible way is to call a batch subroutine in the loop:

REM *** START OF BATCH FILE ***
@echo off
for /f "delims=" %%a in ('dir /b') do (
  set abc=%%a
  call :process
)
goto leave

:process
echo abc= %abc%
goto :eof

:leave
pause
REM *** END OF BATCH FILE ***
0
 

Author Comment

by:EddieF
ID: 12341968
Thanks for the help.  This works great!
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 12342018

DOS isn't the most flexible language in the world... upgrade? ;)

Even if you get it displaying abc properly, which can be done with slightly better variable naming..:

REM *** START OF BATCH FILE ***
@echo off
for /f %%a in ('dir /b') do (
echo a= %%a
set var=%%a
echo var= %var%
)
pause
REM *** END OF BATCH FILE ***

But now all you get is the final value for %%a being echoed back. Bizarre huh?
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 12342024

Oo he's better at this than me ;)
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is comprised of the pearls we have garnered deploying virtualization solutions since Virtual Server 2005 and subsequent 2008 RTM+ Hyper-V in standalone and clustered environments.
NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question