VBScript - Delete locked files

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 13 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ismailc
Posts: 37
Last visit: Tue Jun 09, 2015 12:16 am

VBScript - Delete locked files

Post by ismailc »

Hi,
I want delete all files in a folder, but I get access denied as the file is locked.
I use an email function before i'm trying to delete the files, the send email is a built in function of the application:
fcMailer.SendMailWithAttachment Process.Company, "email", Subj, "Bo", "e:uploadtest.xls"
i tried to set it to nothing: set fcMailer = nothing and set fcMailer.SendMailWithAttachment = nothing
I'm using the below to delete the files

Code: Select all

	Const DeleteReadOnly = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("e:upload*.*"), DeleteReadOnly
	
How do i remove connection holding the file or simply just delete all locked files / or not locked
Please Assist!
User avatar
ismailc
Posts: 37
Last visit: Tue Jun 09, 2015 12:16 am

VBScript - Delete locked files

Post by ismailc »

Hi,
I want delete all files in a folder, but I get access denied as the file is locked.
I use an email function before i'm trying to delete the files, the send email is a built in function of the application:
fcMailer.SendMailWithAttachment Process.Company, "email", Subj, "Bo", "e:uploadtest.xls"
i tried to set it to nothing: set fcMailer = nothing and set fcMailer.SendMailWithAttachment = nothing
I'm using the below to delete the files

Code: Select all

	Const DeleteReadOnly = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("e:upload*.*"), DeleteReadOnly
	
How do i remove connection holding the file or simply just delete all locked files / or not locked
Please Assist!
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

VBScript - Delete locked files

Post by jhicks »

I'd suggest using OpenFiles.exe from the ResourceKit, although it might be a part of your OS now depending on what you have. On Win7 it seems to be included.

http://technet.microsoft.com/en-us/libr ... 90961.aspx
User avatar
ismailc
Posts: 37
Last visit: Tue Jun 09, 2015 12:16 am

VBScript - Delete locked files

Post by ismailc »

Thank You for helping:

I'm struggling to execute in vbscript window.

Code: Select all

	
openfiles.exe /disconnect /op "e:uploadtext.xls" 
	
User avatar
Gyorgy Nemesmagasi
Posts: 50
Last visit: Wed Mar 16, 2022 12:58 pm

VBScript - Delete locked files

Post by Gyorgy Nemesmagasi »

The problem about the double )) when you call the run method. Try this way. You need to copy the openfiles into the folder of your script. If you don't want to check the return code of the executable you not need to use the brackets.

Code: Select all

	
Dim oShell
Dim strScriptFolder
Dim strCommand
	
Set oShell = WScript.CreateObject("WScript.Shell")
	
strScriptFolder = Left(WScript.ScriptFullName,Len(WScript.ScriptFullName)-Len(WScript.ScriptName))
strCommand = """" & strScriptFolder & "openfiles.exe"" /disconnect /op ""e:uploadtest.xls"""
oShell.run strCommand, 1, True
	
Set oShell = Nothing
	
Check the WSH documentation further detailes.
http://www.microsoft.com/downloads/deta ... laylang=en
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

VBScript - Delete locked files

Post by jvierra »

If you copy the files they are not locked for reading.

If you delete the copy you have not deleted the original file and it will still exist.

I think you/we are missing someting here.

You should not delete file that are open to a task . The problemmay occur when running script under a debugger and reqquires stopping and restarting the utility. This happens with PrimalScript all of the time. It happens often when an Office application is being automated and not properly shut down.

OpenFiles is useful only if a process has aborted but leaves a thread deadlocked or hung. You need to be careful as using this incorrectly or under the wrong circumstances can destabilize as system.

OpenFiles is only useful against remote opens unless you set teh global flag for handle tracking which slows system performance.

OpenFiles is primarily a debugging tool and not really intended to be used in scripts. This is why it is hidden in the Resource Kit.

I am noting all of this because you have not really found your cause. It looks like either we misunderstood the cause or there is something else going on.


jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

VBScript - Delete locked files

Post by jvierra »

Please be aware that thes4e references to the path tools have nothing to do with the issue discussed here. FIles cannot be removed because they are locked by a process.



The need to remove the lock in code is well documented by Microsoft.

In the past most of these 'pathtoolong" and "pathtoodeep" tools have been trojans. Use them at your own risk whilenoting that they WIL NOT solve this problem.
This topic is 13 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked