DIR() sort by file LastChange

Post

Posted
Rating:
#1 (In Topic #526)
Regular
bill-lancaster is in the usergroup ‘Regular’
What is the best way (if any) to have an array of files sorted by date?
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
stevedee is in the usergroup ‘Regular’
 Bill, you probably need to take a look at the Stat command.

This should give you the LastModified date, providing you can still access the file system using the file name references in your array.
Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’
A GridView has sorting (sort of)

You have to add your own sorting routine to your code.  wiki shows how /comp/gb.qt4/gridview/sorted - Gambas Documentation

populate the gridview with filenames on column 1 and modified dates on column 2 then with GridView1.Colums.Sort = 1 set you can use it to sort by date.
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
bill-lancaster is in the usergroup ‘Regular’
 Thanks for the replies,
Bear in mind I'm looking for an array of file names sorted by each file's LastChange value.
I found this posting from way back. Its 'messy'.  Add the LastModified date string to the file name, sort it then remove the date part.

Private Procedure SortArray(sPath As String, sOriginal As String[]) As String[]
Dim sTempArray, sReturnArray As New String[]
Dim s As String
  For Each s In sOriginal
    sTempArray.Add(Stat(sPath &/ s).LastModified & "#" & s)
  Next
  sTempArray = sTempArray.Sort()
  For Each s In sTempArray
    sReturnArray.Add(Split(s, "#")[1])
  Next
  Return sReturnArray
End
In case this is my last post here for this year, I'd like to say that I've found this forum to be very helpful over the years so thank you all you responders!
And while I'm at it, the Seasons Greetings to you!
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
stevedee is in the usergroup ‘Regular’

bill-lancaster said

…Bear in mind I'm looking for an array of file names sorted by each file's LastChange value.
I found this posting from way back. Its 'messy'…

From the Dir() in your title, I assumed you didn't have the LastModified date.
If you are grabbing the file name and LastModified date at the same time, I'd recommend you save both in a 2dimentional array. That way you can re-order the list easily.


In case this is my last post here for this year, I'd like to say that I've found this forum to be very helpful over the years so thank you all you responders!
And while I'm at it, the Seasons Greetings to you!

…and a Merry Christmas to you too Bill!
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Guru
cogier is in the usergroup ‘Guru’
My solution to this may be a little unconventional, but it works.

I have just seen Steve's input. His idea of a 2 dimensional array is similar but slightly different to my solution.

Get the list of the file names in an array. sFiles = Dir(User.Home)
Add to each file name in the array its Last Modified Date and a separator(`) so IMG_1234.png becomes 2020/12/23 16:00:00`IMG_1234.png
Sorting the array will put it in date order.
Display the array, splitting each item by its separator.

<IMG src="https://www.cogier.com/gambas/FileDateOrder.png"> </IMG>

Run this in a Graphical application

Code (gambas)

  1. ' Gambas class file
  2.  
  3. sFiles As String[]
  4. GridViewFiles As GridView
  5.  
  6. Public Sub Form_Open()
  7.  
  8.   Dim sPath As String = User.Home &/ "Music"
  9.   Dim iLoop As Integer
  10.  
  11.   sFiles = Dir(sPath)
  12.   SetUpForm
  13.  
  14.   For iLoop = 0 To sFiles.Max
  15.     sFiles[iLoop] = Format(Stat(sPath &/ sFiles[iLoop]).LastModified, "yyyy/mm/dd hh:nn:ss") & "`" & sFiles[iLoop]
  16.   Next
  17.  
  18.   'sFiles.Sort(gb.Ascent) 'Latest at the bottom
  19.   sFiles.Sort(gb.Descent) 'Latest at the top
  20.  
  21.  
  22. Public Sub GridViewFiles_Data(Row As Integer, Column As Integer)
  23.  
  24.   GridViewFiles[Row, Column].Text = Split(sFiles[Row], "`")[Column]
  25.   If Row = 0 And Column = 1 Then GridViewFiles.Columns.Width = -1
  26.  
  27.  
  28. Public Sub SetUpForm()
  29.  
  30.   With Me
  31.     .Width = 750
  32.     .Height = 550
  33.     .Arrangement = Arrange.Vertical
  34.     .Padding = 5
  35.     .Text = "Sort files by date"
  36.  
  37.   With GridViewFiles = New GridView(Me) As "GridViewFiles"
  38.     .Header = GridView.Both
  39.     .Expand = True
  40.     .Rows.Count = sFiles.Count
  41.     .Columns.Count = 2
  42.     .Columns[0].Text = "Time and date"
  43.     .Columns[1].Text = "File name"
  44.  
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’
or don't use gambas Dir and bother doing any sorting, use shell dir command with -1t args , 1 makes file per line , t sorts by time , all taken care of :)

Code (gambas)

  1. Dim sOutput As String
  2. Shell "dir -1t /my/dir/path" To sOutput
  3. Dim MyAlreadySortedArray As String[] = Split(sOutput,"\n")
  4.  
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Guru
cogier is in the usergroup ‘Guru’

BruceSteers said

or don't use gambas Dir and bother doing any sorting, use shell dir command with -1t args , 1 makes file per line , t sorts by time , all taken care of :)

Code (gambas)

  1. Dim sOutput As String
  2. Shell "dir -1t /my/dir/path" To sOutput
  3. Dim MyAlreadySortedArray As String[] = Split(sOutput,"\n")
  4.  

It has it's down sides: -

3C-3E-EG.mp3
Ode\ to\ Joy.mp3
Brake1.mp3
Lady\ Madonna.mp3
Track\ 1.mp3
vlc-record-2019-04-16-09h00m04s-rattle.mp3-.mp3
vlc-record-2019-04-16-08h59m55s-rattle.mp3-.mp3
Charles\ Bolt\ -\ Far\ and\ Beyond-wce9cuixaM0.flac
Charles\ Bolt\ -\ Far\ and\ Beyond-wce9cuixaM0.opus
Brad\ Sucks\ -\ Making\ Me\ Nervous.mp3
Lady\ Madonna.wav
Peer\ Gynt.mmf
Track\ 1.wav
Sweet\ child\ of\ mine\ -\ Jizzy\ Pearl.ogg
Alice\ Cooper\ -\ Poison.ogg
ZAlice\ Cooper\ -\ Poison.ogg
Ann\ Nesby\ -\ I\ Can't\ Explain\ It.mp3
Barbara\ Dickson\ -\ Caravans.mp3
Here\ Is\ The\ News.m4a
Last\ Train\ To\ London.m4a
Brad\ Sucks\ -\ Dropping\ out\ of\ school.ogg
Duffy\ -\ Stepping\ Stone.ogg
15.\ Twist\ in\ My\ Sobriety.ogg
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’

cogier said

BruceSteers said

or don't use gambas Dir and bother doing any sorting, use shell dir command with -1t args , 1 makes file per line , t sorts by time , all taken care of :)

Code (gambas)

  1. Dim sOutput As String
  2. Shell "dir -1t /my/dir/path" To sOutput
  3. Dim MyAlreadySortedArray As String[] = Split(sOutput,"\n")
  4.  

It has it's down sides: -

3C-3E-EG.mp3
Ode\ to\ Joy.mp3
Brake1.mp3
Lady\ Madonna.mp3
Track\ 1.mp3
vlc-record-2019-04-16-09h00m04s-rattle.mp3-.mp3
vlc-record-2019-04-16-08h59m55s-rattle.mp3-.mp3
Charles\ Bolt\ -\ Far\ and\ Beyond-wce9cuixaM0.flac
Charles\ Bolt\ -\ Far\ and\ Beyond-wce9cuixaM0.opus
Brad\ Sucks\ -\ Making\ Me\ Nervous.mp3
Lady\ Madonna.wav
Peer\ Gynt.mmf
Track\ 1.wav
Sweet\ child\ of\ mine\ -\ Jizzy\ Pearl.ogg
Alice\ Cooper\ -\ Poison.ogg
ZAlice\ Cooper\ -\ Poison.ogg
Ann\ Nesby\ -\ I\ Can't\ Explain\ It.mp3
Barbara\ Dickson\ -\ Caravans.mp3
Here\ Is\ The\ News.m4a
Last\ Train\ To\ London.m4a
Brad\ Sucks\ -\ Dropping\ out\ of\ school.ogg
Duffy\ -\ Stepping\ Stone.ogg
15.\ Twist\ in\ My\ Sobriety.ogg

okay use the -N flag too to remove the quotes

-1Nt
Online now: No Back to the top

Post

Posted
Rating:
#10
Guru
BruceSteers is in the usergroup ‘Guru’
 Or there's ..
-Q, –quote-name           enclose entry names in double quotes
      –quoting-style=WORD   use quoting style WORD for entry names:
                               literal, locale, shell, shell-always,
                               shell-escape, shell-escape-always, c, escape
                               (overrides QUOTING_STYLE environment variable)


Of course you then just have to run through the array and remove any dirs or other filetypes you want to omit.

Not sure if dir supports just showing filenames?

you could grep filetypes from the output
"dir -1Nt /My/Dir/Path|grep .mp3"
Online now: No Back to the top
1 guest and 0 members have just viewed this.