midiboy Posted February 3, 2008 Posted February 3, 2008 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.isoIf 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
Pyrosoft Posted February 3, 2008 Posted February 3, 2008 I'd be tempted to use goto's rather than error levels.Something like this might be what you're looking forif exist D:\Isos\Test.iso goto :existsgoto :notexists:existsDEL D:\Isos\Test.isogoto finish:notexistsecho File already deleted:finish
midiboy Posted February 3, 2008 Author Posted February 3, 2008 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
MHz Posted February 3, 2008 Posted February 3, 2008 @echo offif 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)pausegoto :EOFOutput when the file is in usefile foundD:\Isos\Test.isoThe process cannot access the file because it is being used by another process.successfile still existsPress any key to continue . . .failed deletion of the file even though it states success.And when the file is not in usefile foundsuccessPress 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
jaclaz Posted February 3, 2008 Posted February 3, 2008 Maybe you can use an external program, WAITFORFILE:http://www.gdps.dk/products/freeware.shtmlWaitForFileThis 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] filenameThe 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
Yzöwl Posted February 3, 2008 Posted February 3, 2008 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.txtAccess is deniedC:\>del inuse.exeThe process cannot access the file because it is being used by another processC:\>del normal.htmlC:\>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 offDel somename.ext>Nul 2>%Temp%\_$.logFindstr .* %Temp%\_$.log||Echo:successDel %Temp%\_$.log&Ping -n 6 127.0.0.1>NulIn this example if the file is deleted, you'd receive the success message in the console otherwise you'd get the error message.
GreenMachine Posted February 5, 2008 Posted February 5, 2008 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.EXTIF NOT EXIST FILENAME.EXT DO SOMETHINGIF EXIST FILENAME.EXT DO SOMETHING ELSEAm I missing something here?
Yzöwl Posted February 5, 2008 Posted February 5, 2008 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.
GreenMachine Posted February 6, 2008 Posted February 6, 2008 I WAS missing something ... seems I skipped right over that response that said what I said. Must have come out of hibernation too early ...
midiboy Posted February 6, 2008 Author Posted February 6, 2008 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now