?
Solved

Delete Folders

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

801 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