DIR() sort by file LastChange

Post your Gambas programming questions here.
Post Reply
bill-lancaster
Posts: 195
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

DIR() sort by file LastChange

Post by bill-lancaster »

What is the best way (if any) to have an array of files sorted by date?
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: DIR() sort by file LastChange

Post by stevedee »

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.
User avatar
BruceSteers
Posts: 1575
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: DIR() sort by file LastChange

Post by BruceSteers »

A GridView has sorting (sort of)

You have to add your own sorting routine to your code. wiki shows how http://gambaswiki.org/wiki/comp/gb.qt4/gridview/sorted

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.
If at first you don't succeed , try doing something differently.
BruceS
bill-lancaster
Posts: 195
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: DIR() sort by file LastChange

Post by bill-lancaster »

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!
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: DIR() sort by file LastChange

Post by stevedee »

bill-lancaster wrote: Wednesday 23rd December 2020 3:21pm ...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!
User avatar
cogier
Site Admin
Posts: 1126
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: DIR() sort by file LastChange

Post by cogier »

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.

Image

Run this in a Graphical application
' Gambas class file

sFiles As String[]
GridViewFiles As GridView

Public Sub Form_Open()

  Dim sPath As String = User.Home &/ "Music"
  Dim iLoop As Integer

  sFiles = Dir(sPath)
  SetUpForm

  For iLoop = 0 To sFiles.Max
    sFiles[iLoop] = Format(Stat(sPath &/ sFiles[iLoop]).LastModified, "yyyy/mm/dd hh:nn:ss") & "`" & sFiles[iLoop]
  Next

  'sFiles.Sort(gb.Ascent) 'Latest at the bottom
  sFiles.Sort(gb.Descent) 'Latest at the top
  
End

Public Sub GridViewFiles_Data(Row As Integer, Column As Integer)

  GridViewFiles[Row, Column].Text = Split(sFiles[Row], "`")[Column]
  If Row = 0 And Column = 1 Then GridViewFiles.Columns.Width = -1

End

Public Sub SetUpForm()

  With Me
    .Width = 750
    .Height = 550
    .Arrangement = Arrange.Vertical
    .Padding = 5
    .Text = "Sort files by date"
  End With

  With GridViewFiles = New GridView(Me) As "GridViewFiles"
    .Header = GridView.Both
    .Expand = True
    .Rows.Count = sFiles.Count
    .Columns.Count = 2
    .Columns[0].Text = "Time and date"
    .Columns[1].Text = "File name"
  End With

End
User avatar
BruceSteers
Posts: 1575
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: DIR() sort by file LastChange

Post by BruceSteers »

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 :)
Dim sOutput As String
Shell "dir -1t /my/dir/path" To sOutput
Dim MyAlreadySortedArray As String[] = Split(sOutput,"\n")
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1126
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: DIR() sort by file LastChange

Post by cogier »

BruceSteers wrote: Wednesday 23rd December 2020 5:14pm 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 :)
Dim sOutput As String
Shell "dir -1t /my/dir/path" To sOutput
Dim MyAlreadySortedArray As String[] = Split(sOutput,"\n")
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
User avatar
BruceSteers
Posts: 1575
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: DIR() sort by file LastChange

Post by BruceSteers »

cogier wrote: Wednesday 23rd December 2020 5:23pm
BruceSteers wrote: Wednesday 23rd December 2020 5:14pm 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 :)
Dim sOutput As String
Shell "dir -1t /my/dir/path" To sOutput
Dim MyAlreadySortedArray As String[] = Split(sOutput,"\n")
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
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1575
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: DIR() sort by file LastChange

Post by BruceSteers »

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"
Last edited by BruceSteers on Wednesday 23rd December 2020 6:27pm, edited 1 time in total.
If at first you don't succeed , try doing something differently.
BruceS
Post Reply