tags:

views:

1752

answers:

4

We have a batch script that removes files (del) and directories (rd). Does anyone know how to halt (fail) execution of the script if any of these delete statements fail? They could fail if a file/directory is locked by Windows. Thanks.

Update

Statements I'm using:

Del: del *.* /S /Q

RD: FOR /D %%G in (*) DO RD /s /q %%G

+1  A: 

EDIT: Right, so del does not set the ERRORLEVEL correctly. See ERRORLEVEL on DEL and Windows delete command can fail silently

PREVIOUS SOLUTION

You should check the errorlevel.

For example:

del file.txt
if errorlevel 1 goto FAIL
:PASS
echo Worked!
goto :END

:FAIL
echo Failed!
exit /B 1

:END
grammar31
Doesn't seem to work:C:\>test.batC:\>del boo.docboo.doc. The process cannot access the file because it is being used by another process.C:\>if errorlevel 1 goto FAILC:\>echo Worked!Worked!C:\>echo Failed!Failed!C:\>exit /B 1
Marcus
+1  A: 

DEL doesn't return an errorlevel if the file is locked. I just did a test with excel file and I saw a zero (on Windows XP).

It might be better to use IF EXIST for the file to be deleted after you do the delete.

del file.txt
if exist file.txt ECHO "FAIL"

AFTER EDIT Disclaimer: I have no idea how this performs...

You could do this for the files

DIR /B /S /A-d > c:\filestodelete.txt

del *.* /S /Q    

FOR /F %%i in (c:\filestodelete.txt) DO (
    IF EXIST %%i ECHO %%i STILL EXISTS
)

then for the directories

DIR /B /S /Ad > c:\directoriestodelete.txt

FOR /D %%G in (*) DO RD /s /q %%G    

FOR /F %%i in (c:\directoriestodelete.txt) DO (
    IF EXIST %%i ECHO %%i STILL EXISTS
)
Jason Punyon
Good suggestion - except I am removing multiple files (see edited post) so it's hard to do the exist statement for each dir/file.
Marcus
@Marcus Leon - I think what I added should work for your general case...
Jason Punyon
+2  A: 

For deleting the files, you can first try to ren (rename) the file. ren will set ERRORLEVEL to 1 if the file is locked.

@echo OFF

:: Delete all files, but exit if a file is locked.
for %%F in (*.*) do (
    @echo Deleting %%F
    ren %%F tmp 2> nul
    if ERRORLEVEL 1 (
        @echo Cannot delete %%F, it is locked.
        exit /b 1
    )
    del tmp
)

I suspect you may be able to do the same thing for directories, but I can't seem to figure out how to get a directory locked so I can test. The following may work:

:: Remove all directories, but exit if one is locked.
FOR /D %%G in (*) DO (
    @echo Removing %%G
    ren %%G tmpdir 2> nul
    if ERRORLEVEL 1 (
        @echo Cannot remove %%G, it is locked
        exit /b 1
    )
    RD /s /q tmpdir
)
Patrick Cuff
A: 

Here's my preferred way to check for errors while deleting files. We redirect the "error output" (standard file "2") to a file (e.g. delCmd.err). Then we use the FOR command as a way to get access to the ~z "file size" operater. If the size of the output file is not 0, then we know that "del" got an error... we display the error with the "type" command and exit the batch file with a non-zero error code:

del unwanted.txt 2> delCmd.err
FOR /F "usebackq" %%A IN ('delCmd.err') DO set size=%%~zA
if not "%size%"=="0" (
    echo Error deleting unwanted.txt
    type delCmd.err
    exit /B 1
)
JoelFan