[vbscript] Clean Temp Directory at start

Discussion in 'Scripting' started by Drade, Apr 5, 2011.

  1. Drade

    Drade MDL Novice

    Mar 20, 2011
    7
    0
    0
    #1 Drade, Apr 5, 2011
    Last edited by a moderator: Apr 20, 2017
    Hi I have copied this vbscript from internet in order to clean various directory usually on start of windows, but the folder %SystemRoot%\Temp has not been cleaned. When i wanted to open it to see it it has been cleaned, it prompts me to permit me as administrator neverthless my username is administrator. Here is the vbscript

    Code:
    'Declare variables
    Dim objFSO, wsShell
    Dim strTemp, strTempFolder, strFile, strColSubfolders, strSubFolder
    Dim strInternetFolder, strInternet
    
    'Set up environment
    Set wsShell = CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Windir = WSHShell.ExpandEnvironmentStrings("%windir%") 
    
    'start deleting files
    strInternet = wsShell.ExpandEnvironmentStrings("%LocalAppData%")
    Set strInternetFolder = objFSO.GetFolder(strInternet & "\Microsoft\Windows\Temporary Internet Files")
     For Each strFile In strInternetFolder.files
        strFile.Delete True
     Next
    'Delete folders and subfolders
    Set strColSubfolders = strInternetFolder.SubFolders
    On Error Resume Next
    For Each strSubFolder in strColSubfolders
        objFSO.DeleteFolder(strSubFolder), True
    Next
    
    strTemp = wsShell.ExpandEnvironmentStrings("%Temp%")
    Set strTempFolder = objFSO.GetFolder(strTemp)
     For Each strFile In strTempFolder.files
        strFile.Delete True
     Next
    'Delete folders and subfolders
    Set strColSubfolders = strTempFolder.SubFolders
    On Error Resume Next
    For Each strSubFolder in strColSubfolders
        objFSO.DeleteFolder(strSubFolder), True
    Next
    
    strTemp = wsShell.ExpandEnvironmentStrings("%SystemRoot%")
    Set strTempFolder = objFSO.GetFolder(strTemp & "\Temp")
     For Each strFile In strTempFolder.files
        strFile.Delete True
     Next
    'Delete folders and subfolders
    Set strColSubfolders = strTempFolder.SubFolders
    On Error Resume Next
    For Each strSubFolder in strColSubfolders
        objFSO.DeleteFolder(strSubFolder), True
    Next
    
    'Clear memory
    Set objFSO = Nothing
    Set wsShell = Nothing
    Set strTempFolder = Nothing
    Set strFile = Nothing
    Set strColSubfolders = Nothing
    Set strSubFolder = Nothing
    Set strUserProfile = Nothing
    
    WScript.Quit
    
    This file is executed during the start of windows with this batch:
    @echo off
    Schtasks /Create /Tn "DeleteTempFolder " /Tr "%SystemRoot%\System32\DeleteTemp.vbs" /Sc onstart /Ru "" >NUL
    Exit
     
  2. Compo

    Compo MDL Member

    Mar 29, 2011
    136
    106
    10
    #2 Compo, Apr 10, 2011
    Last edited by a moderator: Apr 20, 2017
    Technically the file is not executed by the batch file, the batch file was only used once to create the Scheduled Task. The task is executed by the task scheduler from that point onwards.

    I would see no point whatsoever in deleting the content of the system temp directory and would prefer to see it removed from the script entirely. I would also prefer to schedule the cleaning task to be other than 'onstart' for a couple of reasons. Firstly machines left in sleeping modes as opposed to shut down would not execute the task and secondly there really isn't that much stored in temporary locations to warrant removal so frequently.

    My suggestions if you were to still want to delete the system temp directory would be to enter this at the command prompt
    Code:
    schtasks /change /tn "DeleteTempFolder" /sc weekly /mo 2 /rl HIGHEST
    Schtasks.exe should change the task accordingly and inform you of its success or failure.

    If you want to test it straight away, you could use:
    Code:
    schtasks /run /tn "DeleteTempFolder"
    You should get a brief message and can check for errors either in %systemroot%\SchedLgU.txt or of course by checking the content of the Temp directories specified in the VBScript.
     
  3. Drade

    Drade MDL Novice

    Mar 20, 2011
    7
    0
    0
    I am sorry. i was out for work.
    When i used the sentence
    to try the script, it does tell me that the system does not find the specified file. I donĀ“t understand why as it exists?