Alright, I'm having a problem with a scheduled task not running. At first it wouldn't run the vbs, so I created a bat to run it and then it would work, but only when the user is logged in. I went into the local security policy and made sure my account was in, "log on as batch job" group. When I assign it to then run whether logged in or not, it fails to run. It gets caught up somewhere, without throwing any error codes, but not creating the Excel spreadsheet.

If anyone has any ideas, I'm open to it. Here is what the code looks like.


cscript C:\delete_backup.vbs


' delete_backup.vbs
' ***************************************************
' * This script will delete all files in a folder and
' * sub-folder where when files are x number of days
' * old.
' *
' * Files will be deleted and not retained in the 
' * recycle bin.
' *
' * Alter the path and lifetime variables to your needs

' * Alter this variable, this is the starting point
' * for the deletion.
' * Example : path = "\\teacher\backup" 
' * Example : path = "N:\Logfiles" 

Path = "C:\siouxfallsserv backup\backup\" 

' * Alter this variable, if sub-folders should be processed
' * Process Sub folders Example : Subfolders = True 
' * Process Sub folders Example : Subfolders = False 

Subfolders = True
' * Alter this variable to set the how many days old
' * the file should be before it is deleted.

' * Example : lifetime = date() - 20
' *           Will delete files that are 20 days old or more

Lifetime = date() - 15 

' *******************************************************
' Script starts at this point
' *******************************************************
' Declare a variable as an Array that will store a listing
' of files that will be checked.

FilesArray = Array() 

' Create an instance of the FileSystemObject so that file
' information can be obtained.

set fso = createobject("scripting.filesystemobject") 

' Call the SelectFiles procedure to Fill the array with 
' files and folders that will be deleted

SelectFiles path, lifetime, FilesArray, Subfolders

' Process the FilesArray deleting files as we loop through

numDeleted = 0 

for n = 0 to ubound(FilesArray)
    ' Switch off error checking, so that errors are ignored
    on error resume next
    ' Call the delete function to delete the selected file
    FilesArray(n).delete true 
    ' Handle any errors or results.  This could be modified
    ' to log to a text file.
    'if err.number <> 0 then 
    '    wscript.echo "Unable to delete: " & FilesArray(n).path 
    '    numDeleted = numDeleted + 1 
    'end if 
    ' Switch Error checking back to normal
    on error goto 0 

sub SelectFiles(sPath,vlifetime,FilesArrayToKill,bIncludeSubFolders) 
    ' Switch off Error handling, errors ignored.
    on error resume next 
    'select files to delete and add to array... 
    set folder = fso.getfolder(sPath) 
    set files  = folder.files 

    ' Loop through files that have been found
    for each file in files 
        ' uses error trapping around access to the 
        ' Date property just to be safe 

        dtlastmodified = null
        on error resume Next 
        dtlastmodified = file.datelastmodified 
        on error goto 0 

        if not isnull(dtlastmodified) Then 
            if dtlastmodified < vlifetime then 
                count = ubound(FilesArrayToKill) + 1 
                redim preserve FilesArrayToKill(count) 
                set FilesArrayToKill(count) = file 
            end if 
        end if 

    ' If sub-folders are selected, call the procedure again to update
    ' the array with the contents.

    if bIncludeSubFolders then 
        for each fldr in folder.subfolders 
            SelectFiles fldr.path,vlifetime,FilesArrayToKill,true 
    end if 
end sub

'Create Excel Doc 
Dim oFS, oFolder
Dim objexcel, r, lnameArray, lname, nameLength
set oFS = WScript.CreateObject("Scripting.FileSystemObject")
set oFolder = oFS.GetFolder("C:\siouxfallsserv backup\backup\")

Set objExcel = createobject("Excel.application")  
Set objWorkbook = objExcel.Workbooks.Add()
Set objWorksheet = objWorkbook.Worksheets(1)
objexcel.Cells(1, 1).Value = "Folder Name"
objexcel.Cells(1, 2).Value = "Size (MB)"
objexcel.Cells(1, 3).Value = "# Files"
objexcel.Cells(1, 4).Value = "# Sub Folders"
objexcel.Cells(1, 5).Value = Date()

objexcel.Visible = True
Wscript.Sleep 300

ShowFolderDetails oFolder, r
objexcel.DisplayAlerts = False 
objexcel.ActiveWorkbook.SaveAs("C:\Documents and Settings\Frank\Documents\backup_report.xls")
Function ShowFolderDetails(oF,r)
    Dim F
    objexcel.Cells(r, 1).Value = oF.Name
    objexcel.Cells(r, 2).Value = oF.Size /1024\1024
    objexcel.Cells(r, 3).Value =  oF.Files.Count
    objexcel.Cells(r, 4).Value =  oF.Subfolders.count
    r = r+1
    for each F in oF.Subfolders
        ShowFolderDetails F, r
End Function
6 Years
Discussion Span
Last Post by atrueresistance

If I am not wrong then a batch file can only run when it is called (explicitly by a user). This must be the reason why the scheduled task not able to run when user isn't logged.


Have you tried converting the program to a process so it runs without a logged on account? I'm sure there was a tool out there somewhere that would do the conversion for you. I used it to run a VCN client on a server when there was no one in the office.


I tried converting it to an .exe via ScriptCryptor Compiler, but no luck. The compiled script performs as I programmed it, but same issue as the .vbs script not wanting to run the scheduled task. Thanks for the idea ljdellar, it was worth a try.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.