Solved

Batch file problem

Posted on 2004-10-18
4
387 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 83

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now