Deleting files older than 6 hours

Home Forums Scripting General Scripting Deleting files older than 6 hours

This topic contains 28 replies, has 7 voices, and was last updated by Avatar lazergeek 8 years, 2 months ago.

Viewing 29 posts - 1 through 29 (of 29 total)
  • Author
    Posts
  • Avatar
    krokkie
    Member
    #156160

    I would like to write a script that can be set up as a Scheduled Task to delete files older than 7 hours from a few folders
    The folders are:
    D:SDMAPPDomInfoPlans
    D:SDMAPPDomInfoHourly Metars
    D:SDMAPPDomInfoHourly Tafs
    D:SDMAPPIntlInfoPlans
    D:SDMAPPIntlInfoHourly Metars
    D:SDMAPPIntlInfoHourly Tafs
    D:SDMAPPIntlInfoNotam Briefs
    D:SDMAPPIntlInfoWx Briefs

    I would also like to log how many files are deleted from each folder.

    I found this vbscript through Google, and am not sure if this is the best way of doing it, and how do I change the script to do what I need:

    strFolder = “C:Folder”
    Set objShell = CreateObject(“Shell.Application”)
    Set objFolder = objShell.Namespace(strFolder)
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)

    For Each strFileName in objFolder.Items
    If len(objFSO.GetExtensionName(strFileName)) > 0 Then
    Set objFile = objFSO.GetFile(strFolder & strFileName.Name)
    If DateDiff(“N”,objFile.DateLastModified,Now()) > 360 Then
    objFSO.DeleteFile(strFolder & strFileName.Name),True
    End If
    End If
    Next

    tehcamel
    tehcamel
    Moderator
    #358083

    Re: Deleting files older than 6 hours

    ok so, what you should do, is create a tst folder, with some random, copied data.

    It will take a little while to setup, but when you get to work tomorrow, add some files to your test folder.
    an hour later, add some3 more.
    an hour after that, add some more.

    change the line here:
    If DateDiff(“N”,objFile.DateLastModified,Now()) > 360 Then

    and use a different length of time. (360 is seconds i beleive)
    then kick your script off, in your test location.

    Avatar
    krokkie
    Member
    #339787

    Re: Deleting files older than 6 hours

    cheers tehcamel
    Do i have to write a similar script for each folder and start it with a batch file?
    How do I add the logging?

    Avatar
    igor7
    Member
    #295076

    Re: Deleting files older than 6 hours

    krokkie,
    you can try “Forfiles.exe” in a batch.

    http://forums.petri.com/showpost.php?p=94690&postcount=2

    tehcamel
    tehcamel
    Moderator
    #358085

    Re: Deleting files older than 6 hours

    you would need to make it recursive at some point.. but i’d make sure your timing part works correctly first.

    Rems
    Rems
    Moderator
    #228052

    Re: Deleting files older than 6 hours

    krokkie;245758 wrote:
    cheers tehcamel
    Do i have to write a similar script for each folder and start it with a batch file?
    How do I add the logging?

    You don’t have to rewrite the same code for each target folder. In a script you can make a call to a subroutine section and passing arguments.

    Or, in your case, you could also create iteration by adding the specific folders in to an array and use a For-Next statement to process every item in the array.
    _

    You can write the script so that is able to create/append a logfile OR, you could also run the vbscript from a command line (or from a shortcut file) and redirect the results you normaly would see on the screen to a txt file.
    _

    You mensioned lauching the vbscript(s) from a bach file – maybe that was because you was looking for a way to run multiple vbs files in one run? or, do you prefere running a batch rather than directly starting the vbscript?

    Below is a batch sample (in case you prefere executing a batch).

    Code:
    :: this is a dos batch sample. save the file with the cmd extension.

    @GOTO:start

    ::> arrFolders = Array(“D:SDMAPPDomInfoPlans”, _
    ::> “D:SDMAPPDomInfoHourly Metars”, _
    ::> “D:SDMAPPDomInfoHourly Tafs”, _
    ::> “D:SDMAPPIntlInfoPlans”, _
    ::> “D:SDMAPPIntlInfoHourly Metars”, _
    ::> “D:SDMAPPIntlInfoHourly Tafs”, _
    ::> “D:SDMAPPIntlInfoNotam Briefs”, _
    ::> “D:SDMAPPIntlInfoWx Briefs”)
    ::>
    ::> Set objShell = CreateObject(“Shell.Application”)
    ::> Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    ::> dt = Now()
    ::>
    ::> wscript.echo “reference date-time”, dt
    ::>
    ::> for i=0 to Ubound(arrFolders)
    ::>
    ::> strFolder = arrFolders(i)
    ::> wscript.echo
    ::>
    ::> If objFSO.FolderExists(strFolder) Then
    ::> Set objFolder = objFSO.GetFolder(strFolder)
    ::> wscript.echo “Folder:”, objFolder.Path
    ::> Set colFiles = objFolder.Files
    ::> k = 0 : l = 0
    ::> For Each objFile in colFiles
    ::> k = k + 1
    ::> lstMod = objFile.DateLastModified
    ::> If [B][COLOR=”Red”]DateDiff(“h”,LstMod,dt) >= 7[/COLOR][/B] Then
    ::> l = l + 1
    ::> wscript.echo ” Delete file: “”” _
    ::> & objFile.Name & “”” [” & LstMod & “]”
    ::> objFile.delete()
    ::> End If
    ::> Next
    ::> wscript.echo k, “files /”, l, “deleted.”
    ::> Else
    ::> wscript.echo “ERROR, folder not found:”, strFolder
    ::> End If
    ::> Next
    ::> wscript.echo
    ::> wscript.echo

    :runvbs (creating the vbs file with the above code)
    >”%temp%.$vbs” echo.
    For /f “tokens=1,*” %%x in (
    ‘findstr /rb “::> ” “%*”‘) do (
    call set “line=echo.%%y”
    for /f “tokens=*” %%* in (‘”echo.”%%line%%””‘) do (%%~*)
    ) >>”%temp%.$vbs”
    cscript.exe /nologo /e:vbscript “%temp%.$vbs” >>”%logfile%” 2>&1
    exit /b 0

    :start
    @echo off & cls
    color 9a & title Folder Cleaning.
    Set “logfile=[COLOR=”Navy”][B]c:$$log.txt[/B][/COLOR]”
    echo. Do not close this box…..

    Call:runvbs %~0

    It is possible to just copy the vbs code part (without the leading “::> “) from this batch and paste it to a vbs file. Then create a shortcut that fires the script and redirect the output to a file.
    e.g.,
    cmd /c cscript.exe /nologo “c:pathfile.vbs” >>”%logfile%” 2>&1
    _

    More about DateDiff and its first parameter (=interval) you can read here: http://www.w3schools.com/vbscript/func_datediff.asp

    /Rems

    Avatar
    krokkie
    Member
    #339788

    Re: Deleting files older than 6 hours

    Thanks you for all the info, it is much appreciated

    Avatar
    Dumber
    Participant
    #201702

    Re: Deleting files older than 6 hours

    Why not using powershell? You might be able to do this with just a couple of lines.

    Avatar
    krokkie
    Member
    #339789

    Re: Deleting files older than 6 hours

    I have no knowledge of Powershell

    Avatar
    krokkie
    Member
    #339790

    Re: Deleting files older than 6 hours

    REM

    I copied the script you provided and saved it as .cmd
    When i run it, nothing happens, the screen comes up that says Do not close this, but nothing else.
    The log file is created, but is blank.
    I checked all the folders, and some of them have old files

    Rems
    Rems
    Moderator
    #228055

    Re: Deleting files older than 6 hours

    krokkie;246094 wrote:
    REM

    I copied the script you provided and saved it as .cmd
    When i run it, nothing happens, the screen comes up that says Do not close this, but nothing else.
    The log file is created, but is blank.
    I checked all the folders, and some of them have old files

    for troubleshooting purpose change the lines cscript.exe /nologo /e:vbscript “%temp%.$vbs” >>”%logfile%” 2>&1
    exit /b 0[/CODE]
    to[CODE] start “”/b /w cscript.exe /e:vbscript “%temp%.$vbs”
    pause
    exit /b 0[/CODE]
    Do you see any error messages appear in the box?

    What Windows version is on the machine?

    /Rems[CODE] cscript.exe /nologo /e:vbscript “%temp%.$vbs” >>”%logfile%” 2>&1
    exit /b 0[/CODE]
    to start “”/b /w cscript.exe /e:vbscript “%temp%.$vbs”
    pause
    exit /b 0[/CODE]
    Do you see any error messages appear in the box?

    What Windows version is on the machine?

    /Rems[CODE] start “”/b /w cscript.exe /e:vbscript “%temp%.$vbs”
    pause
    exit /b 0[/CODE]
    Do you see any error messages appear in the box?

    What Windows version is on the machine?

    /Rems

    Avatar
    Dumber
    Participant
    #201712

    Re: Deleting files older than 6 hours

    Powershell example:
    http://ict-freak.nl/2009/03/25/remove-files-older-than-x-days-with-posh/
    this is days though, but could be easily modfied to minutes.

    $Now = Get-Date
    $Days = “3”
    $TargetFolder = “C:LiveUpdate”
    $LastWrite = $Now.AddDays(-$days)
    $Files = get-childitem $TargetFolder -include *.zip, *.x86 -recurse
    | Where {$_.LastWriteTime -le “$LastWrite”}

    foreach ($File in $Files)
    {write-host “Deleting File $File” -foregroundcolor “Red”;

    Remove-Item $File | out-null}[/CODE][CODE]$Now = Get-Date
    $Days = “3”
    $TargetFolder = “C:LiveUpdate”
    $LastWrite = $Now.AddDays(-$days)
    $Files = get-childitem $TargetFolder -include *.zip, *.x86 -recurse
    | Where {$_.LastWriteTime -le “$LastWrite”}

    foreach ($File in $Files)
    {write-host “Deleting File $File” -foregroundcolor “Red”;

    Remove-Item $File | out-null}[/CODE]

    Avatar
    krokkie
    Member
    #339791

    Re: Deleting files older than 6 hours

    Thanks Dumber

    I will surely have a look at your suggestion.

    Avatar
    krokkie
    Member
    #339792

    Re: Deleting files older than 6 hours

    REM

    Let me see if I understand you correctly:

    I need to copy the whole script you provided and save as .cmd

    or

    I need to copy the vbs part and run it with the command you’ve provided

    Avatar
    krokkie
    Member
    #339793

    Re: Deleting files older than 6 hours

    Dumber
    What would I save this Poewershell script as?
    And how would I run it?

    Avatar
    Dumber
    Participant
    #201718

    Re: Deleting files older than 6 hours

    as a PS1 file. You can run it with powershell.exe command.

    instead of $Now.AddDays(-$days) you can use $Now.AddHours(-$hours)
    Of course, replace the $days variable with $hours

    Make sure you test it first ;)

    Avatar
    krokkie
    Member
    #339794

    Re: Deleting files older than 6 hours

    Thanks Dumber, will try that

    Avatar
    krokkie
    Member
    #339795

    Re: Deleting files older than 6 hours

    How would I change the script to include all the folders?

    Rems
    Rems
    Moderator
    #228057

    Re: Deleting files older than 6 hours

    krokkie;246244 wrote:
    REM

    Let me see if I understand you correctly:

    I need to copy the whole script you provided and save as .cmd

    or

    I need to copy the vbs part and run it with the command you’ve provided

    That is correct.

    The previous script does not walk a directory tree and search for files and folders in each branch, it just searching the files in the base folders that were provided. The vbs sample below does also seach in all subfolders.
    Additionally, this sample also append to a logfile by script, so you no longer have to use the cscript command line for starting the script and make the output be redirected to a file.

    arrFolders = Array(“D:SDMAPPDomInfoPlans”, _
    “D:SDMAPPDomInfoHourly Metars”, _
    “D:SDMAPPDomInfoHourly Tafs”, _
    “D:SDMAPPIntlInfoPlans”, _
    “D:SDMAPPIntlInfoHourly Metars”, _
    “D:SDMAPPIntlInfoHourly Tafs”, _
    “D:SDMAPPIntlInfoNotam Briefs”, _
    “D:SDMAPPIntlInfoWx Briefs”)

    Set objShell = CreateObject(“Shell.Application”)
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    dt = Now()

    [COLOR=”Green”]Set objLogFile = objFSO.OpenTextFile(“[COLOR=”Navy”][B]c:test-log.txt[/B][/COLOR]”, 8, True)[/COLOR]

    dim k, l, ld

    [COLOR=”Green”]rem wscript.echo “reference date-time”, dt
    objLogFile.writeLine(“reference date-time ” & dt)[/COLOR]
    for i=0 to Ubound(arrFolders)
    ld = “Folder: ”
    strFolder = arrFolders(i)
    [COLOR=”Green”] rem wscript.echo
    objLogFile.writeLine [/COLOR]
    If objFSO.FolderExists(strFolder) Then
    k = 0 : l = 0
    enumerateFolders objFSO.GetFolder(strFolder)
    [COLOR=”Green”] rem wscript.echo k, “files /”, l, “deleted.”
    objLogFile.writeLine k & ” files / ” & l & ” deleted.”[/COLOR]
    Else
    [COLOR=”green”] rem wscript.echo “ERROR, folder not found:”, strFolder
    objLogFile.writeLine “ERROR, folder not found: ” & strFolder[/COLOR]
    End If
    Next
    [COLOR=”Green”]rem wscript.echo
    rem wscript.echo
    objLogFile.writeLine
    objLogFile.writeLine[/COLOR]

    Sub enumerateFolders(Folder)
    enumerateFiles Folder.Path
    On Error Resume Next
    For Each Subfolder in Folder.SubFolders
    enumerateFolders Subfolder
    Next
    End Sub

    Sub enumerateFiles(sPath)
    Dim objFolder
    Set objFolder = objFSO.GetFolder(sPath)
    [COLOR=”green”] rem wscript.echo ld & objFolder.Path
    objLogFile.writeLine ld & objFolder.Path[/COLOR]
    ld = ” Sub: ”
    Set colFiles = objFolder.Files
    On Error Resume Next
    For Each objFile in colFiles
    k = k + 1
    lstMod = objFile.DateLastModified
    If DateDiff(“h”,LstMod,dt) >= 7 Then
    l = l + 1
    [COLOR=”green”] rem wscript.echo ” Delete file: “”” _
    rem & objFile.Name & “”” [” & LstMod & “]”
    objLogFile.writeLine ” Delete file: “”” _
    & objFile.Name & “”” [” & LstMod & “]”[/COLOR]
    [COLOR=”Red”][B]objFile.delete()[/B][/COLOR]
    End If
    Next
    End Sub
    [/CODE]
    /Rems[CODE]
    arrFolders = Array(“D:SDMAPPDomInfoPlans”, _
    “D:SDMAPPDomInfoHourly Metars”, _
    “D:SDMAPPDomInfoHourly Tafs”, _
    “D:SDMAPPIntlInfoPlans”, _
    “D:SDMAPPIntlInfoHourly Metars”, _
    “D:SDMAPPIntlInfoHourly Tafs”, _
    “D:SDMAPPIntlInfoNotam Briefs”, _
    “D:SDMAPPIntlInfoWx Briefs”)

    Set objShell = CreateObject(“Shell.Application”)
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    dt = Now()

    Set objLogFile = objFSO.OpenTextFile(“c:test-log.txt“, 8, True)

    dim k, l, ld

    rem wscript.echo “reference date-time”, dt
    objLogFile.writeLine(“reference date-time ” & dt)

    for i=0 to Ubound(arrFolders)
    ld = “Folder: “
    strFolder = arrFolders(i)
    rem wscript.echo
    objLogFile.writeLine

    If objFSO.FolderExists(strFolder) Then
    k = 0 : l = 0
    enumerateFolders objFSO.GetFolder(strFolder)
    rem wscript.echo k, “files /”, l, “deleted.”
    objLogFile.writeLine k & ” files / ” & l & ” deleted.”

    Else
    rem wscript.echo “ERROR, folder not found:”, strFolder
    objLogFile.writeLine “ERROR, folder not found: ” & strFolder

    End If
    Next
    rem wscript.echo
    rem wscript.echo
    objLogFile.writeLine
    objLogFile.writeLine

    Sub enumerateFolders(Folder)
    enumerateFiles Folder.Path
    On Error Resume Next
    For Each Subfolder in Folder.SubFolders
    enumerateFolders Subfolder
    Next
    End Sub

    Sub enumerateFiles(sPath)
    Dim objFolder
    Set objFolder = objFSO.GetFolder(sPath)
    rem wscript.echo ld & objFolder.Path
    objLogFile.writeLine ld & objFolder.Path

    ld = ” Sub: “
    Set colFiles = objFolder.Files
    On Error Resume Next
    For Each objFile in colFiles
    k = k + 1
    lstMod = objFile.DateLastModified
    If DateDiff(“h”,LstMod,dt) >= 7 Then
    l = l + 1
    rem wscript.echo ” Delete file: “”” _
    rem & objFile.Name & “”” [” & LstMod & “]”
    objLogFile.writeLine ” Delete file: “”” _
    & objFile.Name & “”” [” & LstMod & “]”

    objFile.delete()
    End If
    Next
    End Sub
    [/CODE]
    /Rems

    Avatar
    krokkie
    Member
    #339796

    Re: Deleting files older than 6 hours

    REMS

    Your script does exactly what I need, and better. Thank you so much for the effort, it is highly appreciated.
    The only problem is I do not want it to delete files from subfolders of the mentioned locations. Most of the locations only contain files, no subfolders. However, there is one location that does contain a Saved subfolder which the files are copied to that I do not want touched, as it is used for archiving.

    How would I modify the script?

    Rems
    Rems
    Moderator
    #228058

    Re: Deleting files older than 6 hours

    krokkie;246585 wrote:
    I do not want it to delete files from subfolders.
    How would I modify the script?

    Change the line [NoParse](28):[/Noparse]
    enumerateFolders objFSO.GetFolder(strFolder)[/CODE]
    to
    [CODE] enumerateFiles objFSO.GetFolder(strFolder)[/CODE]
    The sub routine “enumerateFolders(Folder)” is now disregarded.

    /Rems[CODE] enumerateFolders objFSO.GetFolder(strFolder)[/CODE]
    to
    enumerateFiles objFSO.GetFolder(strFolder)[/CODE]
    The sub routine “enumerateFolders(Folder)” is now disregarded.

    /Rems[CODE] enumerateFiles objFSO.GetFolder(strFolder)[/CODE]
    The sub routine “enumerateFolders(Folder)” is now disregarded.

    /Rems

    Avatar
    krokkie
    Member
    #339797

    Re: Deleting files older than 6 hours

    Thanks REMS, I’ve tested it successfully.

    Once again thank you for your help

    #384054

    Re: Deleting files older than 6 hours

    Hello Ineed a kind of file that will delete files that are older than 1 hour.
    All the files are in the same folder called FOLDER1. The script i will scedule on a windows 2008 server.

    Can someone please fine tune this script for me?

    Manny Thanks!

    arrFolders = Array(“D:SDMAPPDomInfoPlans”, _
    “D:SDMAPPDomInfoHourly Metars”, _
    “D:SDMAPPDomInfoHourly Tafs”, _
    “D:SDMAPPIntlInfoPlans”, _
    “D:SDMAPPIntlInfoHourly Metars”, _
    “D:SDMAPPIntlInfoHourly Tafs”, _
    “D:SDMAPPIntlInfoNotam Briefs”, _
    “D:SDMAPPIntlInfoWx Briefs”)

    Set objShell = CreateObject(“Shell.Application”)
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    dt = Now()

    Set objLogFile = objFSO.OpenTextFile(“c:test-log.txt“, 8, True)

    dim k, l, ld

    rem wscript.echo “reference date-time”, dt
    objLogFile.writeLine(“reference date-time ” & dt)

    for i=0 to Ubound(arrFolders)
    ld = “Folder: “
    strFolder = arrFolders(i)
    rem wscript.echo
    objLogFile.writeLine

    If objFSO.FolderExists(strFolder) Then
    k = 0 : l = 0
    enumerateFolders objFSO.GetFolder(strFolder)
    rem wscript.echo k, “files /”, l, “deleted.”
    objLogFile.writeLine k & ” files / ” & l & ” deleted.”

    Else
    rem wscript.echo “ERROR, folder not found:”, strFolder
    objLogFile.writeLine “ERROR, folder not found: ” & strFolder

    End If
    Next
    rem wscript.echo
    rem wscript.echo
    objLogFile.writeLine
    objLogFile.writeLine

    Sub enumerateFolders(Folder)
    enumerateFiles Folder.Path
    On Error Resume Next
    For Each Subfolder in Folder.SubFolders
    enumerateFolders Subfolder
    Next
    End Sub

    Sub enumerateFiles(sPath)
    Dim objFolder
    Set objFolder = objFSO.GetFolder(sPath)
    rem wscript.echo ld & objFolder.Path
    objLogFile.writeLine ld & objFolder.Path

    ld = ” Sub: “
    Set colFiles = objFolder.Files
    On Error Resume Next
    For Each objFile in colFiles
    k = k + 1
    lstMod = objFile.DateLastModified
    If DateDiff(“h”,LstMod,dt) >= 7 Then
    l = l + 1
    rem wscript.echo ” Delete file: “”” _
    rem & objFile.Name & “”” [” & LstMod & “]”
    objLogFile.writeLine ” Delete file: “”” _
    & objFile.Name & “”” [” & LstMod & “]”

    objFile.delete()
    End If
    Next
    End Sub

    Rems
    Rems
    Moderator
    #228084

    Re: Deleting files older than 6 hours

    Delete the files from c:FOLDER1 that not have been updated in the last hour,

    Code:
    strFolder = “c:FOLDER1”

    If objFSO.FolderExists(strFolder) Then
    Set objFolder = objFSO.GetFolder(strFolder)
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
    lstMod = objFile.DateLastModified
    If DateDiff(“h”,LstMod,dt) > 1 Then objFile.delete()
    Next
    Else
    wscript.echo “ERROR, folder not found:”, strFolder
    End If

    /Rems

    Avatar
    lazergeek
    Member
    #384615

    Re: Deleting files older than 6 hours

    Greetings!

    I have the same situation as ItsMeAgain (delete files older than 1 hour). I’ve tried running your script REM but it gives me an error:

    Line 3
    Error: Object required: ‘objFSO’

    Do I need to inject this script that you’ve entered in your last post to the one that ItsMeAgain entered?

    strFolder = “c:FOLDER1”

    If objFSO.FolderExists(strFolder) Then
    Set objFolder = objFSO.GetFolder(strFolder)
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
    lstMod = objFile.DateLastModified
    If DateDiff(“h”,LstMod,dt) > 1 Then objFile.delete()
    Next
    Else
    wscript.echo “ERROR, folder not found:”, strFolder
    End If
    [/CODE]

    Thanks
    -Renato[CODE]
    strFolder = “c:FOLDER1”

    If objFSO.FolderExists(strFolder) Then
    Set objFolder = objFSO.GetFolder(strFolder)
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
    lstMod = objFile.DateLastModified
    If DateDiff(“h”,LstMod,dt) > 1 Then objFile.delete()
    Next
    Else
    wscript.echo “ERROR, folder not found:”, strFolder
    End If
    [/CODE]

    Thanks
    -Renato

    Rems
    Rems
    Moderator
    #228123

    Re: Deleting files older than 1 hours

    Renatog;252738 wrote:
    Greetings!

    I have the same situation as ItsMeAgain (delete files older than 1 hour). I’ve tried running your script REM but it gives me an error:

    Line 3
    Error: Object required: ‘objFSO’

    Do I need to inject this script that you’ve entered in your last post to the one that ItsMeAgain entered?

    strFolder = “c:FOLDER1”

    If objFSO.FolderExists(strFolder) Then
    Set objFolder = objFSO.GetFolder(strFolder)
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
    lstMod = objFile.DateLastModified
    If DateDiff(“h”,LstMod,dt) > 1 Then objFile.delete()
    Next
    Else
    wscript.echo “ERROR, folder not found:”, strFolder
    End If
    [/CODE]

    Thanks
    -Renato[/QUOTE]
    The red lines below should be added to the snippet to make it run
    [CODE]strFolder = “c:FOLDER1″

    [COLOR=”Red”][B]Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    dt = Now()[/B][/COLOR]

    If objFSO.FolderExists(strFolder) Then
    Set objFolder = objFSO.GetFolder(strFolder)
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
    lstMod = objFile.DateLastModified
    If DateDiff(“h”,LstMod,dt) > 1 Then objFile.delete()
    Next
    Else
    wscript.echo “ERROR, folder not found:”, strFolder
    End If[/CODE]

    /Rems[CODE]
    strFolder = “c:FOLDER1”

    If objFSO.FolderExists(strFolder) Then
    Set objFolder = objFSO.GetFolder(strFolder)
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
    lstMod = objFile.DateLastModified
    If DateDiff(“h”,LstMod,dt) > 1 Then objFile.delete()
    Next
    Else
    wscript.echo “ERROR, folder not found:”, strFolder
    End If
    [/CODE]

    Thanks
    -Renato

    The red lines below should be added to the snippet to make it run
    strFolder = “c:FOLDER1″

    [COLOR=”Red”][B]Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    dt = Now()[/B][/COLOR]

    If objFSO.FolderExists(strFolder) Then
    Set objFolder = objFSO.GetFolder(strFolder)
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
    lstMod = objFile.DateLastModified
    If DateDiff(“h”,LstMod,dt) > 1 Then objFile.delete()
    Next
    Else
    wscript.echo “ERROR, folder not found:”, strFolder
    End If[/CODE]

    /Rems[CODE]strFolder = “c:FOLDER1”

    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    dt = Now()

    If objFSO.FolderExists(strFolder) Then
    Set objFolder = objFSO.GetFolder(strFolder)
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
    lstMod = objFile.DateLastModified
    If DateDiff(“h”,LstMod,dt) > 1 Then objFile.delete()
    Next
    Else
    wscript.echo “ERROR, folder not found:”, strFolder
    End If[/CODE]

    /Rems

    Avatar
    lazergeek
    Member
    #384616

    Re: Deleting files older than 6 hours

    At first I got “Permission denied” but it was due to one of the files having a “Read Only” attribute.
    Thanks!

    Rems
    Rems
    Moderator
    #228125

    Re: Deleting files older than 6 hours

    Renatog;252867 wrote:
    At first I got “Permission denied” but it was due to one of the files having a “Read Only” attribute.
    Thanks!

    To beable to also delete files that are set to Read-only change this line
    If DateDiff(“h”,LstMod,dt) > 1 Then objFile.delete()[/CODE]to[CODE]If DateDiff(“h”,LstMod,dt) > 1 Then objFile.delete(True)[/CODE]

    /Rems[CODE]If DateDiff(“h”,LstMod,dt) > 1 Then objFile.delete()[/CODE]toIf DateDiff(“h”,LstMod,dt) > 1 Then objFile.delete(True)[/CODE]

    /Rems[CODE]If DateDiff(“h”,LstMod,dt) > 1 Then objFile.delete(True)[/CODE]

    /Rems

    Avatar
    lazergeek
    Member
    #384617

    Re: Deleting files older than 6 hours

    You’re awesome!

Viewing 29 posts - 1 through 29 (of 29 total)

You must be logged in to reply to this topic.