Jump to content
Sign in to follow this  
NEOhidra

Delete old verion of a file

Recommended Posts

NEOhidra

Hi i am completly new to scripting and that is why i am asking here. I would like to ask if AutoIt can search a predefined directory fo two versions of one file and delete the older one (compare by name or file version/date of creation it does not matter).

An example may be a better idea, something like

Search C:\installer\ for installer_0.2.*.exe

If C:\installer\ contains installer_0.2.4.exe and installer_0.2.5.exe then delete installer_0.2.4.exe

Could you please help me? Or at least point me where/what should i read in order to make such script?

Share this post


Link to post
Share on other sites
NEOhidra

I will try to do it as simple as possible (not that i understand something). Now i have this:

If FileExists("C:\installer\installer_0.2.*.exe") Then 
; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile("C:\installer\installer_0.2.*.exe")
 
; Check if the search was successful
If $search = -1 Then
     MsgBox(0, "Error", "No files/directories matched the search pattern")
     Exit
 EndIf

 While 1
     $file = FileFindNextFile($search)
     If @error Then ExitLoop
     
     FileDelete("C:\installer\installer_0.2.*.exe")
WEnd
EndIf 
; Close the search handle FileClose($search)

FileDelete("C:\installer\installer_0.2.*.exe")

deletes both files but whith what i should replace it in order to make AutoIt to delete the first file found?

Share this post


Link to post
Share on other sites
NEOhidra

If FileExists("C:\installer\installer_0.2.*.exe") Then 
; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile("C:\installer\installer_0.2.*.exe")
 
; Check if the search was successful
If $search = -1 Then
     MsgBox(0, "Error", "No files/directories matched the search pattern")
     Exit
 EndIf

 While 1
     $file = FileFindNextFile($search)
     If @error Then ExitLoop
     
     FileDelete("C:\installer\installer_0.2.*.exe") Then ExitLoop
WEnd
EndIf 
; Close the search handle FileClose($search)

And again both files are deleted.

This is the output of SciTE:

C:\ai\tes.au3 (15) : ==> Illegal text at the end of statement (one statement per line).:
FileDelete("C:\MxDownload\miranda-im-v0.9.*-unicode.exe") Then ExitLoop
FileDelete("C:\MxDownload\miranda-im-v0.9.*-unicode.exe") ^ ERROR

Share this post


Link to post
Share on other sites
Oneqa

Sry about the post before. Was phoning my gf while replying your email :)

Here you go:

$fPath = "C:\installer"
$wildcard = "installer_0.2.*.exe" ;dont leave blank, will delete all file if do so

$Fullfilename = SearchOneFile($fPath, $wildcard) 
If $Fullfilename <> "*" Then 
    FileDelete($Fullfilename) 
Else
    MsgBox(0, "Error", "No files/directories matched the search pattern")
EndIf

Func SearchOneFile($path, $wCard = "*.*")
Local $fullpath
Local $search
Local $file, $fAttrib

$search = FileFindFirstFile($path & "\" & $wCard)

While 1
    If $search = -1 Or @error Then
        Return "*"
    EndIf

    $file = FileFindNextFile($search)    
    If @error Then 
        Return "*"
    EndIf
     
    $fullpath = $path & "\" & $file 
    $fAttrib = FileGetAttrib($fullpath)
    
    If StringInStr($fAttrib, "D") = 0 Then ;is a file
        Return $fullpath
    Else
        $fullpath = SearchOneFile($fullpath, $wCard)
        If $fullpath <> "*" Then ExitLoop
    EndIf
 WEnd
 
Return $fullpath
EndFunc

as a plus this will do recursive search within all subdirectory ;)

Edited by Oneqa

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.