Jump to content

File Management Question


 Share

Recommended Posts

I need to create a script that checks the creation date of each file in a directory, and if it is older than a certain date copies it to a new location. It will then check the new location and delete any files that are older than a certain date. I am so new to AutoIT that I am not sure where to start. Thanks for any help in terms of links or code.

Link to comment
Share on other sites

I need to create a script that checks the creation date of each file in a directory, and if it is older than a certain date copies it to a new location. It will then check the new location and delete any files that are older than a certain date. I am so new to AutoIT that I am not sure where to start. Thanks for any help in terms of links or code.

If you browse this forum for the past couple of days you will find many topics dealing with datestamps and file searching. I would post some links but I am too tired to be bothered searching.

Edit

I change my mind here is one

Here

Edited by BigDod


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

hi,

cf "recursive " link in my signature for a list of links for folder handling

Best, Randall

This is what I found:

$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
Would you be so kind as to walk me through what this is actually doing? Please forgive me my ignorance, but as I said I am very new to this. The parts I am confused about are "RecFileSearch" and I have no idea where you are actually getting rid of anything or comparing dates. Help :P I am not used to feeling stupid, but I am willing to learn.
Link to comment
Share on other sites

This is what I found:

$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
Would you be so kind as to walk me through what this is actually doing? Please forgive me my ignorance, but as I said I am very new to this. The parts I am confused about are "RecFileSearch" and I have no idea where you are actually getting rid of anything or comparing dates. Help :P I am not used to feeling stupid, but I am willing to learn.
the script that you have is actually to create a list of files and filesizes, it creates filelist.txt containing said list. here's how it works:

$tosearch = "C:"

this sets the starting search location. it can be changed assuming you don't want to start in your root. one thing you could do is take user input to have the start location determined dynamically.

$output = FileOpen("c:\filelist.txt",2)

creates the output file, and opens it for writing, deleting any previous contents.

$start = TimerInit()

creates an initial timestamp that can be evaluated to determine execution time.

RecFileSearch($tosearch)

calls the function that actually does the work, passing the initial search directory.

FileClose($output)
MsgBox(0,"done","it only took " & int(TimerDiff($start)/1000) & " seconds")

once the function is done, we don't need the logfile anymore so close it, and let the user know how long it took.

Local $search = FileFindFirstFile($current & "\*.*")

this declares and assigns a search handle, with a scope that makes it only usable in this function. i did it that way so that when the function calls itself, the second instance of the function will have it's own variables to work with.

While 1

start of an infinite loop...

Dim $file = FileFindNextFile($search)
If @error Or StringLen($file)<1 Then ExitLoop

again declaring a variable with the minimum necessary scope, so that there are no issues with the wrong values being accessed by different recursive calls to the function. The second line creates an exit condition so that when no files are found, the infinite loop that is doing the work exits.

FileWriteLine($output,$current & "\" & $file & "     " & FileGetSize($file))

assuming that a file (or folder) is found, the name and size (files only, DirGetSize() is for folders) are added to the logfile

If StringInStr(FileGetAttrib($current & "\" & $file),"D") And ( $file <> "." Or $file <> ".." ) Then RecFileSearch($current & "\" & $file)

this determines if the file last logged is a folder. if it is, then it calls the function again, passing that as the starting folder.

WEnd
    FileClose($search)
EndFunc
[/wend]
basically just the cleanup.  this is the end of the while loop, thus : wend, and you have to fileclose() any handles created for filefindfirstfile(), and EndFunc just ends the function.


now the code does not in it's current state do exactly what you want.  assuming you don't want a log file, you can remove the FileWriteLine() line, and replace it with code to do what you want for each file.  like maybe:
[code]
$created = FileGetTime($file,1,1)
$created = StringLeft($created,4) & "/" & StringMid($created,5,2) & "/" & StringRight($created,2)
If _DateDiff('D',$created,@YEAR & "/" & @MON & "/" & @MDAY) >= 365 Then FileDelete($file)

make sure you include date.au3 if you use _DateDiff().

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