Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

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. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 


midiboy

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

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Hi Pyrosoft,

yeah, thanks. I did this now and it seems to work. Just does not seem very elegant but it is working so thanks ! :-)

Have a nice day !

Alex

Share this post


Link to post
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

:)

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

I WAS missing something ... seems I skipped right over that response that said what I said. Must have come out of hibernation too early ...

Share this post


Link to post
Share on other sites

Hi Guys,

wow, so many replies ! :-) Thanks all for chiming in.

I have selected the simplest approach just as Greenmachine, Mhz and Pyrosoft suggested.

Thanks all for your help and support !

Alex

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...