Jump to content

Recommended Posts

Posted

Tryin to delete files older than 21 days in a network folder. The user has admin access to the folder, but the script, below doesn't delete any files.

#RequireAdmin
#include <File.au3>
#include <Date.au3>
Global $sFolderPath = "\\SHARE\home\Logs"
Global $iDaysToKeep = 21
Global $sDate = _DateAdd('d', -$iDaysToKeep, _NowCalcDate())
For $sFile In _FileListToArray($sFolderPath)
    $sFilePath = $sFolderPath & "\" & $sFile
    $sFileDate = FileGetTime($sFilePath, 1)
    If $sFileDate <> -1 And $sFileDate < $sDate Then
        FileDelete($sFilePath)
    EndIf
Next

 

  • Moderators
Posted

Moved to the appropriate AutoIt General Help and Support forum, as the Developer General Discussion forum very clearly states:

  Quote

General development and scripting discussions.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Expand  

Moderation Team

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

@Danp2  I'm lost here. Do you mean supply a path value for $sFilePath like below?

#RequireAdmin
#include <File.au3>
#include <Date.au3>
Global $sFolderPath = "\\SHARE\home\Logs"
Global $sfilepath = "\\SHARE\home\Logs\*.htm"
Global $iDaysToKeep = 21
Global $sDate = _DateAdd('d', -$iDaysToKeep, _NowCalcDate())
For $sFile In _FileListToArray($sFolderPath)
    $sFilePath = $sFolderPath & "\" & $sFile
    $sFileDate = FileGetTime($sFilePath, 1, 1) ; $FT_CREATED, $FT_STRING
    If $sFileDate <> -1 And $sFileDate < $sDate Then
        FileDelete($sFilePath)
    EndIf
Next

 

Posted (edited)

@Danp2  I have searched the forums. I think something else is broke because trying do delete files older than X doesn't even work with a batch script, which is what brought me here in the first place.

Edited by copyleft
Posted

Added network path check and console write; script completes with no errors but still no files deleted, like I said, must be some other Windows issue. I'll move along unless someone has other ideas.

#RequireAdmin
#include <File.au3>
#include <Date.au3>
Global $sNetworkPath = "\\SHARE\home\Logs"

If FileExists($sNetworkPath) Then
    ConsoleWrite("Network path is accessible." & @CRLF)
    Global $sCurrentDate = _NowCalc()
    Global $sOldDate = _DateAdd('d', -6, $sCurrentDate)
    Global $aFileList = _FileListToArray($sNetworkPath, "*.htm", 1)
    For $i = 1 To $aFileList
        Global $sFileDate = FileGetTime($sNetworkPath & "\" & $aFileList[$i], 1, 1)
        If $sFileDate <> -1 And $sFileDate < $sOldDate Then
            FileDelete($sNetworkPath & "\" & $aFileList[$i])
            ConsoleWrite("Deleted file: " & $aFileList[$i] & @CRLF)
        EndIf
    Next
Else
    ConsoleWrite("Network path is not accessible." & @CRLF)
EndIf
If $CmdLine > 0 And $CmdLine = "/console" Then
    ConsoleWrite("Running script in console mode..." & @CRLF)
Else
    ConsoleWrite("Running script in normal mode..." & @CRLF)
EndIf

 

Posted (edited)

Hello,

where it's convenient, I like to use powershell from within Autoit.

You could invoke a powershell one-liner with no need to do any loop testing:

$LogDir="\\servername\sharename\log"
$Params='-noprofile -executionpolicy bypass -command "gci "' & $LogDir & '" -file -force -ea si| ? lastwritetime -lt $(get-date).adddays(-21) | rm -force -ea si'
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Params = ' & $Params & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
ShellExecute("powershell",$Params)

 

Notes:

gci    -> get-childitem
-ea si -> erroraction silentlycontinue - suppress any error messages
?      -> where - testing condition
-lt    -> less than (older than)
rm     -> remove-item
Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...