Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a script that recursively loads files from a specific directory (and subdirectories) into a java classpath using a FOR loop. It looks like this:

FOR /r directory %%F IN (*.jar) DO call :addcp %%F

Unfortunately, I need to now exclude a specific subdirectory from the results (I don't want some of the jar files loaded). I have tried nesting an IF statement inside of the FOR loop, but have not had any success.

Changing scripting languages is unfortunately not an option, and iterating out every subdirectory would be a maintenance nightmare. Does anyone have a way to do this?

I tried something like:

FOR /r directory %%F IN (*.jar) DO IF %%F==*string* DO call :addcp %%F

but it didn't work.

share|improve this question
I think the word you're looking for is EXclude. –  Izzy Jul 13 '09 at 19:46
Post the version with the IF statement so we can look at it. –  Dennis Williamson Jul 13 '09 at 19:50
Would it be easier to run a second FOR loop to unload some of the JAR files after the first load runs? I don't know of any exception rules for FOR loops other than using wildcard matching but that requires that all your matching directories use the same wildcard that the excluded folder does not –  Kevin Kuphal Jul 13 '09 at 19:52
Also, this has nothing to do with Java, so I'm retagging it (and adding the OS, which looks like Windows). –  Dennis Williamson Jul 13 '09 at 19:54

3 Answers 3

up vote 3 down vote accepted

Here's a script to dump a list of the absolute paths of all EXE files under "%ProgramFiles%" except those that are in the "Windows NT" subdirectory. I would think you could probably beat this into submission for what you're looking for.

@echo off

for /f "usebackq delims=" %%i in (`dir "%ProgramFiles%\*.exe" /s /a /b`) do call :checkit "%%i" 
goto end

echo %1 | find /i "%ProgramFiles%\Windows NT\" >NUL 2>NUL
if errorlevel 1 echo %~1

share|improve this answer
had to muck with it a bit, but this worked - thanks! –  Keith Jul 13 '09 at 20:46

%%F resolves to the path to the .jar file, not to the directory name, which is why the == doesn't work.

I suggest a 2-step process - first do

dir /s /b directory\*.jar > temp.txt

and then

for /F usebackq %%I in (`findstr /V <excluded path> temp.txt`) do call :addcp %%I

the /V parameter to findstr prints all lines that don't match the string you pass in. You should be able to use wildcards or even regular expressions for your exclusion as well, if you really need to.

share|improve this answer

I would recommend find, take a look at man find. Something like find "%ProgramFiles%\" -iname "*.exe" ! -regex "%ProgramFiles%\folder_to-exclude.*$" -exec call :addcp {} \; might do what you want.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.