Solved

Delete Folders

Posted on 2011-03-21
9
493 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
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
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.

919 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

14 Experts available now in Live!

Get 1:1 Help Now