Sign in to follow this  
Followers 0
NEOhidra

Delete old verion of a file

7 posts in this topic

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



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

just add ExitLoop after the FileDelete() ;)

Share this post


Link to post
Share on other sites

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

#6 ·  Posted (edited)

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

Thank you very much 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  
Followers 0