FileItemize

Started by mueer01, November 06, 2013, 06:36:45 AM

Previous topic - Next topic

mueer01

Hello,

is there any way to get the
filelist = FileItemize("*.txt")
sorted by Modify Date, instead of filename?

Regards,
Erhard

Deana

You are in luck there is already a User Defined Function for this in the tech database. The basic trick is to use FileInfotoArray to read the data into an array, then sort it using ArraySort.:

Code (winbatch) Select

DefineFunction FileItemizeByDate(filemasks,option)
   ; Listing files by date
   ; Sort files by Y:M:D:H:M:S (date)
   ; Option @Ascending lists oldest to newest,@Descending lists newest to oldest
   arrFiles=FileInfoToArray(filemasks) ;retrieves unsorted list
   ; Remove header column
   ArrayRemove( arrFiles, 0, 1)
   ;Sort the array on the modified column
   ArraySort( arrFiles, option, 2 )
   ;Create a tabdelimited list of file names
   filelist = ''
   For xx = 0 To ArrInfo(arrFiles, 1 )-1
      dafile =  arrFiles[xx,0]
      If filelist == "" Then filelist = dafile
      Else filelist = filelist:@TAB:dafile
   Next
   Return filelist
#EndFunction


DirChange('D:\temp')
filelist = FileItemizeByDate('*',@ASCENDING) ; oldest to newest
result = AskItemlist("Files: oldest to newest", filelist, @TAB, @UNSORTED, @SINGLE )


Reference: http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WinBatch/Arrays+Sort~Files~by~Modified~Date.txt
Deana F.
Technical Support
Wilson WindowWare Inc.

mueer01


Secondlaw

Hi,

I'd like to use this to search for wildcards rather than an entire directory.  I'd only like for this to return files based on the criteria I give it....  *.txt or *.bat or DC*.log...etc....

How would I modify this to do that?  This directory has over a million files in it and using the code the way it sits hangs my system for a while.

Thanks,

td

The script already only returns files that match the indicated file masks.   And if you are processing a million or more files it is simply going to take quite awhile.  If you want it to return faster you will need to reduce what the script is doing.  For example, do you need an item  list sorted by date?
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Secondlaw

Quote from: td on October 27, 2015, 12:54:42 PM
The script already only returns files that match the indicated file masks.   And if you are processing a million or more files it is simply going to take quite awhile.  If you want it to return faster you will need to reduce what the script is doing.  For example, do you need an item  list sorted by date?
Let's say I have 100 files in a given directory.  Out of those files, 26 of them start with, "dbus"; I'd like for only those files to be returned and sorted by date and possibly even have the date of the file shown in the output screen.

td

If you look at the Consolidated WIL Help file documentation for the FileInfoToArray function you will see that it accepts file masks so all you have to do is pass the mask "dbus*.*" (assuming the files have an extension) to the above UDF.      If you want the UDF to return the date along with the file name, you have many options. You could concatenate the date to the file name before adding it to the returned list or create a two column array with one column for each file name and the other for the date.   Or you could simply return the sorted arrFiles array.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Secondlaw

Quote from: td on October 28, 2015, 06:40:17 AM
If you look at the Consolidated WIL Help file documentation for the FileInfoToArray function you will see that it accepts file masks so all you have to do is pass the mask "dbus*.*" (assuming the files have an extension) to the above UDF.      If you want the UDF to return the date along with the file name you have many options. You could concatenate the date to the file name before adding it to the returned list or create a two column array with one column for each file name and the other for the date.   Or you could simply return the sorted arrFiles array.
Ok, thank You.  I'll take a look and give it a try.  As usual, you guys are the best.

Secondlaw

So this is what I have:

FileList = fileitemize (Username)

infoarray = FileInfoToArray(filelist, 0)
infostr=(" ")

For xx = 1 To infoarray[0,0]
   infostr = StrCat(infostr,"",infoarray[xx,0],@CRLF)
   infostr = StrCat(infostr,"",infoarray[xx,4],@CRLF)
Next
Dropdown = askitemlist ("Log Files", infostr, @TAB, @UNSORTED, @SINGLE, @FALSE)

I'd just like to have each item listed on its own line in the "Askitemlist".  I'm having trouble with this.  Everything is on 1 line.  Any suggestions?

JTaylor

You are using @CRLF as your delimiter in building the string but @TAB is the delimiter specified in the AskItemList.

Jim

td

Another approach.

Code (winbatch) Select
; No reason to call this function because
; FileInforToArray accepts mask.
; see the help file.
;FileList = fileitemize (strMask)
strMask = "C:\Temp\*.wbt"
aFileInfo = FileInfoToArray(strMask, 0)
ArraySort(aFileInfo, @ASCENDING, 4, 1)
aFileInfo[0,0] = "File Name"
aFileInfo[0,4] = "File Create Date"

; Dialog callback subroutine.
#DefineSubroutine FileInfoProc(FileInfo_Handle,FileInfo_Event,FileInfo_Name,FileInfo_EventInfo,FileInfo_ChangeInfo)
   switch FileInfo_Event                                   
      case @deInit                                         
         ;Remove unwanted columns.
         DialogControlSet(FileInfo_Handle, "ReportView_1", @dcRemoveCol, 6)
         DialogControlSet(FileInfo_Handle, "ReportView_1", @dcRemoveCol, 6)
         DialogControlSet(FileInfo_Handle, "ReportView_1", @dcRemoveCol, 2)
         DialogControlSet(FileInfo_Handle, "ReportView_1", @dcRemoveCol, 2)
         DialogControlSet(FileInfo_Handle, "ReportView_1", @dcRemoveCol, 2)

         ; Set column widths.
         DialogControlSet(FileInfo_Handle, "ReportView_1", @dcColWidth, "-1 -2")

   endswitch                                               
   return(@retDefault)
#EndSubroutine                                             

FileInfoFormat=`WWWDLGED,6.2`

FileInfoCaption=`File Information`
FileInfoX=880
FileInfoY=123
FileInfoWidth=182
FileInfoHeight=160
FileInfoNumControls=003
FileInfoProcedure=`FileInfoProc`
FileInfoFont=`DEFAULT`
FileInfoTextColor=`DEFAULT`
FileInfoBackground=`DEFAULT,DEFAULT`
FileInfoConfig=0

FileInfo001=`022,140,033,011,PUSHBUTTON,"PushButton_OK",DEFAULT,"OK",1,10,@csDefButton,DEFAULT,DEFAULT,DEFAULT`
FileInfo002=`126,138,033,011,PUSHBUTTON,"PushButton_Cancel",DEFAULT,"Cancel",0,20,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
FileInfo003=`001,007,174,125,REPORTVIEW,"ReportView_1",aFileInfo,DEFAULT,DEFAULT,30,@csAsort|@csFirstHeader,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("FileInfo")

if ArrInfo(aFileInfo, 0) then Message( "Selected File", aFileInfo[0,0])
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade