Computing.Net > Forums > Disk Operating System > Adding to batch string

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Adding to batch string

Reply to Message Icon

Name: Paul Collier
Date: September 5, 2002 at 11:35:45 Pacific
OS: DOS/Win
CPU/Ram: 866mhz/128mb
Comment:

I have a directory with several .o files which I want to be put into an enviroment variable, seperated by spaces. My code only puts the name of the last file (alphabetically) in the directory into the variable. What's wrong with this code?
---------------
set ofiles=
for %%o in (*.o) do set ofiles=%ofiles% %%o
echo %ofiles%
---------------

Thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: Secret_Doom
Date: September 5, 2002 at 17:28:18 Pacific
Reply:

It doesn't work, hard to explain why. Basically, the COMMAND.COM will read that line and replace the "%files%" varible for its current value, before executing the FOR cmd. So, you script is the same as:

for %%o in (*.o) do set ofiles= %%o
echo %ofiles%

However, there are ways around that:

@echo off
if "%1"=="GoTo:" GOTO %2
set ofiles=
for %%? in (*.o) do call %? GoTo: setvar %%?
echo %ofiles%
goto eof
:setvar
set ofiles=%ofiles% %3
:eof

This another one will also work, if you'd prefer (it's simpler but less functional):

@echo off
echo SET OFILES=%%OFILES%% %%1> %TEMP%.\SETVAR.BAT
set ofiles=
for %%? in (*.o) do call %TEMP%.\SETVAR.BAT %%?
del %TEMP%.\SETVAR.BAT
echo %ofiles%

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br

___________________________________________________________


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: Adding to batch string

passing param to batch from HREF www.computing.net/answers/dos/passing-param-to-batch-from-href/14323.html

batch file to get subset of dir? www.computing.net/answers/dos/batch-file-to-get-subset-of-dir/15350.html

passing parameters to batch? www.computing.net/answers/dos/passing-parameters-to-batch/14305.html