Jump to content

Recursive File Delete


Recommended Posts

Newbie AutoIT coder here,

I am trying to make a script that will search through a certain folder and look for files that are older than a week and delete them.

I think I might have a good start with the code, but I can't really test it all that well because the files are on some production servers.

Any help would be nice, I got this far from the help file and the forum. I didn't see any code that was posted that does something like this to spring board off of.

Thanks in advance.

#include <date.au3>

While1

$fmt = FileGetTime( "D:\DIR1\DIR2\DIR3\*.tga", 0, 1)
$nf = FileFindFirstFile("$fmt")
$fd = _DateDiff( 'w', $fmt, _NowCalc())

    If $fd > 1 Then
        FileDelete($nf) 

WEnd

[font="Verdana"]People who say it cannot be done should not interrupt those who are doing it. - George Benard Shaw[/font]

Link to comment
Share on other sites

This is off the top of my head but...what about:

#include<date.au3>
Dim $sDir, $hFMT, $sFile

$sDir = 'D:\DIR1\DIR2\DIR3"
$hFMT = FileFindFirstFile($sDir & '\*.tga)
If @error Then Exit
While 1
   $sFile = FileFindNextFile($hFMT)
   If @error Then ExitLoop
   If DateDiff('w', FileGetTime($sDir & '\' & $sFile, 0, 1), _NowCalc()) > 1 Then FileDelete($sDir & '\' & $sFile)
WEnd
FileClose($hFMT)

Hope that works :)

Edited by MSLx Fanboy

Writing AutoIt scripts since

_DateAdd("d", -2, _NowCalcDate())
Link to comment
Share on other sites

Interesting approach MSLx Fanboy, could you explain why you did it that way please? Just so that I can get a understanding how you came up with that.

Granted I'll have to see if I can get a test set up to run it, but would mine have worked, or is yours a different way of looking at it.

Just trying to get a feel for what may have gone wrong.

I want to learn how to write code, not have somebody write it for me :) Just looking for a little direction.

Don't get me wrong, I apperciate your effort MSLx Fanboy, thanks. :evil:

[font="Verdana"]People who say it cannot be done should not interrupt those who are doing it. - George Benard Shaw[/font]

Link to comment
Share on other sites

#include<date.au3>
Dim $sDir, $hFMT, $sFile

$sDir = 'D:\DIR1\DIR2\DIR3"
$hFMT = FileFindFirstFile($sDir & '\*.tga)
If @error Then Exit
While 1
   $sFile = FileFindNextFile($hFMT)
   If @error Then ExitLoop
   If DateDiff('w', FileGetTime($sDir & '\' & $sFile, 0, 1), _NowCalc()) > 1 Then  FileDelete($sDir & '\' & $sFile)
WEnd
FileClose($hFMT)

Interesting approach MSLx Fanboy, could you explain why you did it that way please? Just so that I can get a understanding how you came up with that.

Granted I'll have to see if I can get a test set up to run it, but would mine have worked, or is yours a different way of looking at it.

Just trying to get a feel for what may have gone wrong.

I want to learn how to write code, not have somebody write it for me :) Just looking for a little direction.

Don't get me wrong, I apperciate your effort MSLx Fanboy, thanks. :evil:

<{POST_SNAPBACK}>

Your code didn't use the FileFindNextFile, required to get file names, so I had to create the handle right quick, and then perform loops to get each filename and process each one...looping it gets the next file. If FileFindNextFile generates an error (no other files), then it would stop the loop, meaning no more files to be processed.

Oh, had to fix one little bug I had, I put another While 1 instead of WEnd...I'll fix it above too :D

Writing AutoIt scripts since

_DateAdd("d", -2, _NowCalcDate())
Link to comment
Share on other sites

OK, I'll have to look over it a little bit more to really understand the difference between what I had and what you came up with.

Thanks for the feedback, when I get a chance to test it out, I'll let you know how it goes.

The next thing that I would like to try and integrate is to make a operational log of when it deletes so I can keep track of the files that it gets rid of, for trouble shooting purposes for when somebody starts grumping that a graphic was deleted when it shouldn't have been removed.....a just in case sort of thing.

Thanks again MSLx Fanboy :)

[font="Verdana"]People who say it cannot be done should not interrupt those who are doing it. - George Benard Shaw[/font]

Link to comment
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
 Share

  • Recently Browsing   0 members

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