Sign in to follow this  
Followers 0
homunculus

Handy UDF

4 posts in this topic

Hi, as I can't post in the UDF section I'll just do it here. I've created a UDF which deletes old files with a certain naming convention, such as logfiles with a certain date format.

For example nnCron lite logs with the following format:

0525cron.log

or Spybot logs with the following format:

Checks.070521-0900.log

This UDF takes the following options:
  • Working directory
  • File name pattern
  • Number of files to keep
To delete all except the latest 14 nnCron Lite logs, do this:

#include <OldFileDeleteFunction.au3>

OldFileDeleteFunction('C:\Program Files\cron\log','????cron.log',14)

To delete all except the latest 10 SpyBot S&D logs, do this:

#include <OldFileDeleteFunction.au3>

OldFileDeleteFunction('C:\Documents and Settings\All Users\Application Data\Spybot - Search & Destroy\Logs',Checks.??????-????.log',10)

And so on. Please let me know what you think.

Func OldFileDeleteFunction($Directory,$FileSpec,$KeepFiles)
    If FileChangeDir($Directory) = 0 Then Exit
    $OldFileFinder = FileFindFirstFile($FileSpec)
    If $OldFileFinder = -1 Then Exit

    $OldFileArray = ''

    While 1
        $OldFile = FileFindNextFile($OldFileFinder)
        If @error Then ExitLoop

        $OldFileArray &= @CRLF & $OldFile
    WEnd

    FileClose($OldFileFinder)

    $SortProcess = Run('sort.exe /r ', @SystemDir, @SW_HIDE, 1 + 2) ; $STDIN_CHILD + $STDOUT_CHILD, provided as numbers so we don't have to include the Constants file.
    StdinWrite($SortProcess, $OldFileArray)
    StdinWrite($SortProcess)

    $String = StdoutRead($SortProcess)

    $FileArray = StringSplit(StringStripCR($String), @LF)

    If $FileArray[0] > $KeepFiles Then
        For $e = $KeepFiles + 1 To $FileArray[0] - 1
            If FileExists($FileArray[$e]) Then FileDelete($FileArray[$e])
        Next
    EndIf
EndFunc

Share this post


Link to post
Share on other sites



@ homunculus:

Hi, and welcome.

I think it's indeed a useful UDF, thanks for sharing.

Let me make some comments:

- I think you meant RETURN, not EXIT

- You change the working dir. May be it worth to restore it at the end, or modify the function to not change it

- Win98SE (at least my version) do not have sort.exe

- You could use _FileListToArray to create the file array. It is not documented, but I always have received the array already sorted. Anyway, you have _ArraySort, no need for sort.exe.

- You may use the check "If $FileArray[0] > $KeepFiles Then..." near the end of UDF for clarity, but it is not strictly necessary: the For loop will take care about that (it is AutoIt, man!)

Have a good time

alc

Share this post


Link to post
Share on other sites

@ homunculus:

Hi, and welcome.

I think it's indeed a useful UDF, thanks for sharing.

Let me make some comments:

- I think you meant RETURN, not EXIT

- You change the working dir. May be it worth to restore it at the end, or modify the function to not change it

- Win98SE (at least my version) do not have sort.exe

- You could use _FileListToArray to create the file array. It is not documented, but I always have received the array already sorted. Anyway, you have _ArraySort, no need for sort.exe.

- You may use the check "If $FileArray[0] > $KeepFiles Then..." near the end of UDF for clarity, but it is not strictly necessary: the For loop will take care about that (it is AutoIt, man!)

Have a good time

alc

Hi Alc,

Thanks for your comments, much appreciated.

- I used Exit because Exit really means Exit. However, Return is probably better.

- Modifying the function not to change the working dir is a good idea.

- I made (and use) the script for Windows XP

- I wasn't aware this function already exists and was actually a waste of time...

- Indeed, I didn't need the For-Next loop.

So, this UDF can be moved into the trashcan.

Share this post


Link to post
Share on other sites

Hi homunculus:

Definitely no.

You said you are using it, so it works, so it worth. I just wanted to improve it a little. The other day I was in a hurry, I didn't give you the right answer. I don't have to much time now, but I'll try to add some ideas.

Functions are great because you can use them again and again. Writing them carefully allow us to cover as much scenarios as possible. That's why I suggested "Return" and avoiding XP+ solutions (the last statement is controversial, since Win9x has been left behind by many people ).

Other suggestions:

- Using default values for arguments, whenever possible and makes sense

- Validating the arguments. In general, taking care about errors and returning the corresponding info

- I think is a good practice to return values from function, again, whenever possible and makes sense. Here you could return the number of files deleted, or the number of file left, or both in a little array.

Just found this: http://www.autoitscript.com/forum/index.ph...st&p=337553

Well, RAMzor came a few days before you. I think his functions are a good example.

My last suggestion: I would like to have an argument to send the files to Recycle Bin instead of been deleted.

But anyway, these are just my opinions.

Have a nice day

alc

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  
Followers 0