Solved

Delete Folders

Posted on 2011-03-21
9
514 Views
Last Modified: 2012-05-11
I have nearly 30 Terminal Servers and I need a VBScript that will connect to each
of them and look for any folders starting with "psr" and delete them.

Eg
Connectt to \\auprits01\c$  and delete all fodlers starting with "psr"
\\auprits02\c$, auprits03/c$ and so on
0
Comment
Question by:Bianchi928
[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
  • 5
  • 3
9 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 35186658
Hi, do you want those deleted just from the root of the drive, or from the entire drive?  The entire drive would take a long time, whereas enumerating the folders from only the root won't take long.

Regards,

Rob.
0
 

Author Comment

by:Bianchi928
ID: 35187951
Oh..I should have been more specific...

I want to delete all folders with a name starting with "psr" in the following

in \\auprits01\c$\documents and settings\psr???
   \\auprits02\c$\dpcuments and settings\psr???
.
.
  \\auprits34\c$\documents and settings\psr???

I can supply an input list or maybe it can be hardcoded 1 toi 34 in a loop

Thanks
Cheers
0
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
ID: 35188560
Hi,

Try the following code.

regards
Prashanth
On Error resume next
Set objFSO = CreateObject("Scripting.FileSystemObject")

For i=1 To 34
    
    If i<=9 Then
        dir_path="\\auprits0"&i&"\c$\documents and settings\"
    Else
        dir_path="\\auprits"&i&"\c$\documents and settings\"
    End If
    
    Set objfolder=objfso.GetFolder(dir_path)
    
    For Each sf In objfolder.SubFolders
        
        If LCase(Mid(sf.Name,1,3))="psr" Then
            'WScript.Echo sf.Path
            Err.Clear
            sf.Delete
            If Err.Number<>0 Then
                WScript.Echo dir_path & "\" & sf.Name &" Cannot be deleted " & Err.Number
            Else
                WScript.Echo dir_path & "\" & sf.Name &" deleted successfully "
            End If
        End If
    Next
Next

Open in new window

0
Independent Software Vendors: 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:Bianchi928
ID: 35193674
Hi Rob !!

I did a slight modification to test it locally and it came up with the following error message.

c:\documents and settings\psr101a Cannot be deleted 70
c:\documents and settings\psr102a Cannot be deleted 70
c:\documents and settings\psr103a Cannot be deleted 70

N.B I was logged in as administrator.

On Error resume next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.CreateTextFile("C:\Documents and settings\cis0794\desktop\DeleteFolderLog.txt")

For i=1 To 1
   
    If i<=9 Then
        dir_path="c:\documents and settings"
    Else
        dir_path="c:\documents and settings"
    End If
   
    Set objfolder=objfso.GetFolder(dir_path)
   
    For Each sf In objfolder.SubFolders
       
        If LCase(Mid(sf.Name,1,3))="psr" Then
            WScript.Echo sf.Path
            Err.Clear
            sf.Delete
            If Err.Number<>0 Then
                objLog.WriteLine dir_path & "\" & sf.Name &" Cannot be deleted " & Err.Number
            Else
                objLog.WriteLine dir_path & "\" & sf.Name &" deleted successfully "
            End If
        End If
    Next
Next

objLog.Close
0
 

Author Comment

by:Bianchi928
ID: 35194695
I have found an option to delete the folder. But nothing is written to the log ..Any idea ?

On Error resume next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.CreateTextFile("C:\Documents and settings\cis0794\desktop\DeleteFolderLog.txt")

For i=1 To 1
   
    If i<=9 Then
        dir_path="c:\documents and settings"
    Else
        dir_path="c:\documents and settings"
    End If
   
    Set objfolder=objfso.GetFolder(dir_path)
   
    For Each sf In objfolder.SubFolders
       
        If LCase(Mid(sf.Name,1,3))="psr" Then
           'WScript.Echo sf.Path
            Err.Clear
            objfso.deletefolder sf,true
           'sf.Delete
            If Err.Number<>0 Then
                objLog.WriteLine dir_path & "\" & sf.Name &" Cannot be deleted " & Err.Number
            Else
                objLog.WriteLine dir_path & "\" & sf.Name &" deleted successfully "
            End If
        End If
    Next
Next

objLog.Close
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35194740
Hi, I'm not sure what the extra loops are for.  Try this.  It will read a list of server names from servers.txt

Regards,

Rob.
strServers = "servers.txt"
strLogFile = "C:\Documents and settings\cis0794\desktop\DeleteFolderLog.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.CreateTextFile(strLogFile, True)

Set objInput = objFSO.OpenTextFile(strServers, 1, False)
While Not objInput.AtEndOfStream
	strServer = objInput.ReadLine
	If Ping(strServer) = True Then
		strFolderPath = "\\" & strServer & "\C$\Documents and Settings\"
		If objFSO.FolderExists(strFolderPath) = True Then
			For Each objSubFolder In objFSO.GetFolder(strFolderPath).SubFolders
				If LCase(Left(objSubFolder.Name, 3)) = "psr" Then
					strSubPath = objSubFolder.Path
					WScript.Echo strSubPath
					On Error Resume Next
					objFSO.DeleteFolder strSubPath, True
					If Err.Number <> 0 Then
						objLog.WriteLine strSubPath & " cannot be deleted. Error " & Err.Number & ": " & Err.Description
					Else
						objLog.WriteLine strSubPath & " deleted successfully"
					End If
				End If
			Next
		Else
			objLog.WriteLine strFolderPath & " does not exist"
		End If
	Else
		objLog.WriteLine strSubPath & " is offline"
	End If
Wend
objInput.Close
objLog.Close
WScript.Echo "Done"

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0
 

Author Comment

by:Bianchi928
ID: 35194829
Hi Rob,

Before I test your last Script..can you have a look at a test that I'm run on the Terminal Servers
It deletes the folders BUT it doesn't write to the log..The only change that I've done to your previous script is..     objfso.deletefolder sf,true
                    'sf.Delete


On Error resume next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.CreateTextFile("C:\Documents and settings\cis0794\desktop\DeleteFolderLog.txt")

For i=1 To 1
   
    If i<=9 Then
        dir_path="\\auprits0"&i&"\c$\documents and settings\"
    Else
        dir_path="\\auprits"&i&"\c$\documents and settings\"
    End If
   
    Set objfolder=objfso.GetFolder(dir_path)
   
    For Each sf In objfolder.SubFolders
       
        If LCase(Mid(sf.Name,1,4))="psr9" Then
            WScript.Echo sf.Path
            Err.Clear
            objfso.deletefolder sf,true
           'sf.Delete
            If Err.Number<>0 Then
                objLog.WriteLine dir_path & "\" & sf.Name &" Cannot be deleted " & Err.Number
            Else
                objLog.WriteLine dir_path & "\" & sf.Name &" deleted successfully "
            End If
        End If
    Next
Next

objLog.Close

Cheers
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35194843
It won't write to the log because you're using sf.Name after it's been deleted.   Change
            objfso.deletefolder sf,true
           'sf.Delete
            If Err.Number<>0 Then
                objLog.WriteLine dir_path & "\" & sf.Name &" Cannot be deleted " & Err.Number
            Else
                objLog.WriteLine dir_path & "\" & sf.Name &" deleted successfully "
            End If

Open in new window


to this:
            strPath = sf.path
            objfso.deletefolder strPath, True
            If Err.Number<>0 Then
                objLog.WriteLine dir_path & "\" & strPath &" Cannot be deleted " & Err.Number
            Else
                objLog.WriteLine dir_path & "\" & strPath &" deleted successfully "
            End If

Open in new window


Regards,

Rob.
0
 

Author Closing Comment

by:Bianchi928
ID: 35194928
All perfect

Thanks
Cheers
0

Featured Post

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!

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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