Sign in to follow this  
Followers 0
savvyuser

Deleting old files

22 posts in this topic

Hi AutoIt Experts,

Does anyone here knows how to write a code that would delete more than a year files in a folder? Then, the script would also scan and delete a year older files in all subdirectories.

Appreciate your help..... Thanks......

Share this post


Link to post
Share on other sites



It would be beneficial to you if you posted more about your problem, such as your attempts so far, and more information about the files. For example, what aspect of the file needs to be more than a year old (when it was created, last accessed, or last modified)?

But to answer your question directly...

Does anyone here knows how to write a code that would delete more than a year files in a folder?

Yes I know how to write code to do that.

See what I mean? More information would be nice.

Share this post


Link to post
Share on other sites

Hi GreenMachine,

Thank you for minding my problem. Actually, I have already script for my purpose which is to delete files in directory for more than a year. Inside my folder I have plenty of subfolders and files were saved and I want a script that will scan all the files in the folder including files in subdirectories. Then, it will delete files more than 365 days. I got stuck at this line $fileJulDate = _DateToDayValue ($mod[0], $mod[1], $mod[2])[/color].

Please give me clue how to resolve this issue. Appreciate your help... Thanks...

#include <date.au3>

; Get Station number

$StationName = @ComputerName

$StationNumber = StringTrimLeft($StationName, 8)

; Delete files in all dumped folders

$ipath = "Q:\STN-" & $StationNumber & "\"

$JulianDate = _DateToDayValue(@YEAR, @MON, @MDAY)

$search = FileFindFirstFile($ipath & "*.*")

While 1

$file = FileFindNextFile($search)

If @error Then ExitLoop

; Get the last modified time of this file

$mod = FileGetTime($ipath & $file)

$fileJulDate = _DateToDayValue ($mod[0], $mod[1], $mod[2])

$datediff = $JulianDate - $fileJulDate

; If the file is more than 365 days old then delete it

If $datediff > 365 Then FileDelete($ipath & $file)

WEnd

; Close the search handle

FileClose($search)

Exit

Share this post


Link to post
Share on other sites

Hi Randall,

Attached file is the error I encountered. In my own assumption, the error occured due to directory problem. What can you advise to make the script workable.

Thanks......

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Hi Randall,

Attached file is the error I encountered. In my own assumption, the error occured due to directory problem. What can you advise to make the script workable.

Thanks......

lots of file search questions lately... ok, here's what i've got. this is a recursive file search i made for another thread that could fit your need with minimal changes. just maybe ask for user input as to the starting folder if you don't want it to search the whole c:\ and add a check to filedelete() based on filegettime($file) -one note, beta is required.

$tosearch = "C:"
$output = FileOpen("c:\filelist.txt",2)
$start = TimerInit()
RecFileSearch($tosearch)
FileClose($output)
MsgBox(0,"done","it only took " & int(TimerDiff($start)/1000) & " seconds")
Func RecFileSearch($current)
    Local $search = FileFindFirstFile($current & "\*.*")
    While 1
    Dim $file = FileFindNextFile($search)
    If @error Or StringLen($file)<1 Then ExitLoop
    FileWriteLine($output,$current & "\" & $file & "     " & FileGetSize($file))
    If StringInStr(FileGetAttrib($current & "\" & $file),"D") And ( $file <> "." Or $file <> ".." ) Then RecFileSearch($current & "\" & $file)
    WEnd
    FileClose($search)
EndFunc

***edit*** removed 2 unnecessary lines...

Edited by cameronsdad

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

Hi Randall,

Attached file is the error I encountered. In my own assumption, the error occured due to directory problem. What can you advise to make the script workable.

Thanks......

Yes, .... I really don't know...

i suggest you

1. put in some message boxes after checking if file exists...

2. check the Julian date is correct (?international settings...)

3. any other debugging you can think of.

4. try a simple directory; "@script", and change the delete line o a file write or consolewrite to see the results.

Best, Randall

(@cameronsdad - looks good...)

Share this post


Link to post
Share on other sites

Yes, .... I really don't know...

i suggest you

1. put in some message boxes after checking if file exists...

2. check the Julian date is correct (?international settings...)

3. any other debugging you can think of.

4. try a simple directory; "@script", and change the delete line o a file write or consolewrite to see the results.

Best, Randall

(@cameronsdad - looks good...)

thanks! i did it this morning for a thread, and ended up suggesting it to like 5 others(including this one)

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

Hi Guys,

Thanks for your unending showers of your knowledge.. I will made some adjustments on my script later. I don't have problem on the folder where I want to accomplish deletion. That script is part of my startup script, I have 30 workstations which each documents are saved on network folder Q:\STN-"station number"... Actually it is working fine only on the folder itself but it will not delete files in its subfolders. Aside from that, my other conditions are:

1.) the script will tolerate (.) and (..) folders

2.) Script will delete more than one year files in succeeding subfolders.

Is it that posible in AUTOIT?

Thanks......

Share this post


Link to post
Share on other sites

Hi Guys,

Thanks for your unending showers of your knowledge.. I will made some adjustments on my script later. I don't have problem on the folder where I want to accomplish deletion. That script is part of my startup script, I have 30 workstations which each documents are saved on network folder Q:\STN-"station number"... Actually it is working fine only on the folder itself but it will not delete files in its subfolders. Aside from that, my other conditions are:

1.) the script will tolerate (.) and (..) folders

2.) Script will delete more than one year files in succeeding subfolders.

Is it that posible in AUTOIT?

Thanks......

the recursive part, of checking the sub folders etc can be done with the little function i supplied above. all you have to do, is copy the function part of the code supplied to your script, then pass it the name of the folder. And you'll have to modify the part where i have "FileWriteLine($output,.." because you don't want a log. instead of that line, you could change the line to something like (psuedo code because i'm trying to tell you how to do it without doing it for you) if filegetsize($file) is older than a year then filedelete($file). if you put the function into your script like that, and change that one line, then you're all set.


1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

Hi Cameron....

I've tried to run the script and it ended up with error. It says "unable to open file, the maximum number of open files has been exceeded. And it suggested at Line 8 {Local $search = FileFindFirstFile($current & "\*.*")

Is there another way around?

Share this post


Link to post
Share on other sites

Hi,

Try this, replacing the "...." with delete (prefer move or writefileline while testing!)

hi,

There are a couple of other links that could help, though I can't find them on the forum right now!

ExploreAU3

Autoit3Explorer.au3

Just so we don't have to re-invent the wheel....

There was "going" to be a UDF....?

Randall

Share this post


Link to post
Share on other sites

Hi Cameron....

I've tried to run the script and it ended up with error. It says "unable to open file, the maximum number of open files has been exceeded. And it suggested at Line 8 {Local $search = FileFindFirstFile($current & "\*.*")

Is there another way around?

you only get that error if you use the stable release, use beta and there will be no error

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

Hi Cameron,

I have already ran the script with no error. However, it seems that nothing was happened during the script execution. I have also checked the log file but nothing was written onto it. Do i have to integrate my script with yours? Is it okay to explain in technical way what do you mean by recursive in checking subfolders.... Sorry, If I ask too much... hope this doesn't bother you....

Thank you so much for being part of this support....

Share this post


Link to post
Share on other sites

It's possible to avoid the need for recursion and function writing by delegating the file listing to the command prompt, as demonstrated below. I haven't tested the script but it should be close to what you want.

#Include <Date.au3>

; Set location to write list
Local Const $ListPath = @TempDir & '\StnListing.txt'

; Get today's Julian date
Local $Today = _DateToDayValue(@YEAR, @MON, @MDAY)

; Get station number
Local $StnNo = StringTrimLeft(@ComputerName, 8)

; Get base directory
Local $BaseDir = 'Q:\STN-' & $StnNo

; Call DOS to output a list of all files under this directory
; List will be saved to StnListing.txt in the user's temporary folder
RunWait(@ComSpec & ' /C Dir /B /S /A-D "' & $BaseDir & '" > "' & $ListPath & '"', '', @SW_HIDE)

; Attempt to open that list
Local $Handle = FileOpen($ListPath, 0)
; FileOpen() returns -1 if there's an error
If $Handle = -1 Then
    MsgBox(0x10, 'Error', 'Could not open listing. Does it exist?')
    Exit
EndIf

; Read each line of the list
; 'While 1' means loop forever
; We break out conditionally within the loop
While 1
    Local $File = FileReadLine($Handle)
; FileReadLine() fails when the end of the file is reached
    If @Error Then ExitLoop
; $File now contains a full path to a file under the base directory
    Local $FGT = FileGetTime($File)
    $FileDate = _DateToDayValue($FGT[0], $FGT[1], $FGT[2])
    If ($Today - $FileDate) > 365 Then FileDelete($File)
WEnd

; Close and delete the list
FileClose($Handle)
FileDelete($ListPath)

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Hi Cameron,

I have already ran the script with no error. However, it seems that nothing was happened during the script execution. I have also checked the log file but nothing was written onto it. Do i have to integrate my script with yours? Is it okay to explain in technical way what do you mean by recursive in checking subfolders.... Sorry, If I ask too much... hope this doesn't bother you....

Thank you so much for being part of this support....

well the script i posted isn't setup to delete anything, it just lists files. If you modified/removed the FileWriteLine() line as someone suggested previously, that's why nothing is being written to the log file.

***edit***

sorry forgot to answer your question about recursion; a recursive function is a function that calls itself until a base case is hit. in this case, the base case is the @error generated by FileFindNextFile() when there are no more files in the current directory. What the script i posted does, is it takes a starting path (c:\ by default) and checks every file in the directory to see if it's a directory, if it is, it calls itself with that as the new starting path, and starts over in the sub-folder. If a file is encountered that is not a directory, the name and size of the file are written to the log. and it just keeps repeating that until all calls for subfolders have returned because of the @error saying that there are no more files to be examined in those folders. and once all files have been examined in the initial directory, the function ends. To suit your needs, you would just have to decide how you want to treat files that are not folders: something like : (and make sure you read the rest of the post before attaching and using the code)

If FileGetTime($file,0,1) < @YEAR - 1 & @MON & @MDAY & @HOUR & @MIN & @SEC Then FileDelete($file)

i want to stress that if you use the line above, you make sure you completely understand what the script is doing, and change the starting location so that it doesn't start at c:\. If you don't, it will almost certainly make your computer unusable if you installed your OS more than year ago...

Edited by cameronsdad

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

well the script i posted isn't setup to delete anything, it just lists files. If you modified/removed the FileWriteLine() line as someone suggested previously, that's why nothing is being written to the log file.

***edit***

sorry forgot to answer your question about recursion; a recursive function is a function that calls itself until a base case is hit. in this case, the base case is the @error generated by FileFindNextFile() when there are no more files in the current directory. What the script i posted does, is it takes a starting path (c:\ by default) and checks every file in the directory to see if it's a directory, if it is, it calls itself with that as the new starting path, and starts over in the sub-folder. If a file is encountered that is not a directory, the name and size of the file are written to the log. and it just keeps repeating that until all calls for subfolders have returned because of the @error saying that there are no more files to be examined in those folders. and once all files have been examined in the initial directory, the function ends. To suit your needs, you would just have to decide how you want to treat files that are not folders: something like : (and make sure you read the rest of the post before attaching and using the code)

If FileGetTime($file,0,1) < @YEAR - 1 & @MON & @MDAY & @HOUR & @MIN & @SEC Then FileDelete($file)

i want to stress that if you use the line above, you make sure you completely understand what the script is doing, and change the starting location so that it doesn't start at c:\. If you don't, it will almost certainly make your computer unusable if you installed your OS more than year ago...

another example of recursion for you, because i needed the practice and didn't think another example would hurt. this is a script that asks you to type a word, then it returns all possible permutations of the letters in the given word, and displays the output in notepad. A few things to note:

1) the number of possible permutations is the factorial of the number of letters in the word; "stack" with 5 letters returns 120 permutations, but "stacks" with 6 returns 720. and it just goes up from there. The script takes longer for longer words obviously.

2) autoit limits the number of recursive function calls, 384 i believe, so if you do write your own recursive function for something else, make sure it's not going to have to call itself more than that...

***edit*** sorry, i'm a tard forgot to add the code.

#include<array.au3>
Global $count = 0;sets up a counter
Global $word = InputBox("word", "enter a word", "cat");takes user input to get the word
Global $size = StringLen($word);determines the size of the word to find the total number of possible permutations
                            ;number of possible = factorial of length or: possible = $size * ($size -1) * ($size -2) * ($size - 3)...
Global $broken = StringSplit($word,"");breaks the word into letters
_ArrayDelete($broken,0);removes the count element from the array created by the stringsplit() function
$tmp = "";creates a container to hold the final string to output
$start = TimerInit()
DoAnagram($size);begins the recursive function
ConsoleWrite(@lf & TimerDiff($start)& @lf)
If StringLen($tmp) Then
run("Notepad");runs notepad to display data
WinWait("Untitled - Notepad");waits for notepad to exist
ControlSetText("Untitled - Notepad","",15,$tmp);updates the edit control in notepad to contain all of the data returned
EndIf

Func DoAnagram($newsize);this is the function that calls itself repeatedly to achieve the result
    if $newsize <> 1 Then;this check ensures that when the base case is encountered, no further calls to this function happen
        For $j = 1 to $newsize;this has it call itself for every possible combination, as explained in the declaration of the $size variable
            DoAnagram($newsize -1);it calls itself after decrementing the parameter it was passed
            If $newsize = 2 Then displayword();when the base case is being called, the word is displayed
            rotate($newsize);this rotates the letters that are not the first letter
        Next
    EndIf
EndFunc

Func rotate($newsize2);this function takes the number of characters that it is passed
    $position = $size - $newsize2;determines the number of characters that need to be rotated
$temp = $broken[$position];grabs the element currently in the first position
For $iterator = $position + 1 to $size-1;cycles through entire portion of the word
    $broken[$iterator - 1] = $broken[$iterator];moves each character left
Next
$broken[$iterator -1] = $temp;and stacks the element that was left most onto the right side
EndFunc

Func displayword();this function adds the combinations to the string to be displayed
    if $count < 99 Then $tmp = $tmp & " "
    if $count < 9 Then $tmp = $tmp & " "
    $count = $count + 1
    $tmp = $tmp & $count & " "
    For $iterator2 = 0 to $size - 1
        $tmp = $tmp & $broken[$iterator2]
    Next
    $tmp = $tmp & "  "
    $tmp = $tmp & ""
    If not Mod($count,6) Then $tmp = $tmp & @CRLF
EndFunc
Edited by cameronsdad

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

Hi LxP.....

Wow it looks okay...... Will try it later.... You're the man.....

Share this post


Link to post
Share on other sites

you should try my *XPClean Menu*

its at the bottom of this post

Enjoy!

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

Hi Cameron,

Thanks for shading a light regarding on my inquiry. I believe with LxP, i will evaluate first his suggestion by the getting file using DOS .... I'm sure some of the guys here are also having a problem the same with mine. Will tell you on any progress I have made...

Thanks a lot man...

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