Link to home
Start Free TrialLog in
Avatar of alttechnology
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.FileSystemObject")
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
Avatar of gafoorgk
gafoorgk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alttechnology
alttechnology

ASKER

I know that I have very limited experience but, I think I understand most of this.  I do error on the Private Function line.  Is that syntax correct?  I get the error looking for the end the statement.   I do not know the correct line.  Can you help  Thanks !

 
i didn't understand u. can u explain in detail ?!
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 ')'

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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(7, 38) Microsoft VBScript compilation error: Expected
 ')'

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
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.dir00Line 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(strFolderName)
    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