Skip when an error occurs

I have the following code in a package (cmd):

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

EDIT: To make things clearer: for /f... looks for a directory called "Example" and loops to look for more directories than one. The first command is the delete command, it deletes all the files in the directory. command , which occurs when an error occurs, is an echo command that writes some error information to a text file. now hole skip thing; sometimes files cannot be deleted due to access denied or this file is in use by... Usually, what would happen if there were no gaps, it would just stop the team and freeze. So, I want this to not happen. Alternatively, I want to use something like skip , so it can skip the file and continue anyway. Therefore, I think that this command should be passed to the delete command.

Hope this is clear now.

Thanks in advance.

+4
source share
8 answers

Like this?

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

Create two batch files and run delex.cmd. Files and directories that are not deleted will be written to the delex.txt file. Those who hang will have a summary with a minimum CMD that will be killed after the delay with ping (thanks to the Doc Brown suggestion).

 delex2.cmd ---------- @echo off del /q %1 if exist %1 echo %1 not deleted!>>delex.txt exit delex.cmd --------- @echo off if exist delex.txt del delex.txt for /f "delims=" %%f in ('dir /s /b example') do start "delextaskkill" /min delex2.cmd "%%f" ping 127.0.0.1 -n 3 -w 1000> nul taskkill /fi "Windowtitle eq delextaskkill"> nul 

Tested with

 \example | file1 | file2 | file3 | file4 | file5 | \---example file1 file2 file3 file4 file5 
+2
source

When del used, and "access is denied" or "this file is used ...", %errorlevel% is 0, so testing %errolevel% useless. Perhaps the following simple solution works in your case:

 for /f "delims=" %%f in ('dir /b /s Example') do ( del %%f if exist %%f ( echo "file not deleted" ) else ( echo "file deleted" ) ) 
+1
source

I believe that this is what you want

 for /f "delims=" %%f in ('dir /b /s Example') do ( command if not errorlevel 1 ( command ) else ( command goto :eof ) ) 
0
source

Perhaps this is more advanced than can be done using only the built-in cmd processing. Why not consider using Windows Scripting Host (vbscript / jscript) or even PowerShell? Both are likely to provide you with the level of control you are requesting.

0
source

Try this batch file:

 @echo off REM For each directory named 'Example' ... for /f "delims=" %%f in ('dir /b /s Example') do ( REM .. enter the directory and delete all the files found there. pushd . cd %%f del /q * for /f "delims=" %%z in ('dir /b') do ( REM Any files that still exist must have been inaccessable. Log an error. echo Unable to delete file %%z > C:\logfile.txt ) popd ) 

Tested with the following directory structure:

 folder\ |------Example\ | |-------file1 (write-protected) | |-------file2 | |-------file3 |------deeper\ |-------Example\ |-------file4 |-------file5 (write-protected) |-------file6 

After starting the batch file, only file1 and file5 . An access message was rejected for each write-protected file; if this is annoying, you redirect the output of the batch file, e.g. script.bat > NUL , to hide it.

0
source

So, after all the existing answers have not satisfied you, and you absolutely must skip in your batch file, I will try again:

 @echo off setlocal for /f "delims=" %%f in ('dir /b /s batch') do call :DoSomething %%f goto end :DoSomething echo Here i am: %1 if %errorlevel%==1 goto :eof echo No error occured goto :eof :end endlocal 

The trick is that the for loop calls a subfunction in the same file and provides the necessary parameter. This new call is made in a new context and can only access variables that are defined after do call :DoSomething in that order.

So, you need to access the variables here with %1 , %2 , etc. If you want to leave this context, you must have goto :eof to go to the end of the file (this marker is predefined in batch mode and should not appear in your file), which leaves the context and returns to the for loop.

After going through the whole cycle, we just go to the marker :end , clean it up a bit and finish it.

0
source

The following are the extensions of the files you want recursively in the "dirname" directory tree and run commandstuff.bat with respect to this file name:

for / r% i in (c: \ dirname \ *. ext) do commandstuff "% i"

Commandstuff.bat is as follows:

@ECHO OFF
del% 1
IF (% ERRORLEVEL% == 0) goto END
: ERROR
Echo Delete error% 1

: END
Echo end

This will run commandstuff.bat to delete the files you want. If an error occurs, it will simply repeat the file data and continue processing the next file.

0
source

All Articles