Solved

Batch file problem

Posted on 2004-10-18
4
395 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
  • 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 70

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 70

Expert Comment

by:Chris Dent
ID: 12342024

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

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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…
IT certifications are a concrete representation of continual learning on the part of the candidate.  Continual learning is necessary for the long term success of an IT professional, but are IT certifications the right path for you?
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

840 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