Jump to content
MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. ×

How to check if a file is locked in a cmd ?


midiboy
 Share

Recommended Posts

Hi guys,

I do have a special situation here. I would like to check in a cmd if a file was deleted or not (because it was locked by another process for instance).

I tried this:

if exist D:\Isos\Test.iso DEL D:\Isos\Test.iso

If not %Errorlevel% == 0 then .....

However, the errorcode is always 0 no matter if the file was successfully deleted or not. If it was deleted there is no response in the cmd, if the file is locked then the cmd gives a response like "file could not be deleted because it is used by another process". However, this is different in various language versions and this should work on any WinXP/Server/Vista version.

Any ideas ???

Hope so ...

Thanks a lot for your help !

Alex

Link to comment
Share on other sites


I'd be tempted to use goto's rather than error levels.

Something like this might be what you're looking for

if exist D:\Isos\Test.iso goto :exists
goto :notexists

:exists
DEL D:\Isos\Test.iso
goto finish

:notexists
echo File already deleted

:finish

Link to comment
Share on other sites


@echo off

if exist D:\Isos\Test.iso (
echo file found
del D:\Isos\Test.iso
) else (
echo file not found
)

If %errorlevel% == 0 (
echo success
) else (
echo error
)

if exist D:\Isos\Test.iso (
echo file still exists
)

pause

goto :EOF

Output when the file is in use

file found

D:\Isos\Test.iso

The process cannot access the file because it is being used by another process.

success

file still exists

Press any key to continue . . .

failed deletion of the file even though it states success.

And when the file is not in use

file found

success

Press any key to continue . . .

succeeded deletion of the file.

Checking if the file exists once is insufficient to validate deletion and %errorlevel% seems to return success upon failure if the file is in use so using a file exists after the deletion does inform of whether deletion actually did fail or not.

HTH

:)

Link to comment
Share on other sites

Maybe you can use an external program, WAITFORFILE:

http://www.gdps.dk/products/freeware.shtml

WaitForFile

This command line program waits until the file, specified as a parameter, can be opened exclusively. It can be used in .bat files if you want to process, such as copy, delete and so forth, a file that is currently opened by another program. WaitForFile will wait indefinitely until it can open the requested file.

Usage:

WaitForFile [r|w|rw] filename

The parameter r asks WaitForFile to try opening the file with exclusive read access.

The parameter w asks WaitForFile to try opening the file with exclusive write access.

The parameter rw asks WaitForFile to try opening the file with exclusive read/write access.

When WaitForFile exits, the file can be opened.

jaclaz

Link to comment
Share on other sites

The del command only outputs an error not a success so if the file isn't deleted then the error message will be shown in the console window! (This is how you know if the file was or wasn't deleted).

examples:

C:\>del readonly.txt
Access is denied

C:\>del inuse.exe
The process cannot access the file because it is being used by another process

C:\>del normal.html

C:\>

In the last example the file was deleted therefore no error was visible.

Technically speaking I suppose you could say that if the file is deleted then you'll get an empty STDERR output from the command. This of course means that if you need to you could capture the STDERR and check it too!

@Echo off
Del somename.ext>Nul 2>%Temp%\_$.log
Findstr .* %Temp%\_$.log||Echo:success
Del %Temp%\_$.log&Ping -n 6 127.0.0.1>Nul

In this example if the file is deleted, you'd receive the success message in the console otherwise you'd get the error message.

Link to comment
Share on other sites

Personally ... I like the simple approach. "I would like to check in a cmd if a file was deleted or not ". I read that to mean that if it still exists, it was not deleted, no?

DEL FILENAME.EXT

IF NOT EXIST FILENAME.EXT DO SOMETHING

IF EXIST FILENAME.EXT DO SOMETHING ELSE

Am I missing something here?
Link to comment
Share on other sites

Personally ... I like the simple approach. "I would like to check in a cmd if a file was deleted or not ". I read that to mean that if it still exists, it was not deleted, no?
<snip>
Am I missing something here?
No not at all, that's basically what MHz said.
I would say that the best concept perhaps is to check if the file does exist after the del operation and then act on that condition.
My response was more as an explanation of the process rather than a method. Just because there's no errorlevel reported there is still a stderr/stdout.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.


×
×
  • Create New...