Page 1 of 1

How to catch DEL errors? It's possible?

Posted: 01 Apr 2016 09:44
by einstein1969
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?

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

Posted: 01 Apr 2016 10:41
by dbenham
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

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

Posted: 01 Apr 2016 10:54
by einstein1969
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