Jump to content
Sign in to follow this  
DeFuser

A few easy questions (I hope)

Recommended Posts

Hello all,

I wrote a utility that culls through a given path and deletes files more than X days old. My concern is that I make use of the DIR /B /L /S command, which lists both files and folders. I don't want to delete any folders.

I've searched the help file and have yet to find a command that can test as to whether a path is a folder.

If I use FileExists and give it a folder path it returns 1.

If I use FileDelete and give it a folder path is doesn't *seem* to delete the folder (but I want to be certain before I run this on real data).

My questions are:

1) Is there a command to check whether an entity is a folder?

2) Will FileDelete delete folders? (I don't think it does, based on experience and the existence of DirRemove)

If the above answers are no and yes respectively, any insight on how to approach the issue would also be appreciated.

Thanks,

Def

Share this post


Link to post
Share on other sites

Here is the requested (as well as almost finished) example, replete with Larry's spot-on advice.

Thanks guys, I really appreciate the input (and feel free to offer criticism of my methods too).

Regards,

Def

(edited to switch from |codebox| to |autoit|)

#NoTrayIcon
#Include <Array.au3>                            
#Include <File.au3>                             
#Include <date.au3>                             
#include<string.au3>                            

AutoItSetOption("MustDeclareVars",1)                
HotKeySet("{F9}", "Terminate")                  

If $CmdLine[0] = 2 Then 
    Dim $Path = $CmdLine[1]                         ;sets the path
    Dim $Days = $CmdLine[2]                         ;sets files older than X to be deleted  
Else
    MsgBox(64, "Error - Correct usage is:", "PURGEFILES [path] [days]", 0)
    Exit
EndIf

Dim $FileList = $Path & "\_" & Random() & ".txt"
Dim $TodaysDate = StringTrimRight(_Nowcalc(),9)     ;returns date in yyyy/mm/dd format
Dim $TotalLines
Dim $CurrentLine
Dim $TargetFile
Dim $Timestamp
Dim $AgeInDays

FileOpen($FileList,0)

RunWait(@ComSpec & " /c " & 'dir ' & $Path & ' /B /L /S > ' & $FileList, "", @SW_HIDE)

$TotalLines = _FileCountLines($FileList)
    
For $CurrentLine = 1 to $TotalLines
    $TargetFile = FileReadLine($FileList,$CurrentLine)
    If FileExists($TargetFile) = 1 Then
        $Timestamp = GetFileTimestamp($TargetFile)
        $AgeInDays = _DateDiff("D", $Timestamp, $TodaysDate)
        If $AgeInDays > $Days Then
            If Not StringInStr(FileGetAttrib($TargetFile),"D") Then
                MsgBox(64, $TargetFile & "  :  " & $AgeInDays & "  :  " & FileGetAttrib($TargetFile), "This would be deleted.", 0)
                ;FileDelete($TargetFile)
            EndIf
        EndIf
    EndIf
Next

FileDelete($FileList)

Func GetFileTimestamp($File)
    $Timestamp = FileGetTime($File,1,1)
    $Timestamp = StringTrimRight($Timestamp,6)
    $Timestamp = _StringInsert($Timestamp,"/",6)
    $Timestamp = _StringInsert($Timestamp,"/",4)
    Return $Timestamp
EndFunc

Func Terminate()                                
    Exit
EndFunc
Edited by DeFuser

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×
×
  • Create New...