|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|||
|
DOS Batch - Need to delete a file either if opened or not opened
Below is my batch file
====================== @echo off cls echo. if not exist a.pdf goto nopdf del a.pdf echo PDF file is deleted. goto end :nopdf echo PDF file does not exist. :end @echo on ========================== Please tell me what updations should I make to my batch file, so that it deletes the file which is opened too. |
|
||||
|
If a PDF file is currently opened then it is locked. That means the Adobe Reader process (or Foxit Reader, etc.) has locked the file. Therefore, no you have no writing permissions or whatsoever regarding that file. This is because the active process has an open handle to that file. An open file handle prevents it from being deleted.
There are two workarounds, neither is a direct and straightforward solution. You either find the PID (Process ID) or name of the process that has locked the file, terminate that process, and as a result, you will be able to delete the file since the handle disappears and it becomes unlocked. Or you schedule it using the MoveFileEx API. Calling this API on the specified file schedules it so that during the next reboot your OS will lookup the said file and delete (or move it, replace it, depends on your option). Simply put, you mark the file for deletion. The deleting process will be executed during the very next system reboot. Here's a script that implements the aforementioned API: DelLater. And screw batch files. Your code is very simple, thus, doing it in WSH will become very easy and that way you can easily continue by further implementing the API and so on. For more information and standalone applications that lookup the process ID, terminate the active process, then delete the file check out the following links: here and here. That's all. Good luck.
__________________
“Greatness, combined with the hint of a 24-carat lifestyle, is within us striving to make it through, craving for attention, and in the end, recognition, leaving a record worthy of biography on forging success stories.” —ME Check out MadHyeNa's Website & Article Index. Visit Nilpo.com - Ask the Windows Guru!™I succeed, conquer, and achieve, therefore I am.™ |
|
|||
|
From my above batch file, when I try to delete a file which is already open, I am getting the followin message. Please tell me how do I catch the following message from my batch file
"The process cannot access the file because it is being used by another process." |
|
|||
|
off the top of my head, capturing text from a windows GUI screen isnt possible... from batch at least.
__________________
Join IRC NOW!! irc.goodchatting.com |
|
|||||
|
Assuming you're using Adobe Acrobat Reader this will do the trick.
vb Code:
__________________
Click the image if at any point you don't like my decision.10011100011000101111100011100000011000000100000011 10010011011110110001101101011011110100011000001110 0100111101000100001 Visit Nilpo.com and Ask the Windows Guru! Open me for some very useful links!
![]() |
![]() |
| Viewing: Dev Hardware Forums > SOFTWARE > Programming > DOS Batch - Need to delete a file either if opened or not opened |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|