Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   Dev Hardware ForumsSOFTWAREProgramming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Hardware Forums Sponsor:
  Trader Rating: 0 · #1  
Old June 3rd, 2008, 05:19 AM
Simmy Simmy is offline
n00b DevH'er
Dev Hardware Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 2 Simmy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m
Reputation Power: 0
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.

Reply With Quote
  Trader Rating: 4 · #2  
Old June 3rd, 2008, 05:43 AM
madhyena's Avatar
madhyena madhyena is offline
SץsAםмιи & Wяιτєя
Dev Hardware Demi-God (4500 - 4999 posts)
 
Join Date: Sep 2004
Location: Romania, the one and only.
Posts: 4,566 madhyena User rank is General 87th Grade (Above 100000 Reputation Level)madhyena User rank is General 87th Grade (Above 100000 Reputation Level)madhyena User rank is General 87th Grade (Above 100000 Reputation Level)madhyena User rank is General 87th Grade (Above 100000 Reputation Level)madhyena User rank is General 87th Grade (Above 100000 Reputation Level)madhyena User rank is General 87th Grade (Above 100000 Reputation Level)madhyena User rank is General 87th Grade (Above 100000 Reputation Level)madhyena User rank is General 87th Grade (Above 100000 Reputation Level)madhyena User rank is General 87th Grade (Above 100000 Reputation Level)madhyena User rank is General 87th Grade (Above 100000 Reputation Level)madhyena User rank is General 87th Grade (Above 100000 Reputation Level)madhyena User rank is General 87th Grade (Above 100000 Reputation Level)madhyena User rank is General 87th Grade (Above 100000 Reputation Level)madhyena User rank is General 87th Grade (Above 100000 Reputation Level)madhyena User rank is General 87th Grade (Above 100000 Reputation Level)madhyena User rank is General 87th Grade (Above 100000 Reputation Level)  Folding Points: 65608 Folding Title: Intermediate FolderFolding Points: 65608 Folding Title: Intermediate FolderFolding Points: 65608 Folding Title: Intermediate FolderFolding Points: 65608 Folding Title: Intermediate Folder
Time spent in forums: 1 Year 1 Month 3 Weeks 1 Day 15 h 42 m 9 sec
Reputation Power: 6786
Send a message via AIM to madhyena Send a message via Yahoo to madhyena Send a message via Google Talk to madhyena
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.

Reply With Quote
  Trader Rating: 0 · #3  
Old June 3rd, 2008, 06:05 AM
Simmy Simmy is offline
n00b DevH'er
Dev Hardware Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 2 Simmy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m
Reputation Power: 0
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."

Reply With Quote
  Trader Rating: 0 · #4  
Old June 3rd, 2008, 08:38 AM
bob_man_uk bob_man_uk is offline
Stressed to the nines
Dev Hardware Frequenter (2500 - 2999 posts)
 
Join Date: Sep 2004
Location: in the middle of nowhere important
Posts: 2,788 bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)bob_man_uk User rank is General 46th Grade (Above 100000 Reputation Level)  Folding Points: 836090 Folding Title: Super Ultimate Folder - Level 2Folding Points: 836090 Folding Title: Super Ultimate Folder - Level 2Folding Points: 836090 Folding Title: Super Ultimate Folder - Level 2Folding Points: 836090 Folding Title: Super Ultimate Folder - Level 2Folding Points: 836090 Folding Title: Super Ultimate Folder - Level 2Folding Points: 836090 Folding Title: Super Ultimate Folder - Level 2Folding Points: 836090 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 1 Month 1 Day 4 h 50 m 55 sec
Reputation Power: 4044
Send a message via ICQ to bob_man_uk Send a message via AIM to bob_man_uk Send a message via MSN to bob_man_uk Send a message via Yahoo to bob_man_uk Send a message via Google Talk to bob_man_uk Send a message via Skype to bob_man_uk Send a message via XFire to bob_man_uk
Bebo Facebook Orkut
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

Reply With Quote
  Trader Rating: 2 · #5  
Old June 4th, 2008, 03:05 AM
Nilpo's Avatar
Nilpo Nilpo is offline
Dev Hardware God (5000 - 5499 posts)
 
Join Date: May 2004
Location: Salem, OH
Posts: 5,344 Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)  Folding Points: 189035 Folding Title: Super Ultimate Folder - Level 1Folding Points: 189035 Folding Title: Super Ultimate Folder - Level 1Folding Points: 189035 Folding Title: Super Ultimate Folder - Level 1Folding Points: 189035 Folding Title: Super Ultimate Folder - Level 1Folding Points: 189035 Folding Title: Super Ultimate Folder - Level 1Folding Points: 189035 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 1 Week 2 Days 21 h 53 m 39 sec
Reputation Power: 5514
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
MySpace Orkut
Assuming you're using Adobe Acrobat Reader this will do the trick.
vb Code:
Original - vb Code
  1. strFile = "a.pdf"
  2.  
  3. Set objFso = CreateObject("Scripting.FileSystemObject")
  4.  
  5. DeleteFile strFile
  6. If Err.Number <> 0 Then
  7.     Set objWMIService = GetObject("winmgmts:" _
  8.         & "{impersonationLevel=impersonate}!\\.\root\cimv2")
  9.     Set colProcs = objWMIService.ExecQuery( _
  10.         "Select * From Win32_Process Where Name = 'AcroRd32.exe'")
  11.  
  12.     For Each objProc In colProcs
  13.         objProc.Terminate
  14.     Next
  15.  
  16.     DeleteFile strFile
  17. End If
  18.  
  19. Sub DeleteFile(strFile)
  20.     If objFso.FileExists(strFile) Then
  21.         On Error Resume Next
  22.         Err.Clear
  23.         intResult = objFso.DeleteFile(strFile, True)
  24.     End If
  25. End Sub
Comments on this post
bob_man_uk agrees!
__________________
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!    

Reply With Quote
  Trader Rating: 0 · #6  
Old July 1st, 2008, 09:51 PM
omrsafetyo's Avatar
omrsafetyo omrsafetyo is offline
Contributing User
Dev Hardware Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Maine, USA
Posts: 297 omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Days 21 h 32 m 32 sec
Reputation Power: 221
Send a message via AIM to omrsafetyo
If I still used batch, I would suggest that this probably could be done with a combination of the "find" command and "tasklist", combined with the "taskkill" command - and in fact probably rather easily.

Reply With Quote
Reply

Viewing: Dev Hardware ForumsSOFTWAREProgramming > DOS Batch - Need to delete a file either if opened or not opened


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway