• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 950
  • Last Modified:

Script to delete non-locked files and folders

Hi experts,

our users have their c:\temp folder cleaned everytime they login. This folder (and/or subfolders) may sometimes contain files that are in-use/locked by the OS. Up until now we used deltree.exe to clean up this folder. The program simply skipped the locked files.
With the introduction of W7 X64, deltree no longer functions. Some googling pointed us to del3.exe as alternative. Unfortunatly, this program exits after the first locked file/folder.

As our login script is VBS based, I'm looking for a procedure that can delete all files and subfolders in any given folder, skipping locked files/folders.

0
tdemeyer
Asked:
tdemeyer
1 Solution
 
sr75Commented:
Here is what I have used.
On Error Resume Next

strPath = "C:\Temp"

Set FSO = CreateObject("Scripting.FileSystemObject")
DelFiles(strPath)
CleanSubfolders FSO.GetFolder(strPath)

wscript.echo "all done"

Sub CleanSubFolders(Folder)
	On Error Resume Next
	For Each Subfolder in Folder.SubFolders
		DelFiles(Subfolder)
        	
		CleanSubFolders Subfolder
		
		DelFolder(Subfolder)
    	Next
End Sub

Sub DelFiles(Folder)
	On Error Resume Next
	set objFolder = FSO.GetFolder(Folder)
	For Each File in objFolder.Files
		FSO.DeleteFile(File)
	Next
End Sub

Sub DelFolder(Folder)
	On Error Resume Next
	boolCheck = False
	For Each File in Folder.Files
		boolCheck = True
	Next	
	For Each Subfolder in Folder.Subfolders
		boolCheck = True
	Next
	If boolCheck = False then
		FSO.DeleteFolder(Folder)
	End If
End Sub

Open in new window

0
 
tdemeyerAuthor Commented:
Thanks, needed some minor adjustments to fit our own script (due to option explicit), but works flawlessly.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now