1.

Solve : Batch file For loop File in Directory?

Answer»

Hi,

 I need to be able to loop the files in a directory and for each one append it to a path variable, I keep getting close but no cigar. Could someone help? here is what I have so far:

Code: [Select]:noEnvReset
set MY_CLASSPATH=
FOR %%f IN ( "%MY_DIR%\lib\*.jar" ) DO CALL :process "%%~f"
goto end

:process
if "%MY_CLASSPATH%"=="" set MY_CLASSPATH=%1
set MY_CLASSPATH=%MY_CLASSPATH%;%1
GOTO:EOF

:end
echo %MY_CLASSPATH%
If I take the conditional "if" out of the "process" sub-routine it builds the path fine but then dies at the end with:

Code: [Select]and was unexpected at this time.
If I leave the conditional in it just dies with the same error. Any help would be appreciated. Cheers.I've resolved this temporarily by passing the process sub-routine the names of all the paths using %~sf but would appreciate it if anyone had any better ideas. Cheers.I couldn't figure out why the call was necessary, so why not let the for do all the work, and get rid of the PESKY leading semi-colon after all the fireworks are over.

Code: [Select]echo off
:noenvreset
set my_classpath=
for /f "tokens=* delims=" %%f in ('dir /s /b /a-d "%MY_DIR%\lib\*.jar"') do (
call set my_classpath=%%my_classpath%%;%%~f)
)
set my_classpath=%my_classpath:~1%
echo %my_classpath%

This is not necessarily a better IDEA, just an alternative to what you came up with.

Good luck. 

you really don't like delayed expansion, do you, Sidewinder?
Quote from: DIAS de verano on November 19, 2008, 11:53:38 AM

you really don't like delayed expansion, do you, Sidewinder?


!DUE to unforeseen circumstances, the RESPONSE to this post has been delayed!

 

Thanks a lot for the help. It appears to work.

I will try and work out what the *censored* all these slashes and such mean at a later date, thanks again.


Discussion

No Comment Found