Solved

Batch file problem

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Migrate DHCP from server 2000 to 2008 1 623
Windows 2003 server: List of EVENT IDs 1 714
Migrate Windows NT to Windows 2003 2 502
Windows 7 7 258
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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

12 Experts available now in Live!

Get 1:1 Help Now