How to catch DEL errors? It's possible?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

How to catch DEL errors? It's possible?

#1 Post by einstein1969 » 01 Apr 2016 09:44

Hi,

I open this even if is already an open thread "Access denied in concurrent access" because this is more specific.
The solution on other thread my be different.

I have two batch file:

master_del.cmd

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem start the slave process
start "Slave" "%comspec%" /c "slave_del.cmd"

Title Master

rem prepare file
>file.bat (
  >nul ping -n 2 127.0.0.1
  echo A
)

Echo Done.
pause

slave_del.cmd

Code: Select all

@echo off
setlocal EnableDelayedExpansion

for /l %%. in () do if exist "file.bat" (
  del file.bat
  echo errorlevel:!errorlevel!
  del file.bat && echo OK || echo NOK
)


The output of slave_del is this:

Code: Select all

C:\Users\ACER\Desktop\file.bat
Impossibile accedere al file. Il file è utilizzato da un altro processo.
errorlevel:0
C:\Users\ACER\Desktop\file.bat
Impossibile accedere al file. Il file è utilizzato da un altro processo.
OK
C:\Users\ACER\Desktop\file.bat
Impossibile accedere al file. Il file è utilizzato da un altro processo.
errorlevel:0
C:\Users\ACER\Desktop\file.bat
Impossibile accedere al file. Il file è utilizzato da un altro processo.
OK
C:\Users\ACER\Desktop\file.bat
Impossibile accedere al file. Il file è utilizzato da un altro processo.
errorlevel:0
C:\Users\ACER\Desktop\file.bat
Impossibile accedere al file. Il file è utilizzato da un altro processo.
OK
...


Execute the master.cmd for view the output.

I can't catch the errors :(

How I can do?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to catch DEL errors? It's possible?

#2 Post by dbenham » 01 Apr 2016 10:41

I know it sounds crazy, but it is impossible to directly detect a DEL error. :shock: :evil:

I know of only two strategies:

1) Test afterward to see if the file(s) still exists - For some reason, I don't like this option, but it may be the most efficient. I haven't really tested

2) Redirect stderr to 1 and stdout to null when DELeting, and pipe the result to FINDSTR looking for any output. If FINDSTR detects output, then DEL failed, else DEL succeeded.

Code: Select all

del someFileSpec 2>&1 1>null | findstr "^" && echo ERROR || echo OK

If you don't need to see the actual error message, then redirect FINDSTR output to null

Code: Select all

del someFileSpec 2>&1 1>null | findstr "^" >nul && echo ERROR || echo OK


Dave Benham

einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: How to catch DEL errors? It's possible?

#3 Post by einstein1969 » 01 Apr 2016 10:54

Thanks Dave.

I like the second method.

For the first method I think that the DEL command can deferred and the file exist for a while. See this

Post Reply