Solved

VBScript and WMI Need to delete Folder/Files under the parent directory How to?

Posted on 2006-06-28
9
1,274 Views
Last Modified: 2009-12-16
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 !
0
Comment
Question by:alttechnology
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 3

Assisted Solution

by:gafoorgk
gafoorgk earned 500 total points
ID: 17006967
follow this code or take it as a template for your need. if file or folder could not be delete for some reason, it's logged and returned.


Option Explicit

Private Sub Command1_Click()
    Debug.Print DeleteAll("\\<sharename>\TEST")
End Sub

Private Function DeleteAll(strFolder As String) As String
    Dim fso As Scripting.FileSystemObject
    Dim objFile As Scripting.File
    Dim objFolder As Scripting.Folder
    Dim strLog As String
   
    '// Create new file system object
    Set fso = New Scripting.FileSystemObject
   
    On Error Resume Next
   
    '// First delete all files
    For Each objFile In fso.GetFolder(strFolder).Files
        objFile.Delete True
        '// If errors occured
        If (Err.Number > 0) Then
            strLog = strLog & "File " & objFile.Path & " could not be deleted." & vbCrLf
            Err.Clear
        End If
    Next
   
    '// Then delete all folders
    For Each objFolder In fso.GetFolder(strFolder).SubFolders
        objFolder.Delete (True)
        '// If errors occured
        If (Err.Number > 0) Then
            strLog = strLog & "Folder " & objFolder.Path & " could not be deleted." & vbCrLf
            Err.Clear
        End If
    Next
   
    '// Finally return the log
    DeleteAll = strLog
   
    '// Clear objects
    Set fso = Nothing
End Function
0
 

Author Comment

by:alttechnology
ID: 17014716
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 !

 
0
 
LVL 3

Expert Comment

by:gafoorgk
ID: 17022331
i didn't understand u. can u explain in detail ?!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:alttechnology
ID: 17023867
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 ')'

0
 
LVL 3

Accepted Solution

by:
gafoorgk earned 500 total points
ID: 17025621
ah sorry. actually the code was not tested the line 'objFolder.Delete (True)' caused the error. following code is corrected.

Option Explicit

Private Sub Command1_Click()
    Debug.Print DeleteAll("C:\Documents and Settings\gafoorgk\Desktop\New Folder")
End Sub

Private Function DeleteAll(strFolder As String) As String
    Dim fso As Scripting.FileSystemObject
    Dim objFile As Scripting.File
    Dim objFolder As Scripting.Folder
    Dim strLog As String
   
    '// Create new file system object
    Set fso = New Scripting.FileSystemObject
   
    On Error Resume Next
   
    '// First delete all files
    For Each objFile In fso.GetFolder(strFolder).Files
        objFile.Delete True
        '// If errors occured
        If (Err.Number > 0) Then
            strLog = strLog & "File " & objFile.Path & " could not be deleted." & vbCrLf
            Err.Clear
        End If
    Next
   
    '// Then delete all folders
    For Each objFolder In fso.GetFolder(strFolder).SubFolders
        objFolder.Delete True
        '// If errors occured
        If (Err.Number > 0) Then
            strLog = strLog & "Folder " & objFolder.Path & " could not be deleted." & vbCrLf
            Err.Clear
        End If
    Next
   
    '// Finally return the log
    DeleteAll = strLog
   
    '// Clear objects
    Set fso = Nothing
End Function
0
 

Author Comment

by:alttechnology
ID: 17091627

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
0
 

Author Comment

by:alttechnology
ID: 17092948
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
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question