Jump to content

Script to delete old files


 Share

Recommended Posts

;) There's a really cool script for deleting old files that I stumbled upon while reading through the AutoIt forums. I would like to give it a try ... but was hoping somebody could guide me along by answering a couple of questions:

1). I want to delete files older than 10 days from the following directory: "C:\archive" ... so I change the entry in the script to: "const $MAXAGE = 10 ; days". Is this correct?

2). My next change is where I specified "C\archive","*.*,$MAXAGE) Is this the only place where I specify the directory? I don't want to wipe out the wrong things!

3). Lastly, at the very bottom of the script are two commented out lines which read:

Change this function to actually delete the file !! Be careful !! It's recursive !!

; Wrong path and pattern, and your data is "gone" ....

I must admint that I'm very new to AutoIt and I've no clue as to what this is telling me to do. But I also know I don't want to guess. So could one of you experts help me understand what I need to do to try using this script.

Remember ... all I want is to delete any files from "C:\archive" that are older than 10 days.

Thanks to everyone!

Here is the link to the original post that contained this script:

http://www.autoitscript.com/forum/index.ph...c=15748&hl=

The script code including my two changes is below:

#include <Date.au3>
#include <File.au3>

const $MAXAGE = 10   ; days
global $msg = ""

$retval = _FileDeleteRecursive("C:\archive","*.*",$MAXAGE)
msgbox(0,"","Number of Files found: " & $retval)
ClipPut($msg)

func _FileDeleteRecursive($sPath, $pattern, $maxage)
     local $files = _FileListToArray($sPath,$pattern,1)
     if @error <> 0 then return -1

     local $dirs = _FileListToArray($sPath,"*",2)
     if @error <> 0 then return -1

     local $age, $nFilesFound = 0, $retval

     if IsArray($files) then
        for $n = 1 to $files[0]
            $age = _DateDiffInDays($sPath & "\" & $files[$n])
            if $age > $maxage then 
               _DeleteFile($sPath & "\" & $files[$n],$age)
               $nFilesFound += 1
            endif
        next
     endif

     if IsArray($dirs) then
        for $n = 1 to $dirs[0]
             $retval = _FileDeleteRecursive($sPath & "\" & $dirs[$n], $pattern,$maxage)
           if $retval > 0 then $nFilesFound += $retval
        next
     endif
     return $nFilesFound
endfunc

func _DateDiffInDays($filename)
    if not FileExists($filename) then return -1
    local $filetime =  FileGetTime($filename)
    local $tempDate = $filetime[0] & "/" & $filetime[1] & "/" & $filetime[2]
    local $currDate = @YEAR & "/" & @MON & "/" & @MDAY
    return _DateDiff("D",$tempDate,$currDate)
endfunc 

func _DeleteFile($filepath,$age)
    $msg = $msg & "Age: " & $age & "  File: " & $filepath & @CRLF
;
; Change this function to actually delete the file !! Be careful !! It's recursive !!
; Wrong path and pattern, and your data is "gone" ....
;
endfunc
Link to comment
Share on other sites

I've already set up a test. My original backup is someplace else. I've already created a test directory called C:\Archive.

I may be an Autoit idiot ... but not an all-inclusive idiot. ;)

And, I ran the script on a Test PC to ensure I'm not going to mess anything up on my live PC.

I've got 15 old files in the ARCHIVE directory. And the script reports that 15 files meet the criteria for expiration (a good sign). BUT ... I don't know what to change within the script to actually get it to delete these files.

Any pointers for an Autoit newbie? :lmao:

Link to comment
Share on other sites

Maybe add FileDelete($filepath) to _DeleteFile() ?

This is the help and support forum? Is it not?

As I stated, I'm new to Autoit. I've asked three very specific questions (albeit as basic as they are) in hopes that somebody with great Autoit experience could answer them.

You've not really offered anything of value except subtle hints ... and frankly ... messing with a script that deletes things (especially considering I'm a newbie) is not something I'm keen to chance on.

So, please ... help if you can and wish ... or choose not to.

I do apologize for being cross ... but I came to the help and support forum for just that. I don't understand why there is this avoidance to provide simple, straight answers.

Link to comment
Share on other sites

  • Developers

This is the help and support forum? Is it not?

- SNIP-

I do apologize for being cross ... but I came to the help and support forum for just that. I don't understand why there is this avoidance to provide simple, straight answers.

Don't understand why you make these statements.. i see somebody is trying to help and maybe others will help later... Where is it said that you will be assisted with x minutes ?

I know I have posted a script for this function as well a few times but am not in the mood anymore to try to find it for you ... Search should be able to track it down.

success with your scripting .

;)

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

This is the help and support forum? Is it not?

As I stated, I'm new to Autoit. I've asked three very specific questions (albeit as basic as they are) in hopes that somebody with great Autoit experience could answer them.

You've not really offered anything of value except subtle hints ... and frankly ... messing with a script that deletes things (especially considering I'm a newbie) is not something I'm keen to chance on.

So, please ... help if you can and wish ... or choose not to.

I do apologize for being cross ... but I came to the help and support forum for just that. I don't understand why there is this avoidance to provide simple, straight answers.

he told you, add FileDelete ($filePath) to _DeleteFile. Here at these forums, and any other decent forums, we don't just write a script for you, we guide you, telling you what you need to do to acomplish your tasks.

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

lod3n's reply was simple and straightforward. If you are having trouble converting his suggestion into actual code, ask politely.

Maybe add FileDelete($filepath) to _DeleteFile() ?

Translates to

#include <Date.au3>
#include <File.au3>

const $MAXAGE = 10   ; days
global $msg = ""
global $nFilesDel = 0

$retval = _FileDeleteRecursive("C:\archive","*.*",$MAXAGE)
msgbox(0,"","Number of files found: " & $retval & " Number of files deleted: " & $nFilesDel)
ClipPut($msg)

func _FileDeleteRecursive($sPath, $pattern, $maxage)
     local $files = _FileListToArray($sPath,$pattern,1)
     if @error <> 0 then return -1

     local $dirs = _FileListToArray($sPath,"*",2)
     if @error <> 0 then return -1

     local $age, $nFilesFound = 0, $retval

     if IsArray($files) then
        for $n = 1 to $files[0]
            $age = _DateDiffInDays($sPath & "\" & $files[$n])
            if $age > $maxage then 
               _DeleteFile($sPath & "\" & $files[$n],$age)
               $nFilesFound += 1
            endif
        next
     endif

     if IsArray($dirs) then
        for $n = 1 to $dirs[0]
             $retval = _FileDeleteRecursive($sPath & "\" & $dirs[$n], $pattern,$maxage)
           if $retval > 0 then $nFilesFound += $retval
        next
     endif
     return $nFilesFound
endfunc

func _DateDiffInDays($filename)
    if not FileExists($filename) then return -1
    local $filetime =  FileGetTime($filename)
    local $tempDate = $filetime[0] & "/" & $filetime[1] & "/" & $filetime[2]
    local $currDate = @YEAR & "/" & @MON & "/" & @MDAY
    return _DateDiff("D",$tempDate,$currDate)
endfunc 

func _DeleteFile($filepath,$age)
    $msg = $msg & "Age: " & $age & "  File: " & $filepath & @CRLF
    $response = MsgBox(4, "Delete File", "Delete " & $filepath & "?")
    If $response = 6 Then
        FileDelete($filepath)
        $nFilesDel += 1     
    EndIf   
endfunc

This code is untested. It's always a good idea to be polite. Good luck.

IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]
Link to comment
Share on other sites

Written words always come across vastly different than if spoken. With that I apologize for being rude. But my stance remains the same.

My first two questions could be answered with yes or no. The third was certainly more of a cry for help.

I have a strong personal belief that if I take the time to ask clear, detailed questions, then (if you're going to take the time to reply), offer up the same in return. I confessed I'm new to Autoit, so getting "maybe" or hints when toying with a deletion script aren't helpful. I hope this is agreeable.

If you look at the code posted, the changes I thought I needed were included. I wasn't looking for a free ride as some of you are eager to believe.

Regards

Link to comment
Share on other sites

If you look at the code posted, the changes I thought I needed were included. I wasn't looking for a free ride as some of you are eager to believe.

O.K. here we go.

Q1: Yes

Q2: Yes

Q3: code bleow, as already posted, however WITHOUT and questions asked, so be carefull

not to use the wrong path (c:\ instead of c:\archive).

func _DeleteFile($filepath,$age)
    FileDelete($filepath)
endfunc

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

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