VB Script to Delete Old Backup Files

VB Script to Delete Old Backup Files.

I have created VB Script which deletes 7 days old file in Folder and Sub Folder.

iDaysOld = 7
strPath = "C:\BACKUP"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)
Set colSubfolders = objFolder.Subfolders
Set colFiles = objFolder.Files

For Each objFile in colFiles
   If objFile.DateLastModified < (Date() - iDaysOld) Then
       objFile.Delete
   End If
Next

For Each objSubfolder in colSubfolders
   Set colFiles = objSubfolder.Files
   For Each objFile in colFiles
       If objFile.DateLastModified < (Date() - iDaysOld) Then
           objFile.Delete
       End If
   Next
Next

Regards,
Nirav Gajjar

VB Scirpt to get Folder Size

VB Scirpt to get Folder Size.

VB Script to get Folder Property.


dim oFS, oFolder
set oFS = WScript.CreateObject("Scripting.FileSystemObject")
set oFolder = oFS.GetFolder("D:\Nirav's Diary")

ShowFolderDetails oFolder

sub ShowFolderDetails(oF)
dim F
    wscript.echo oF.Name & ":Size=" & oF.Size/1024.00/1024.00 &"MB"
    wscript.echo oF.Name & ":Files=" & oF.Files.Count
    wscript.echo oF.Name & ":Folders=" & oF.Subfolders.count
    wscript.echo oF.Name & ":Modified Date=" & oF.DateLastModified
    wscript.echo oF.Name & ":Modified Date=" & oF.DateCreated
    for each F in oF.Subfolders
        ShowFolderDetails(F)
    next
end sub

Regards,

Nirav Gajjar

VB Script to Reboot Server

VB Script to Reboot Server.

Reboot server and send information mail to user.


SendMail "alerts@extreame-advice.com","nirav@extreame-advice.com", "Server Restarted at" & Now(), "Server Restarted"

Dim objShell
Set objShell = WScript.CreateObject ("WScript.shell")
objShell.run "Shutdown /r /c “00:15” /t 30"
Set objShell = Nothing

Sub SendMail(Sender, Recipient, Subject, Message)
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = Subject
objMessage.From = Sender
objMessage.To = Recipient
objMessage.htmlbody = Message
objMessage.Send
End Sub

Regards,

Nirav Gajjar

VB Script to get list of Network Computers

Vb Script to get list of Network Computers.

Create Table ListOfServers
(
	ServerName Varchar(20),
	DomainName Varchar(50),
	CreatedDate DateTime
)

 

'On error resume next
Const DB_CONNECT_STRING = "Provider=SQLOLEDB.1;Data Source=ExtreamAdvice;Initial Catalog=Nirav's Diary;user id ='sa';password='password'"
Set myConn = CreateObject("ADODB.Connection")
Set myCommand = CreateObject("ADODB.Command" )
myConn.Open DB_CONNECT_STRING
Set myCommand.ActiveConnection = myConn

Const ADS_SCOPE_SUBTREE = 2
strDomainDn = "DomainName"

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
    "Select Name, Location from 'LDAP://" & strDomainDn & "' " _
        & "Where objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    strComputer = objRecordSet.Fields("Name").Value
    myCommand.CommandText = "Insert into ListOfServers (ServerName, DomainName,CreatedDate) values('" & strComputer & "','" & strDomainDn & "',getdate())"
    myCommand.Execute
    objRecordSet.MoveNext
Loop
myConn.Close

WScript.Echo "Done!"

Regards,
Nirav Gajjar