WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: milesg on November 21, 2017, 10:49:33 AM

Title: Winbatch FileDelete
Post by: milesg 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
Title: Re: Winbatch FileDelete
Post by: JTaylor on November 21, 2017, 11:05:56 AM
Take a look at FileExist().

Jim
Title: Re: Winbatch FileDelete
Post by: td 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.   
Title: Re: Winbatch FileDelete
Post by: milesg 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