Jump to content
Vivaed

List files in folder, delete all but newest version

Recommended Posts

Vivaed

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!

Share this post


Link to post
Share on other sites
l3ill

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

Share this post


Link to post
Share on other sites
Vivaed
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.

Share this post


Link to post
Share on other sites
Melba23

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
l3ill

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
  • Like 1

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

  • Similar Content

    • nacerbaaziz
      By nacerbaaziz
      Hello
      I have a query please
      It is about windows file explorer
      If i design a tool in autoit language to delete files for example
      and i  added it in the context menu in File Explorer
      If i right-click on any file And you choose the tool, it will be get the path of the selected file or folder
      The problem here is
      If i select a set of files or folders
      it only detect the first one
      The code that i  used is
      if $CMDLine[0] = 0 then
      msgBox(16, "error", "you don't selecte any files")
      exit
      else
      for $i = 1 to $CMDLine[0]
      msgBox(0, "files", $CMDLine[$i]
      next
      endIf
      exit
    • TheSaint
      By TheSaint
      Files Checklist
      Just a simple little (complex enough) program I whipped up, for a purpose ... but only too happy to share.
      It can of course be modified to do more, but at the moment just compares file names with or without path ... Location versus Checklist.
      There are of course other compare programs out there, most doing a lot more than mine (i.e. WinMerge or FileComparer). But I wanted something quick and simple with drag & drop, that uses up a smallish amount of screen real estate.
      NOTE - The SAVE button is also a LOAD one, and displays 'Load' when the list is empty. So you can save and load specific lists. While in SAVE mode, the button can also be used with CTRL held down, to ADD another list to existing (displayed one) ... combining as they say ... though not saved until you SAVE. CLEAR button removes a selected entry, or if used with CTRL held down, queries about removing all listed entries.
              
      Files Checklist v1.2.zip
      Files Checklist v1.3.zip
      Files Checklist v1.4.zip  (Includes a BUGFIX.)
        (source included)
      WARNING - While this program does not act on files or folders ADDED or CHECKED, you might act on the results, so I advise caution, and I also recommend using the new 'Relative' option in v1.3 or newer ... but you still need to understand what you are doing and what the results actually mean. Some basic examples in the next post.
      Enjoy!
      RELATED
      Users might also be interested in another somewhat related program of mine.
      DeleteIf Same
      Cheers!
    • Ascer
      By Ascer
      1. Description.
      Udf working with MSDN System.Collections.ArrayList. Allow you to make fast operations on huge arrays, speed is even x10 better than basic _ArrayAdd.  Not prefered for small arrays < 600 items. 2. Requirements
      .NET Framework 1.1 - 4.5 (on this version Microsoft destroy old rules) System Windows 3. Possibilities.
      ;=============================================================================================================== ; UDF Name: List.au3 ; ; Date: 2018-02-17, 10:52 ; Description: Simple udf to create System Collections as ArrayList and make multiple actions on them. ; ; Function(s): _ListCreate -> Creates a new list ; _ListCapacity -> Gets a list size in bytes ; _ListCount -> Gets items count in list ; _ListIsFixedSize -> Get bool if list if fixed size ; _ListIsReadOnly -> Get bool if list is read only ; _ListIsSynchronized -> Get bool if list is synchronized ; _ListGetItem -> Get item on index ; _ListSetItem -> Set item on index ; ; _ListAdd -> Add item at end of list ; _ListClear -> Remove all list items ; _ListClone -> Duplicate list in new var ; _ListContains -> Get bool if item is in list ; _ListGetHashCode -> Get hash code for list ; _ListGetRange -> Get list with items between indexs ; _ListIndexOf -> Get index of item ; _ListInsert -> Insert a new item on index ; _ListInsertRange -> Insert list into list on index ; _ListLastIndexOf -> Get index last of item ; _ListRemove -> Remove first found item ; _ListRemoveAt -> Remove item in index ; _ListRemoveRange -> Remove items between indexs ; _ListReverse -> Reverse all items in list ; _ListSetRange -> Set new value for items in range ; _ListSort -> Sort items in list (speed of reading) ; _ListToString -> Get list object name ; _ListTrimToSize -> Remove unused space in list ; ; Author(s): Ascer ;=============================================================================================================== 4. Downloads
      List.au3 5. Examples
      SpeedTest _ArrayAdd vs ListAdd SpeedTest ArraySearch vs ListIndexOf Basic usage - crating guild with members  
    • nacerbaaziz
      By nacerbaaziz
      Hello all
      I have a question please
      I've searched a lot on how to make a GUI for programs designed by Autoit to accepts dragging files from Windows File Explorer
      Unfortunately, however, it failed
      Is there any way to do this?
      Where I am making a audio player and I want to activate the feature of dragging files from Windows Explorer to a program window to play them
      I hope to find a simple solution here in this wonderful forum
      Thank you very much in advance
    • nacerbaaziz
      By nacerbaaziz
      hello dears
      First I would like to apologize to you for my many questions.
      I have a new question if you allow
      I am programming an audio player for blinds
      I had a problem
      I put an option in the folders context menu to Opens the audio files that in the selected folder
      I did not know how to make the Autoit Search the subfolders
      Please provide an example of how to search for  files in the subfolders
      Let's say, for example, MP3 files
      Just give me a simple example and I will try to modify it as appropriate for the program I designing it
      Please help me to find the solution
      Thanks in advance
×