User Tools

Site Tools


microsoft:scripting:vbscript:scripts:deldircontents

Versie 1

For deleting the contents of a directory but not the directory itself:

folderName = "C:\Temp"
 
CONST WaitOnReturn = true
CONST HideWindow = 0
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
 
'Declare the function
Function DeleteContents(dirName)
 
	If objFSO.FolderExists(dirName) Then
 
	Set delcontentsfolder = objFSO.GetFolder(dirName)
 
	'Remove all files in the directory
	For Each File in delcontentsfolder.Files
 
		objFSO.DeleteFile dirName & "\" & File.Name
 
	Next
 
	'Remove all subdirectories
	For Each Directory in delcontentsfolder.SubFolders
 
		objShell.Run "cmd /c rmdir /s /q " & chr(34) & dirName & "\" & Directory.Name & chr(34), HideWindow, WaitOnReturn
 
	Next
 
	'Cleanup
	Set delcontentsfolder = Nothing
 
	End If
 
End Function
 
'Call the function
DeleteContents(foldername)
 
'Cleanup
Set objShell = Nothing
Set objFSO = Nothing
microsoft/scripting/vbscript/scripts/deldircontents.txt · Last modified: 2009/09/17 19:29 by bas

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki