Renegade Public Forums
C&C: Renegade --> Dying since 2003™, resurrected in 2024!
Home » General Discussions » General Discussion » vbscript help needed
vbscript help needed [message #413590] Wed, 09 December 2009 07:41
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma:
General (3 Stars)
I'm really not very interested in vbscript at all, but I need to change an existing script I butchered together so that I can delete any subfolder (subfolders of subfolders of subfolders etc etc etc) where the folder contains no files.
I'm already deleting the files themselves if they are over x days old, but it leaves the folders there...

A seperate loop to run afterwaprs is fine, but would prefer to delete this folders that the files are being deleted out of as I go along in the current loop if possible.

If you can help, I would appreciate it.



' VBscript to delete files in a particular directory after seven days
' Only deletes files that have not been modified and accessed in seven days




''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''' Settings for configuration '''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' This setting defines where the log file is placed.
Wheretoplacelogfile = "c:\test\log"
' This setting defines how many days the file needs to of been un-modified and not accessed before deletion
NumberOfDays = -7
' This setting defines what folder needs to be cleaned up
FolderForCleaning = "c:\fake"


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''' End of settings '''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

LocalDate = Day(Now()) & "-" & Month(Now()) & "-" & Year(Now())
LocalTime = right("00" & Hour(NOW()), 2) & ":" & right("00" & Minute(NOW()), 2) & ":" & right("00" & Second(NOW()), 2)
Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
Wheretoplacelogfile = Wheretoplacelogfile & "Deletion Log File created by " & objNetwork.UserName & " " & LocalDate & ".txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set FileWriter = fso.CreateTextFile(Wheretoplacelogfile , True)
FileWriter.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
FileWriter.WriteLine("Clean-up log file")
FileWriter.WriteLine("Generated on the " & LocalDate & " at " & LocalTime)
FileWriter.WriteLine("Log file generated by " & objNetwork.UserName & " using computer name " & objNetwork.ComputerName & " on the " & objNetwork.UserDomain & " domain" )
FileWriter.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
FileWriter.WriteLine(" ")
FileWriter.WriteLine(" ")
FileWriter.WriteLine(" ")
FileWriter.WriteLine(" ")
FileWriter.WriteLine(" ")


Dim Queue
Queue = FolderForCleaning & ";"

     While Queue <> ""
            Dim Path
			Path = Split(Queue,";")(0)
			
			
			Queue = Right(Queue,Len(Queue) - Len(Path)-1)


     Dim oFSO, oFolder, oFiles, oFile, OSubFolders
        set oFSO = CreateObject("Scripting.FileSystemObject")
        set oFolder = oFSO.GetFolder(Path)
        set oFiles = oFolder.Files
		set oSubFolders = oFolder.SubFolders
		For Each oSubFolder in OSubFolders
		'Note : Only use *lowercase* letters in the folder names below:
		If Not LCase(oSubFolder.Name) = "do not delete this folder contents" And Not LCase(oSubFolder.Name) = "my special folder" Then
			Queue = Queue & oSubFolder & ";"
			End if
		Next
		
		For Each oFile In oFiles
            On Error Resume Next
            If (oFile.DateLastModified < DateAdd("d", NumberOfDays, Now())) Then
            FileWriter.WriteLine("File: " & oFile.Name)
            FileWriter.WriteLine("File located: " & oFile.Path)
            FileWriter.WriteLine("The file size is: " & oFile.Size & "Bytes" & "  and is a " & oFile.Type)
            FileWriter.WriteLine("Last modified: " & oFile.DateLastModified)
            FileWriter.WriteLine("File confirmed for deletion...")
            FileWriter.WriteLine("Deleting file...")
            oFile.Delete(True)
            FileWriter.WriteLine("The file has now been deleted ")
            FileWriter.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
       		FileWriter.WriteLine(" ")
       		FileWriter.WriteLine(" ")
       		End If
       
        Next

     Wend




' Probably never going to happen, but best to just check the data again incase it took longer then a day, lol...
LocalDate = Day(Now()) & "-" & Month(Now()) & "-" & Year(Now())
LocalTime = right("00" & Hour(NOW()), 2) & ":" & right("00" & Minute(NOW()), 2) & ":" & right("00" & Second(NOW()), 2)
FileWriter.WriteLine(" ")
FileWriter.WriteLine(" ")
FileWriter.WriteLine(" ")
FileWriter.WriteLine(" ")
FileWriter.WriteLine(" ")
FileWriter.WriteLine(" ")
FileWriter.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
FileWriter.WriteLine("Completed on the " & LocalDate & " at " & LocalTime)
FileWriter.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
FileWriter.Close


Set objMessage = CreateObject("CDO.Message") 

objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = sendusingnnum

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smpthub"

'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = someauthtypehere

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = some number

'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = numhere

objMessage.Configuration.Fields.Update


' Was going to email myself the entire log file in the form of the emails body, but screw that...
'These constants are defined to make the code more readable
'Const ForReading = 1, ForWriting = 2, ForAppending = 8
'Dim fso, f
'Set fso = CreateObject("Scripting.FileSystemObject")
'Open the file for reading
'Set f = fso.OpenTextFile(Wheretoplacelogfile, ForReading)
'The ReadAll method reads the entire file into the variable BodyText
'BodyText = f.ReadAll
'Close the file
'f.Close
'Set f = Nothing
'Set fso = Nothing 



objMessage.Subject = "Logging for the shared drive"
objMessage.From = "fake@fake.com" 
objMessage.To = "spencer-e.elliott@fake.com" 
objMessage.CC = "somethotherdude@fake.com"
'objMessage.TextBody = BodyText
'objMessage.TextBody = Wheretoplacefile
objMessage.HTMLBody = vbCrLf & "<a href='file://\\myservernamehere\log files\" & "Deletion Log File created by " & objNetwork.UserName & " " & LocalDate & ".txt" & "'>Clickhere to view the log file for the drive</a>"
objMessage.Send




[Updated on: Wed, 09 December 2009 07:42]

Report message to a moderator

 
Read Message
Previous Topic: Game-Maps Staff Position
Next Topic: Recyle your old PC
Goto Forum:
  


Current Time: Sun Nov 03 00:59:19 MST 2024

Total time taken to generate the page: 0.00517 seconds