user3208239
user3208239

Reputation: 761

Within a batch file, how can one break out of nested if statements if one criteria is met, but not the surrounding for loop?

Here's what I have...

For /F %%A In ('Type "C:\List.Of.PCs.txt"') Do @(
  Set PC=%%A
  For /F %%B In ('Dir "C:\Users" /B') Do @(
    Set USR=%%B
    If "!USR!" Neq "AAA" (
      If "!USR!" Neq "ABC" (
        If "!USR!" Neq "CDE" (
          If "!USR!" Neq "DEF" (
            [28 more if statements]
            Dir "\\!PC!\C$\Users\!USR!\AppData\Local\Temp\Logs\File.To.Find.Log" >Nul 2>Nul
            If "%ErrorLevel%" Equ "0" Echo File found on !PC! for !USR! >"C:\Results.txt"
          )
        )
      )
    )
  )
)

The goal is to break out of all those if statements if only one of them is satisfied, then start over with the outer-most for loop. Is this possible? If so, how the heck is that done? Thanks in advance.

Upvotes: 0

Views: 83

Answers (1)

user7818749
user7818749

Reputation:

I see that you have an exclusion list, so if you only want to exclude certain users, but run the command for the rest, I would do something like this:

@echo off
set "exclude=AAA BBB CCC DDD EEE FFF"
For /F %%A In ('Type "C:\List.Of.PCs.txt"') Do @(
for %%I in (%exclude%) do (
  For /F %%B In ('Dir /b "C:\Users"') Do (
    if not "%%B" == "%%I" (
            Dir "\\%%A\C$\Users\%%B\AppData\Local\Temp\Logs\File.To.Find.Log" | findstr /IRC:"File\.To\.Find\.Log" >nul 2>&1
            If errorlevel 0 (Echo File found on %%A for %%B)>>"C:\Results.txt"
      )
    )
  )
)

EDIT as per your comment, look at these examples (and can be run from cmd).

set "var=123"
if not "%var:~1%" == "1" if not "%var:~-1%" == "2" if not "%var:~1%" == "9" echo %var%

The above will print true as none of the positions evaluated is true. Each if statement is evaluated, if any of them are not true, it will perform the task.

but then:

if not "%var:~1%" == "1" if not "%var:~-1%" == "3" if not "%var:~1%" == "9" echo %var%

Will not perform the echo as one of the evaluations was not met. You can place these in any order and it will still not echo as each are evaluated.

Therefore, your code can be something like (Untested):

@echo off
setlocal enabledelayedexpansion
For /F %%A In ('Type "C:\List.Of.PCs.txt"') Do @(
  For /F %%B In ('Dir /b "C:\Users"') Do (
    set "usr=%%B"
    if not "!usr:~1,3!" == "AAA" if not "!usr:~2,4!" == "BBB" if not "!usr:~1,3!" == "CCC" (
            Dir "\\%%A\C$\Users\!usr!\AppData\Local\Temp\Logs\File.To.Find.Log" | findstr /IRC:"File\.To\.Find\.Log" >nul 2>&1
            If errorlevel 0 (Echo File found on %%A for !usr!)>>"C:\Results.txt"
    )
  )
)

PS!! if not "var" == "tes" is not measure if characters match, where if 12 equ 12 is mathematical. you cannot perform if "var" leq "tes as var cannot be less or equal to tes.

See if /? for more on that.

On your last comment. No, it cannot assume if one did not match that the rest wont either. Here's a scenario:

Pete is a user to be excluded, so:

if not pete == sam if not pete == pete ...

You're expecting it to exit, because pete is not sam, so it must run the command, however, the next if would have matched. So each of the users must me evaluated against all the exclusions..

Upvotes: 1

Related Questions