Skip when error occurs

Deniz Zoeteman picture Deniz Zoeteman · Dec 27, 2009 · Viewed 8.8k times · Source

i have the following code in batch (cmd):

for /f "delims=" %%f in ('dir /b /s Example') do (
command
if %errorlevel%==1 (
command
SKIP
)
command
)

EDIT: To make things more clear: for /f... searches for a directory called 'Example' and loops to search for more directories than one. the first command is a delete command, it deletes all files in the directory. the command that happens when an error occurs, is a echo command which writes some info about the error to a text file. now the hole skip thing; sometimes, the files can't be deleted because of access denied or this file is in use by.... Normally, what would happen if there weren't a skip thing, it would just stop the command and hang. So, what i want to do, is prevent this from happening. Alternatively, i want to use something like skip, so it can skip the file and continue anyways. So i think this command needs to be piped in the delete command.

I hope it's clear now.

Thanks in advance.

Answer

atzz picture atzz · Dec 27, 2009

Like this?

for /f "delims=" %%f in ('dir /b /s Example') do (
  command
  if not errorlevel 1 (
    command-for-success
  ) else (
    command-for-error
  )
)