?
Solved

Delete Folders

Posted on 2011-03-21
9
Medium Priority
?
525 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 2000 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

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.

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

649 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