Dir descending
-
- Posts: 40
- Joined: Feb 03, 2017 22:40
Dir descending
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
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
Re: Dir descending
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.
You have to sort the results yourself in any case.
Re: Dir descending
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: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.
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>
As well as calling any sort of sort-algo.
Code: Select all
dir /O:[options] > dir.txt
-
- Posts: 40
- Joined: Feb 03, 2017 22:40
Re: Dir descending
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() ?
Maybe someone knows of an alternative (library) for dir() ?
-
- Posts: 862
- Joined: May 05, 2015 5:35
- Location: Germany
Re: Dir descending
You could use an array instead:darwin4erver wrote:Is there really no way to have that kind of functionality without having to use an external file ?
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
Re: Dir descending
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.darwin4erver wrote:Is there really no way to have that kind of functionality without having to use an external file ?
It looks like you're missing important basics, so please see:
https://en.wikipedia.org/wiki/Sorting
https://en.wikipedia.org/wiki/Sorting_algorithm
-
- Posts: 40
- Joined: Feb 03, 2017 22:40
Re: Dir descending
Certainly not, having developed professionally and privatly all my life :-)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
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).
Re: Dir descending
You can keep within the dos/dir realm and use shell with the dos sort with the reverse flag.
example:
OK on Win 10.
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
Re: Dir descending
Of course there are libs out there, which can do that more easily, e.g. like that (though it's VBCode):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).
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
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
Olaf
-
- Posts: 40
- Joined: Feb 03, 2017 22:40
Re: Dir descending
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.
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.