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.
Locked
User avatar
ismailc
Posts: 37
Joined: Mon Nov 09, 2009 6:11 pm

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
Joined: Mon Nov 09, 2009 6:11 pm

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
Joined: Sun Jan 21, 2007 11:31 pm

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
Joined: Mon Nov 09, 2009 6:11 pm

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
Joined: Sun Mar 03, 2013 12:40 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: 14553
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 14553
Joined: Tue May 22, 2007 9:57 am
Contact:

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.

Locked