Skip to content

Posts from the ‘Scripting’ Category

Delete Files/Folders older than specified date


””””””””””””””””””””””””””””””””””””””””””””””””’

‘Created by Mark Torng (NTServices”

‘Created October 17th 2007

‘Some Lines Adopted from Microsoft Scripting Center ””””””””””””””””””””””””””””””””””””””””””””””””’

Option Explicit

On Error Resume Next

 

Dim fso, PathToClean, numberOfDays, folder, rootFolder, objFolder, objSubfolders, objFiles, folderToClean, folderToCheck, fts, foldersToSkip, skippedfolders Set fso = CreateObject(“Scripting.FileSystemObject”)

Set fts = CreateObject(“Scripting.Dictionary”)

  

‘ENTER THE PATH THAT CONTAINS THE FILES YOU WANT TO CLEAN UP

  

‘Path to the root directory that you’re cleaning up PathToClean = “C:\THIS FOLDER”

 

‘ENTER THE NUMBER OF DAYS SINCE THE FILE WAS LAST MODIFIED

 

numberOfDays = 1

   

‘ENTER THE NAMES OF THE FOLDERS YOU DO NOT WANT TO BE DELETED

 

‘ALL FOLDERS WITH THE SPECIFIED NAME WILL NOT BE DELETED.

‘ALL FILES IN THE SPECIFIED FOLDERS WILL NOT BE DELETED.

””””””””””””””””””””””””””””””””””

‘ INSTRUCTIONS TO ADD FOLDERS YOU WISH TO SKIP ‘If you don’t want to save anything use:  foldersToSkip = “”

‘If you want to omit folders:  foldersToSkip = “Folder1;Folder2”

 

foldersToSkip = “”

 

‘Check to make sure path is not a drive root If Right(PathToClean, 2) = “:\” or Right(PathToClean, 1) = “:” Then

      msgbox “Whoa Nelly!  It’s best not to run the Janitor on a drive root like ” + PathToClean, vbOkOnly, “Don’t Do That!”

End If

  

‘Start at the folder specified and walk down the directory tree

 

Set rootFolder = fso.GetFolder(PathToClean) If Err.Number > 0 Then

      msgbox PathToClean + “is not a valid directory path.  Please correct the path and run the script again.”, vbOkOnly, “Path Not Found”

      Wscript.Quit

End If

 

EnumerateFoldersToSkip(foldersToSkip)

GetSubfolders(rootFolder)

CleanupFiles(rootFolder)

Set fso = Nothing

 

Wscript.Quit

 

Sub EnumerateFoldersToSkip(skippedfolders)

      If skippedfolders <> “” Then

            If InStr(1, skippedfolders, “;”, 1) <> 0 Then

                  Dim arrSkippedFolders, sf

                  arrSkippedFolders = Split(skippedfolders, “;”)

                  For each sf In arrSkippedFolders

                        fts.Add UCase(Trim(sf)), “”

                  Next

            Else

                  fts.Add UCase(skippedfolders), “”

            End If

      End If

End Sub

 

Sub GetSubfolders(folder)

      If CheckForSkip(folder) Then Exit Sub

 

      Dim oSubfolder

      Set objFolder = fso.GetFolder(folder)

      Set objSubfolders = objFolder.Subfolders

 

      For Each oSubfolder in objSubfolders

            If fts.Exists(UCase(oSubfolder.Name)) = False Then

                  ‘Recursively go down the directory tree

                  GetSubfolders(oSubfolder.Path)

     

                  ‘Cleanup any files that meet the criteria

                  CleanupFiles(oSubfolder.Path)

     

                  ‘Delete the folder if its empty

                  CleanupFolder(oSubfolder.Path)     

            End If

      Next

End Sub

 

Sub CleanupFiles(folderToClean)

      If CheckForSkip(folderToClean) Then Exit Sub

 

      dim objFile

      Set objFolder = fso.GetFolder(folderToClean)

      Set objFiles = objFolder.Files

 

      For Each objFile in objFiles

            If DateDiff(“d”, objFile.DateLastModified, Now) > numberOfDays Then

                  objFile.Delete

            End If

      Next

 

      Set objFolder = Nothing

      Set objFiles = Nothing

End Sub

 

Sub CleanupFolder(folderToCheck)

      If CheckForSkip(folderToCheck) Then Exit Sub

 

      Set objFolder = fso.GetFolder(folderToCheck)

      Set objSubfolders = objFolder.Subfolders

      Set objFiles = objFolder.Files

 

      If objFiles.Count = 0 and objSubfolders.Count = 0 Then

            objFolder.Delete

      End If

 

      Set objFolder = Nothing

      Set objSubfolders = Nothing

      Set objFiles = Nothing

End Sub

 

Function CheckForSkip(folderToCheck)

                CheckForSkip = fso.FileExists(folderToCheck & “\JANITOR.SKIP”) End Function

Defrag Script for all Fixed Disks


Defragging your system is an EXTREMELY important thing to do. A slow performing disk is the number one killer of overall system performance. In my opinion a “slow” machine is due to POOR disk performance. 

DON’T LET THIS HAPPEN TO YOU!!!

Defrag

‘Mark Torng DEFRAG all fixed disks with VBScript

Const LogFile = “C:\Defrag.log”

Const DriveTypeRemovable = 1
Const DriveTypeFixed = 2
Const DriveTypeNetwork = 3
Const DriveTypeCDROM = 4
Const DriveTypeRAMDisk = 5

Set FSO = CreateObject(“Scripting.FileSystemObject”)
Set Drives = FSO.Drives

For each Drive in Drives
 if Drive.DriveType = 2 then
  RunCmd Drive
 end if
Next

Sub RunCmd(DriveString)
Set WshShell = WScript.CreateObject(“WScript.Shell”)
RunString = “%comspec% /c echo ” & WeekDayName(WeekDay(Now), True) & ” ” & Now & ” ” & DriveString
Return = WshShell.Run(RunString & ” >> ” & LogFile & ” 2>&1″, 0, TRUE)
RunString = “%comspec% /c %WINDIR%\System32\Defrag.exe ” & DriveString & ” -f”
Return = WshShell.Run(RunString & ” >> ” & LogFile & ” 2>&1″, 0, TRUE)
Set WshShell = Nothing
End Sub

As always, you must use the visual basic script extention.

Copy the above, paste into a notepad, and save the file as whateveryouwant.vbs

Removing Network printers with VBScript


Another easy one for you… if you should ever need to remove network printers that are installed on a machine… this is a great way to do so! batch it out, convert it to an executable.. it’s just a great tool!

‘Start Script

Dim net
Set net = CreateObject(“WScript.Network”)
net.AddWindowsPrinterConnection “
\\Server\PrintShare0

net.AddWindowsPrinterConnection “\\Server\PrintShare1
net.AddWindowsPrinterConnection “
\\Server\PrintShare2
net.AddWindowsPrinterConnection “
\\Server\PrintShare3
net.AddWindowsPrinterConnection “
\\Server\PrintShare4
net.AddWindowsPrinterConnection “
\\Server\PrintShare5
net.SetDefaultPrinter “\\Server\PrintShare3

‘End Script

As always, you must use the visual basic script extention.

Copy the above, paste into a notepad, and save the file as whateveryouwant.vbs

Adding printers with VBScript


Pretty easy stuff… if you should ever need to add a login script to map a printer, or batch out a very large printer install… even convert to an EXE and push out a printer change to hundreds of systems with System Management Server.. this is a great way to do so!

‘Start Script

Dim net
Set net = CreateObject(“WScript.Network”)
net.AddWindowsPrinterConnection “
\\Server\PrintShare0

net.AddWindowsPrinterConnection “\\Server\PrintShare1
net.AddWindowsPrinterConnection “
\\Server\PrintShare2
net.AddWindowsPrinterConnection “
\\Server\PrintShare3
net.AddWindowsPrinterConnection “
\\Server\PrintShare4
net.AddWindowsPrinterConnection “
\\Server\PrintShare5
net.SetDefaultPrinter “
\\Server\PrintShare3

‘End Script

As always, you must use the visual basic script extention.

Copy the above, paste into a notepad, and save the file as whateveryouwant.vbs