Jump to content
Sign in to follow this  

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.



Share this post

Link to post
Share on other sites

Can you post an example of what you're doing? There are several things you can do

FileDelete will not delete a folder, but if a file is not specified it will delete all the files (as far as I know).

A decision is a powerful thing

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).



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

#Include <Array.au3>                            
#Include <File.au3>                             
#Include <date.au3>                             

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  
    MsgBox(64, "Error - Correct usage is:", "PURGEFILES [path] [days]", 0)

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


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)


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

Func Terminate()                                
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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...