none
Deletion of temp folders on c drive

    Question

  • How to delete c:\temp and c:\windows\temp contents when c drive reaches to 20% using the vb script ?
    rajendra prasad.Anumolu
    Thursday, October 20, 2011 3:07 PM

Answers

  • Hi  every one,

    I Prepared script for above issue,This script will delete  some space on C drive.please find the paths where we can delete contents for free space

    C:\Temp

    C:\windows\Temp

    Recycle bin

    .tmp,.chk,.dmp files......................................Please find the script below.

     

    '******************** CLEAR THE SPACE ON C DRIVE WHEN IT REACHES TO THRESHOLD SIZE****************

     

    '********************* DELETES CONTENTS ON C:\TEMP  ***********************************
    On error resume Next
    Dim perFree


    Dim fso, startFolder, OlderThanDate
    'Dim fso, startFolder, OlderThanDate

    Set fso = CreateObject("Scripting.FileSystemObject")
    If perFree < 20  Then

    startFolder = "C:\temp"         ' folder to start deleting (subfolders will also be cleaned)


    OlderThanDate = DateAdd("d", -1, Date)  ' 01 day (adjust as necessary)

    DeleteOldFiles startFolder, OlderThanDate

    Function DeleteOldFiles(folderName, BeforeDate)

       Dim folder, file, fileCollection, folderCollection, subFolder

       Set folder = fso.GetFolder(folderName)

       Set fileCollection = folder.Files

       For Each file In fileCollection

          If file.DateLastModified < BeforeDate Then

             fso.DeleteFile(file.Path)

          End If

       Next

         Set folderCollection = folder.SubFolders

        For Each subFolder In folderCollection

           DeleteOldFiles subFolder.Path, BeforeDate

       Next


    End function

    '********************* DELETING THE CONTENTS ON C:\WINDOWS\TEMP\ ************************

    Set fso = CreateObject("Scripting.FileSystemObject")
    If perFree < 20  Then

    startFolder = "C:\windows\temp"         ' folder to start deleting (subfolders will also be cleaned)


    OlderThanDate = DateAdd("d", -1, Date)  ' 01 days (adjust as necessary)

    DeleteOldFiles startFolder, OlderThanDate

    Function DeleteOldFiles(folderName, BeforeDate)

       Dim folder, file, fileCollection, folderCollection, subFolder

       Set folder = fso.GetFolder(folderName)

       Set fileCollection = folder.Files

       For Each file In fileCollection

          If file.DateLastModified < BeforeDate Then

             fso.DeleteFile(file.Path)

          End If

       Next

         Set folderCollection = folder.SubFolders

        For Each subFolder In folderCollection

           DeleteOldFiles subFolder.Path, BeforeDate

       Next


    End function

    '***********************DELETES THE .TMP,.CHK,.DMP EXTENSION FILES ************************
    DIM strExtensionsToDelete,strFolder
    DIM objFSO

    ' Folder to delete files from (files will also be deleted from subfolders)
    strFolder = "C:\"
    ' A comma separated list of file extensions
    ' Files with extensions provided in the list below will be deleted
    strExtensionsToDelete = "tmp,chk,dmp"

    ' *****************************************************************************************

    set objFSO = createobject("Scripting.FileSystemObject")

    RecursiveDeleteByExtension strFolder,strExtensionsToDelete

    'wscript.echo "Finished"

    sub RecursiveDeleteByExtension(byval strDirectory,strExtensionsToDelete)
     DIM objFolder, objSubFolder, objFile
     DIM strExt

     set objFolder = objFSO.GetFolder(strDirectory)
     for each objFile in objFolder.Files
      for each strExt in SPLIT(UCASE(strExtensionsToDelete),",")
       if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
        'wscript.echo "Deleting:" & objFile.Path
        objFile.Delete
        exit for
       end if
      next
     next 
     for each objSubFolder in objFolder.SubFolders
      RecursiveDeleteByExtension objSubFolder.Path,strExtensionsToDelete
     next
    End sub


    '********************* DELETES THE LOG FILES ON C:\WINDOWS\SYSTEM32\LOGFILES\.....**************
     
       Dim oFS, aFdrList, sFdr, iKeepDays, sFileExt, dToDay, dCutOff, oFdr, oFile

       Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
        aFdrList = Array("C:\WINDOWS\system32\LogFiles\W3SVC1")
     
       iKeepDays = 90
     
       sFileExt="log"
       dToDay = Date()
      dCutOff = CDate(dToDay - iKeepDays)
     
       For Each sFdr In aFdrList
         Set oFdr = oFS.GetFolder(sFdr)
        For Each oFile in oFdr.Files
           Err.Clear
           On Error Resume Next
            If (UCase(oFS.GetExtensionName(oFile.Name)) = UCase(sFileExt) And IsOlder(oFile, dCutOff)) Then oFile.Delete True
           On Error goto 0
         Next
      Next
     
       Set oFS = Nothing
       WScript.Quit
     '---------------------
       Function IsOlder(oFile, dCutOff)
         If oFile.DateCreated < dCutOff Then IsOlder = True Else IsOlder = False
         'If oFile.DateLastModified < dCutOff Then IsOlder = True Else IsOlder = False
       End Function


    End if
    End if

    '***************************** EMPTY THE RECYCLE BIN ***********************
        Const RECYCLE_BIN = &Ha&
        Const FILE_SIZE = 3
       
        '#--------------------------------------------------------------------------
        '#  Declare Variables
        '#--------------------------------------------------------------------------
        Dim objShell, objFolder, colItems
        Set objShell = CreateObject("Shell.Application")
        Set objFolder = objShell.Namespace(RECYCLE_BIN)
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set colItems = objFolder.Items
       
        '#--------------------------------------------------------------------------
        '#  Delete everything in the Recycle Bin
        '#--------------------------------------------------------------------------
        For Each objItem in colItems
            If (objItem.Type = "File folder") Then
                objFSO.DeleteFolder(objItem.Path)

            Else
                objFSO.DeleteFile(objItem.Path)
            End If
        Next

     

     


    rajendra prasad.Anumolu
    Friday, October 21, 2011 4:52 PM

All replies

  • You will obtain more quickly answer in scripting forum. See here  http://social.technet.microsoft.com/Forums/en-US/ITCG/threads

    Consider alternatively using Powershell. Appropriate PS forum is here  http://social.technet.microsoft.com/Forums/en/winserverpowershell/threads

    You need to create a cycle, where you measure the size of directory and size of all files on disc and check the condition. Then you delete the content of TEMP. I am not sure,if it is possible in any case. My guess is that there are some files used by running processes and the deletition may fail.

    A more elaborate PS script should check, if the files to be deleted are used by running processes and does not touch afected files when deleting others "free" ones.

    Regards

    Milos

     


    Thursday, October 20, 2011 3:25 PM
  • open a note pad.type below commands

    cd c:\users\%username%\appdata\local
    rmdir /s /q temp

     

    save the file as tempdel.bat.

    using shedules task you can run this bat file.

    but this is delete the particular users temp files only

     


    Darshana Jayathilake
    Thursday, October 20, 2011 4:15 PM
  • Hi  every one,

    I Prepared script for above issue,This script will delete  some space on C drive.please find the paths where we can delete contents for free space

    C:\Temp

    C:\windows\Temp

    Recycle bin

    .tmp,.chk,.dmp files......................................Please find the script below.

     

    '******************** CLEAR THE SPACE ON C DRIVE WHEN IT REACHES TO THRESHOLD SIZE****************

     

    '********************* DELETES CONTENTS ON C:\TEMP  ***********************************
    On error resume Next
    Dim perFree


    Dim fso, startFolder, OlderThanDate
    'Dim fso, startFolder, OlderThanDate

    Set fso = CreateObject("Scripting.FileSystemObject")
    If perFree < 20  Then

    startFolder = "C:\temp"         ' folder to start deleting (subfolders will also be cleaned)


    OlderThanDate = DateAdd("d", -1, Date)  ' 01 day (adjust as necessary)

    DeleteOldFiles startFolder, OlderThanDate

    Function DeleteOldFiles(folderName, BeforeDate)

       Dim folder, file, fileCollection, folderCollection, subFolder

       Set folder = fso.GetFolder(folderName)

       Set fileCollection = folder.Files

       For Each file In fileCollection

          If file.DateLastModified < BeforeDate Then

             fso.DeleteFile(file.Path)

          End If

       Next

         Set folderCollection = folder.SubFolders

        For Each subFolder In folderCollection

           DeleteOldFiles subFolder.Path, BeforeDate

       Next


    End function

    '********************* DELETING THE CONTENTS ON C:\WINDOWS\TEMP\ ************************

    Set fso = CreateObject("Scripting.FileSystemObject")
    If perFree < 20  Then

    startFolder = "C:\windows\temp"         ' folder to start deleting (subfolders will also be cleaned)


    OlderThanDate = DateAdd("d", -1, Date)  ' 01 days (adjust as necessary)

    DeleteOldFiles startFolder, OlderThanDate

    Function DeleteOldFiles(folderName, BeforeDate)

       Dim folder, file, fileCollection, folderCollection, subFolder

       Set folder = fso.GetFolder(folderName)

       Set fileCollection = folder.Files

       For Each file In fileCollection

          If file.DateLastModified < BeforeDate Then

             fso.DeleteFile(file.Path)

          End If

       Next

         Set folderCollection = folder.SubFolders

        For Each subFolder In folderCollection

           DeleteOldFiles subFolder.Path, BeforeDate

       Next


    End function

    '***********************DELETES THE .TMP,.CHK,.DMP EXTENSION FILES ************************
    DIM strExtensionsToDelete,strFolder
    DIM objFSO

    ' Folder to delete files from (files will also be deleted from subfolders)
    strFolder = "C:\"
    ' A comma separated list of file extensions
    ' Files with extensions provided in the list below will be deleted
    strExtensionsToDelete = "tmp,chk,dmp"

    ' *****************************************************************************************

    set objFSO = createobject("Scripting.FileSystemObject")

    RecursiveDeleteByExtension strFolder,strExtensionsToDelete

    'wscript.echo "Finished"

    sub RecursiveDeleteByExtension(byval strDirectory,strExtensionsToDelete)
     DIM objFolder, objSubFolder, objFile
     DIM strExt

     set objFolder = objFSO.GetFolder(strDirectory)
     for each objFile in objFolder.Files
      for each strExt in SPLIT(UCASE(strExtensionsToDelete),",")
       if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
        'wscript.echo "Deleting:" & objFile.Path
        objFile.Delete
        exit for
       end if
      next
     next 
     for each objSubFolder in objFolder.SubFolders
      RecursiveDeleteByExtension objSubFolder.Path,strExtensionsToDelete
     next
    End sub


    '********************* DELETES THE LOG FILES ON C:\WINDOWS\SYSTEM32\LOGFILES\.....**************
     
       Dim oFS, aFdrList, sFdr, iKeepDays, sFileExt, dToDay, dCutOff, oFdr, oFile

       Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
        aFdrList = Array("C:\WINDOWS\system32\LogFiles\W3SVC1")
     
       iKeepDays = 90
     
       sFileExt="log"
       dToDay = Date()
      dCutOff = CDate(dToDay - iKeepDays)
     
       For Each sFdr In aFdrList
         Set oFdr = oFS.GetFolder(sFdr)
        For Each oFile in oFdr.Files
           Err.Clear
           On Error Resume Next
            If (UCase(oFS.GetExtensionName(oFile.Name)) = UCase(sFileExt) And IsOlder(oFile, dCutOff)) Then oFile.Delete True
           On Error goto 0
         Next
      Next
     
       Set oFS = Nothing
       WScript.Quit
     '---------------------
       Function IsOlder(oFile, dCutOff)
         If oFile.DateCreated < dCutOff Then IsOlder = True Else IsOlder = False
         'If oFile.DateLastModified < dCutOff Then IsOlder = True Else IsOlder = False
       End Function


    End if
    End if

    '***************************** EMPTY THE RECYCLE BIN ***********************
        Const RECYCLE_BIN = &Ha&
        Const FILE_SIZE = 3
       
        '#--------------------------------------------------------------------------
        '#  Declare Variables
        '#--------------------------------------------------------------------------
        Dim objShell, objFolder, colItems
        Set objShell = CreateObject("Shell.Application")
        Set objFolder = objShell.Namespace(RECYCLE_BIN)
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set colItems = objFolder.Items
       
        '#--------------------------------------------------------------------------
        '#  Delete everything in the Recycle Bin
        '#--------------------------------------------------------------------------
        For Each objItem in colItems
            If (objItem.Type = "File folder") Then
                objFSO.DeleteFolder(objItem.Path)

            Else
                objFSO.DeleteFile(objItem.Path)
            End If
        Next

     

     


    rajendra prasad.Anumolu
    Friday, October 21, 2011 4:52 PM