Jump to content

FileGetTime / Remove Files Problem


karman
 Share

Recommended Posts

Hello,

I have a little problem here, I will try to explain it as good as i can (poor english..)

I have a folder with a few files, every minute I would like to remove all the files except the two newest ones.

How would I do this? Any hints are welcome. Thank you! :)

$LastMinute = @MIN
While(1)
    If ($LastMinute <> @MIN) Then
        $LastMinute = @MIN
        RemoveFiles()
    EndIf
Sleep(300)
WEnd

Func RemoveFiles()
    ;some fancy code here
EndFunc
Edited by karman
Link to comment
Share on other sites

HI,

0 = Modified (default)

1 = Created

2 = Accessed

What?

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Hello,

I have a little problem here, I will try to explain it as good as i can (poor english..)

I have a folder with a few files, every minute I would like to remove all the files except the two newest ones.

How would I do this? Any hints are welcome. Thank you! :)

$LastMinute = @MIN
While(1)
    If ($LastMinute <> @MIN) Then
        $LastMinute = @MIN
        RemoveFiles()
    EndIf
Sleep(300)
WEnd

Func RemoveFiles()
    ;some fancy code here
EndFunc
Do the following on a command line:

REM -- Directory of directories only
dir /a:d

REM -- Directory of files (not-directories) only
dir /a:-d

REM -- Sort ouput by date/time
dir /a:-d /o:d

REM -- Get only filenames (bare list)
dir /a:-d /o:d /b

REM -- Save result to a file
dir /a:-d /o:d /b > FileList.txt

So you could Run() the command line "dir /a:-d /o:d /b > FileList.txt", then do a _FileReadToArray() on the output file, and then delete all but the last two from the array.

More slick would be catching the output with $StdOutRead() so there's no intermediate file, but also more complicated to code.

:P

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Is it possible to sort it by name too? With a wildcard?

For example sort all file names that begins with "hello"..

Cheers

"dir hello*.* /a:-d /o:d /b > FileList.txt"

Now, all this could be done with internal AutoIT commands, but the commandline DIR does it all for you, so it's the easiest way.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

HI,

just a test: Does this also work for you?

#Include <File.au3>
#Include <Array.au3>

$folder = "c:\Downloads\test1\"
$filter = "*.au3"

$FileList = _FileListToArray($folder, $filter, 1)
If (Not IsArray($FileList)) And (@error = 1) Then
    MsgBox(0, "", "No Files\Folders Found.")
    Exit
EndIf

Global $Times[$FileList[0] + 1]

For $i = 1 To UBound($FileList) - 1
    $aFileTimes = FileGetTime($folder & $FileList[$i], 0, 0) ; modified
    If Not @error Then $Times[$i] = $aFileTimes[0] & $aFileTimes[1] & $aFileTimes[2] & $aFileTimes[3] & $aFileTimes[4] & $aFileTimes[5] & " " & $FileList[$i]
Next
_ArrayDisplay($FileList, "$FileList")
_ArraySort($Times, 1, 1)
_ArrayDisplay($Times, "Times")

If UBound($Times) >= 3 Then
    For $i = 3 To UBound($Times)-1
        FileDelete($folder & _StringBetween1($Times[$i], " "))
    Next
EndIf

Func _StringBetween1 ($s_String, $s_Start = 0, $s_End = 0)
    $s_Start = StringInStr($s_String, $s_Start) + StringLen($s_Start)
    Return StringMid($s_String, $s_Start, StringInStr($s_String, $s_End) - $s_Start)
EndFunc   ;==>_StringBetween1

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

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