viewpoint-particle

Author Topic: Winbatch FileDelete  (Read 66 times)

milesg

  • Newbie
  • *
  • Posts: 10
Winbatch FileDelete
« on: November 21, 2017, 10:49:33 am »
I have a Winbatch script that uses the FileDelete command but sometimes the file I am trying to delete is in use by another program/user so my FileDelete fails and terminates my program
How can I prevent this from happening or recover from the filedelete failure and continue processing.
Thanks
Garth

JTaylor

  • Pundit
  • *****
  • Posts: 806
    • Data & Stuff Inc.
Re: Winbatch FileDelete
« Reply #1 on: November 21, 2017, 11:05:56 am »
Take a look at FileExist().

Jim

td

  • Tech Support
  • *****
  • Posts: 2223
    • WinBatch
Re: Winbatch FileDelete
« Reply #2 on: November 21, 2017, 11:10:34 am »
Generally, FileDelete errors because the file is marked read-only or the file is currently opened without read sharing by another process.  If the problem is the later, you can check for the no read sharing status by calling the FileExist function.   It will return 2 when the file does not have read sharing enabled. 

The alternative would be to use WinBatch error handle techniques.  You can either use the ErrorMode function around the call to FileDelete or use IntControl 73 for a more comprehensive error handler.  Check out the WIL Consolidated Help file for details.

Of course if the file is simply marked by the old DOS attributes as read-only, you can detect and change them using the FileAttr* functions.   
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


milesg

  • Newbie
  • *
  • Posts: 10
Re: Winbatch FileDelete
« Reply #3 on: November 22, 2017, 03:36:33 am »
Thank you all for your assistance - I used the intcontrol option to recover from the failure after writing a warning to the Syslog.
Garth