Solved

Delete Folders

Posted on 2011-03-21
9
484 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
  • 5
  • 3
9 Comments
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Bianchi928
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Bianchi928
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
All perfect

Thanks
Cheers
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now