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
Solved

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

Posted on 2006-06-28
9
1,260 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

790 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