alttechnology
asked on
VBScript and WMI Need to delete Folder/Files under the parent directory How to?
Hi,
I need to delete files and folders under a directory but keep the base. I have tried FSO Example
Set FSO = CreateObject("Scripting.Fi leSystemOb ject")
FSO.DeleteFolder "D:\test\*", True
FSO.DeleteFile "D:\test\*", True
But if a file/directory is open or can not be removed the script stops and ends. From what I can tell I will have to use WMI in the script and enumerate. I am a total newbie!! and dont have a clue. What I want to do is
1) Delete all files and subdirectories under \\<sharename>\TEST\ but not TEST itself
2) If a file or folder is open and locked skip that file/folder and continue (FSO can NOT do this)
3) Pass a error level back.
If someone can give me a working example it really would be great!!!!
Thanks !
I need to delete files and folders under a directory but keep the base. I have tried FSO Example
Set FSO = CreateObject("Scripting.Fi
FSO.DeleteFolder "D:\test\*", True
FSO.DeleteFile "D:\test\*", True
But if a file/directory is open or can not be removed the script stops and ends. From what I can tell I will have to use WMI in the script and enumerate. I am a total newbie!! and dont have a clue. What I want to do is
1) Delete all files and subdirectories under \\<sharename>\TEST\ but not TEST itself
2) If a file or folder is open and locked skip that file/folder and continue (FSO can NOT do this)
3) Pass a error level back.
If someone can give me a working example it really would be great!!!!
Thanks !
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
i didn't understand u. can u explain in detail ?!
ASKER
Sure,
I am sure this is a simple fix but, I get an error on
Function DeleteAll(strFolder As String) As String
Error VBScript compilation error: Expected ')'
I am sure this is a simple fix but, I get an error on
Function DeleteAll(strFolder As String) As String
Error VBScript compilation error: Expected ')'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry for not responding sooner. I was on vacation. I just tested the script and I get the following error. I jsut did a cut and paste and it errors on
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
D:\Scripts\newdelfiles.vbs
')'
Hhmm... I just ran it in VBSEdit and it does not like the
Private Function DeleteAll(strFolder As String) As String
I am testing this on a Windows XP WS. Sorry for not being able to dubug this further...
Thanks Al
ASKER
gafoorgk, Here is one that works quite well. The only issue I have is if there is files in the root it does not like that and errors with
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
c:\testlog\werec13.dir00Li ne 50
D:\Scripts\Thisisit.vbs(63 , 5) (null): 0x80041002
Dim arrFolders()
intSize = 0
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
strFolderName = "c:\testlog"
Set colSubfolders = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1
For Each objFolder in colSubfolders
GetSubFolders strFolderName
Next
Sub GetSubFolders(strFolderNam e)
Set colSubfolders2 = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")
For Each objFolder2 in colSubfolders2
strFolderName = objFolder2.Name
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1
GetSubFolders strFolderName
Next
End Sub
For i = Ubound(arrFolders) to 1 Step -1
strFolder = arrFolders(i)
strFolder = Replace(strFolder, "\", "\\")
Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where Name = '" & strFolder & "'")
For Each objFolder in colFolders
errResults = objFolder.Delete
Next
Next
'Now do the files in root of the directory
wscript.echo strFolderName & "Line 50"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='"& strFolderName & "'} Where " _
& "ResultClass = CIM_DataFile")
'If colFileList.Count = 0 Then
' Wscript.Echo "There were no files in the folder."
'Else
' Wscript.Echo "There were " & colFileList.Count & " files in the folder."
'
For Each objFile In colFileList
objFile.Delete
Next
'End If
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
c:\testlog\werec13.dir00Li
D:\Scripts\Thisisit.vbs(63
Dim arrFolders()
intSize = 0
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
strFolderName = "c:\testlog"
Set colSubfolders = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1
For Each objFolder in colSubfolders
GetSubFolders strFolderName
Next
Sub GetSubFolders(strFolderNam
Set colSubfolders2 = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")
For Each objFolder2 in colSubfolders2
strFolderName = objFolder2.Name
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1
GetSubFolders strFolderName
Next
End Sub
For i = Ubound(arrFolders) to 1 Step -1
strFolder = arrFolders(i)
strFolder = Replace(strFolder, "\", "\\")
Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where Name = '" & strFolder & "'")
For Each objFolder in colFolders
errResults = objFolder.Delete
Next
Next
'Now do the files in root of the directory
wscript.echo strFolderName & "Line 50"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='"& strFolderName & "'} Where " _
& "ResultClass = CIM_DataFile")
'If colFileList.Count = 0 Then
' Wscript.Echo "There were no files in the folder."
'Else
' Wscript.Echo "There were " & colFileList.Count & " files in the folder."
'
For Each objFile In colFileList
objFile.Delete
Next
'End If
ASKER