Dir descending

General FreeBASIC programming questions.
Post Reply
darwin4ever
Posts: 40
Joined: Feb 03, 2017 22:40

Dir descending

Post by darwin4ever »

I have a question related to the Dir function

As far I can see Dir always returns the results in ascending filename order.

Maybe I overlooked, but I can't find a way to instruct Dir to provide the results in descending order (on filename and/or date-time)

Is that not possible ? Any workaround ? ......

Thanks
St_W
Posts: 1634
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Dir descending

Post by St_W »

I don't think that DIR guarantees any order at all - they don't have to be in ascending order.
You have to sort the results yourself in any case.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Dir descending

Post by MrSwiss »

WORKAROUND:
The dir (shell) command does sort the output (default = alphabetical [a - z]), the sort order
can be specified, with [optional] command line switches (arguments):

dir -- command line arguments, are:

Code: Select all

C:\Windows\system32>dir /?
Displays a list of files and subdirectories in a directory.

DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
  [/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

  [drive:][path][filename]
              Specifies drive, directory, and/or files to list.

  /A          Displays files with specified attributes.
  attributes   D  Directories                R  Read-only files
               H  Hidden files               A  Files ready for archiving
               S  System files               I  Not content indexed files
               L  Reparse Points             -  Prefix meaning not
  /B          Uses bare format (no heading information or summary).
  /C          Display the thousand separator in file sizes.  This is the
              default.  Use /-C to disable display of separator.
  /D          Same as wide but files are list sorted by column.
  /L          Uses lowercase.
  /N          New long list format where filenames are on the far right.
  /O          List by files in sorted order.
  sortorder    N  By name (alphabetic)       S  By size (smallest first)
               E  By extension (alphabetic)  D  By date/time (oldest first)
               G  Group directories first    -  Prefix to reverse order
  /P          Pauses after each screenful of information.
  /Q          Display the owner of the file.
  /R          Display alternate data streams of the file.
  /S          Displays files in specified directory and all subdirectories.
  /T          Controls which time field displayed or used for sorting
  timefield   C  Creation
              A  Last Access
              W  Last Written
  /W          Uses wide list format.
  /X          This displays the short names generated for non-8dot3 file
              names.  The format is that of /N with the short name inserted
              before the long name. If no short name is present, blanks are
              displayed in its place.
  /4          Displays four-digit years

Switches may be preset in the DIRCMD environment variable.  Override
preset switches by prefixing any switch with - (hyphen)--for example, /-W.

C:\Windows\system32>
By writing the result to a file, then read it in, you can avoid FB's dir Function.
As well as calling any sort of sort-algo.

Code: Select all

dir /O:[options] > dir.txt
darwin4ever
Posts: 40
Joined: Feb 03, 2017 22:40

Re: Dir descending

Post by darwin4ever »

Is there really no way to have that kind of functionality without having to use an external file ?

Maybe someone knows of an alternative (library) for dir() ?
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Dir descending

Post by grindstone »

darwin4erver wrote:Is there really no way to have that kind of functionality without having to use an external file ?
You could use an array instead:

Code: Select all

ReDim As String dirlist(1)
Dim As boolean sorted = TRUE
Dim As Integer x

dirlist(1) = Dir("C:\*", -1)
Do
	ReDim Preserve dirlist(UBound(dirlist) + 1)
	dirlist(UBound(dirlist)) = Dir("",-1)
Loop While Len(dirlist(UBound(dirlist)))
ReDim Preserve dirlist(UBound(dirlist) - 1)

'unsorted
For x = 1 To UBound(dirlist)
	Print x;" ";dirlist(x)
Next

'if required, replace with an other sorting algo that fits your needs
Do
	sorted = TRUE
	For x = 1 To UBound(dirlist) - 1
		If dirlist(x) > dirlist(x + 1) Then
			Swap dirlist(x), dirlist(x + 1)
			sorted = FALSE
		EndIf
	Next
Loop Until sorted

Print
'sorted
For x = 1 To UBound(dirlist)
	Print x;" ";dirlist(x)
Next

Sleep
St_W
Posts: 1634
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Dir descending

Post by St_W »

darwin4erver wrote:Is there really no way to have that kind of functionality without having to use an external file ?
Sure there is - just store it and sort it yourself as I've already told you in my previous post. You don't need an external file for that; just store in memory.

It looks like you're missing important basics, so please see:
https://en.wikipedia.org/wiki/Sorting
https://en.wikipedia.org/wiki/Sorting_algorithm
darwin4ever
Posts: 40
Joined: Feb 03, 2017 22:40

Re: Dir descending

Post by darwin4ever »

St_W wrote:It looks like you're missing important basics, so please see:
https://en.wikipedia.org/wiki/Sorting
https://en.wikipedia.org/wiki/Sorting_algorithm
Certainly not, having developed professionally and privatly all my life :-)
I perfectly know all kind of sorting algorithmes and their strenghts / weaknesses

But, I'm completely new to FreeBasic and "lazy", meaning I don't want to reinvent the wheel when somewhere there is a function (library) which does what I need (and maybe more).
dodicat
Posts: 8236
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Dir descending

Post by dodicat »

You can keep within the dos/dir realm and use shell with the dos sort with the reverse flag.

example:

Code: Select all

shell "dir >dirfiles.txt" 'create a file for dir output
shell "type dirfiles.txt" 'show the file

var cd=curdir             'this file is in curdir         
var cmd= "SORT/r " + cd + "\dirfiles.txt /o " +cd +"\result.txt"  'SORT/r = sort in reverse order into result.txt
shell cmd  'execute the command
print "_______________________________________________________"
shell "type "+cd+"\result.txt" 'have a look see.


'delete these two files when done with them -- optional
'kill "dirfiles.txt"
'kill "result.txt"
sleep

 
OK on Win 10.
OSchmidt
Posts: 49
Joined: Jan 01, 2016 12:27
Contact:

Re: Dir descending

Post by OSchmidt »

darwin4erver wrote:... I don't want to reinvent the wheel when somewhere there is a function (library) which does what I need (and maybe more).
Of course there are libs out there, which can do that more easily, e.g. like that (though it's VBCode):

Code: Select all

Dim DirList, i
Set DirList = New_c.FSO.GetDirList("C:\temp", dlSortByLastWriteTime)
 
For i = DirList.FilesCount - 1 To 0 Step -1  'reverse enumeration of the sorted content
  Debug.Print DirList.FileName(i)
Next
The above could be adapted to FreeBasic (with a COM-lib helper) - but it would be Win32-only then.

Though there's also libs more native to FreeBasic of course, like the FB-extended-library: http://ext.freebasic.net

With the extended lib, you could write grindstones example this way (corrected with regards to the placment of the While-condition):

Code: Select all

#include once "ext/containers/array.bi"

var DirList = ext.fbext_Array(((String)))
    DirList.PushBack Dir("C:\Code\*")
        Do While Len(*DirList.Back): DirList.PushBack Dir: Loop 
    DirList.PopBack 
Print "Files found: "; DirList.Size
    DirList.Sort
    For i As Long = DirList.Size - 1 To 0 Step -1 'reverse enumeration of the sorted content
        Print i, *DirList.at(i)
    Next
Sleep
HTH

Olaf
darwin4ever
Posts: 40
Joined: Feb 03, 2017 22:40

Re: Dir descending

Post by darwin4ever »

Folks, thank you very much for your suggestions !

As I preferred a solution without an external file, I adapted the very helpful grindstones example to my needs.
To be honest, I only saw the much shorter Oschmidt solution after having finished that, but will certainly look into it (and into the commands used)
In the meanwhile I have a working version, which is great.
Post Reply