Jump to content

List files in folder, delete all but newest version


Vivaed
 Share

Recommended Posts

I am trying to read all the files in a folder, then whatever file is newest, keep only that one and delete the rest.

Example files:  (these all have version numbers embedded in them, I dont want to rely on the file name)

Faint.exe

Faint-v2.0.exe

Faint-v3.0.exe

Faint-v4.0.exe

 

What I have so far:

#include <File.au3>

RemoveOldFiles()

ConsoleWrite(FileGetVersion("C:\WB Resources\FAINT_DONT_LINK_THESE\Faint-v4.0.0.exe") &@CRLF)


Func RemoveOldFiles()

    Local $aFileList = _FileListToArray("C:\WB Resources\FAINT_DONT_LINK_THESE\", "*.exe",$FLTA_FILES)
    For $i = 0 To UBound($aFileList) - 1
        $aFileVersion = FileGetVersion($aFileList[$i])
        ConsoleWrite($aFileList[$i] & @CRLF)
        ConsoleWrite($aFileVersion & @CRLF)
    Next
EndFunc


OUTPUT:

>Running:(3.3.14.2):C:\Program Files (x86)\AutoIt3\autoit3_x64.exe "C:\Users\bot\ownCloud\WellBeats\Delete Old Files\delete-test.au3"    
--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
3
0.0.0.0
a.exe
0.0.0.0
b.exe
0.0.0.0
Faint-v4.0.0.exe
0.0.0.0
4.0.0.18 <-- This is the correct file version....
+>07:34:26 AutoIt3.exe ended.rc:0
+>07:34:26 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 0.6647

Not sure what I am missing here?

Thanks!

Link to comment
Share on other sites

Once you have your array ( have a look at it with _arrayDisplay)

Arraysort so the highest number is at the end

ArrayExtract nth element (last element)

Sorry just need to read the last element...

Edited by l3ill
Link to comment
Share on other sites

43 minutes ago, l3ill said:

Once you have your array ( have a look at it with _arrayDisplay)

Arraysort so the highest number is at the end

ArrayExtract nth element (last element)

Sorry just need to read the last element...

Thanks for the info.

The issue at this point is its not displaying the correct information, it returns 0.0.0.0 for all even if the version is 4.0.0.18

In my example I run the function, then I run the FileGetVersion on just on file and the two outputs are different.

Link to comment
Share on other sites

  • Moderators

Vivaed,

I suspect you are getting that response because you are not passing the full path to FileGetVersion. Try using the $bReturnPath parameter when running  _FileListToArray so that the returns contain the full path and see if you get the correct version number returned.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Had some time to play, try this:

#include <Array.au3>

Global $aFileListVers[0]
Local $aFileList = _FileListToArray("C:\WB Resources\FAINT_DONT_LINK_THESE\", "*.exe", $FLTA_FILES, True)

For $i = 0 To UBound($aFileList) - 1
    $aFileVersion = FileGetVersion($aFileList[$i], $FV_PRODUCTVERSION)
    _ArrayAdd($aFileListVers, $aFileVersion)
Next

_ArrayDisplay($aFileListVers)
_ArraySort($aFileListVers, 1)
_ArrayDisplay($aFileListVers)
MsgBox(0, "Version", "Highest Version: " & $aFileListVers[0])

I did notice that not all exe's have a version as such that autoit can find it this way.

So check yours first using Context Menu (Right Click) Properties/Details and look at Product Version.

If its empty this will not produce anything.

This one for instance has a File Version but no Product Version so you would have to change the FileGetVersion Parameter

capture_17102016_225727.jpg

Edited by l3ill
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

×
×
  • Create New...