Need to exclude a specific result from a Batch For Loop

9,086

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

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

:end
Share:
9,086

Related videos on Youtube

Ian Stanway
Author by

Ian Stanway

Updated on September 17, 2022

Comments

  • Ian Stanway
    Ian Stanway over 1 year

    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.

    • aeroshock
      aeroshock almost 15 years
      I think the word you're looking for is EXclude.
    • Dennis Williamson
      Dennis Williamson almost 15 years
      Post the version with the IF statement so we can look at it.
    • Kevin Kuphal
      Kevin Kuphal almost 15 years
      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
    • Dennis Williamson
      Dennis Williamson almost 15 years
      Also, this has nothing to do with Java, so I'm retagging it (and adding the OS, which looks like Windows).
  • Ian Stanway
    Ian Stanway almost 15 years
    had to muck with it a bit, but this worked - thanks!