WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Arrays
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

DirInfotoArray Sort


Question:

Is is possible to move thru an array created by DirInfotoArray in the order that the dir were created? I need to build a list of directories and loop thru them from newest to oldest (or vice versa)so that I can delete their contents and then remove them (except for the last 2 that were created)? If I just loop "for xx=1 to infoarray[0,0]" then it works thru the array in alphabetic order of dir name! Or is there another way to achieve this? Maybe sorting directories before creating the list?

Answer:

Here is some code the uses the array sort UDFs in the tech support database http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/Arrays+Array~Sort~UDFs~from~Akreutzer.txt

;*************************************************************************
;
;   Array sort for two-dimensional arrays.
;
;   Uses the "QuickSort" algorithm, modified to
;   preserve the original row order for equal keys.
;
;   Array ....... The array to be sorted
;   KeyColumn ... The column to be used as the sort key
;
;   The return value is a copy of the original array, sorted.
;   (The original array is not modified.)
;
;**************************************************************************
#DefineFunction ArraySort(Array, KeyCol)
  TopRow = ArrInfo(Array,1)-1
  TopCol = ArrInfo(Array,2)-1
  KeyArr = ArrDimension(TopRow+1,2)
  For x = 0 To TopRow
       KeyArr[x,0] = Array[x,KeyCol]
       KeyArr[x,1] = x
  Next
  ArraySortKeys(KeyArr,0,TopRow)
  NewArr = ArrDimension(TopRow+1,TopCol+1)
  For x = 0 To TopRow
     R = KeyArr[x,1]
     For y = 0 To TopCol
         NewArr[x,y] = Array[R,y]
     Next
  Next
  Drop(KeyArr)
  Return NewArr
#EndFunction

;-----------------------------------------------------------
; Called recursively to sort the key array.
;   item[x,0] of the array is the sort key.
;   item[x,1] is the original row number, and is used
;             as a tie-breaker if the keys are equal.
;-----------------------------------------------------------
#DefineFunction ArraySortKeys(Array, LowBound, HighBound)
  If HighBound <= LowBound Then Return

  xLow = LowBound
  xHigh = HighBound
  xPivot = Int((LowBound + HighBound) / 2)
  PivotKey = Array[xPivot,0]
  PivotRow = Array[xPivot,1]

  While (xLow <= xHigh)
     While ArraySortArrayLess(xLow)
        xLow = xLow + 1
        If xLow == HighBound Then Break
     EndWhile
     While ArraySortPivotLess(xHigh)
         xHigh = xHigh - 1
         If xHigh == LowBound Then Break
     EndWhile
     If xLow < xHigh
         Temp = Array[xLow,0]
         Array[xLow,0] = Array[xHigh,0]
         Array[xHigh,0] = Temp
         Temp = Array[xLow,1]
         Array[xLow,1] = Array[xHigh,1]
         Array[xHigh,1] = Temp
     EndIf
     If xLow <= xHigh
         xLow = xLow + 1
         xHigh = xHigh - 1
     End If
  EndWhile

  ArraySortKeys(Array, LowBound, xHigh)
  ArraySortKeys(Array, xLow, HighBound)
#EndFunction

#DefineSubRoutine ArraySortArrayLess(xLess)
    If Array[xLess,0] < PivotKey Then Return @TRUE
    If Array[xLess,0] > PivotKey Then Return @FALSE
    Return Array[xLess,1] < PivotRow
#EndSubRoutine
#DefineSubRoutine ArraySortPivotLess(xLess)
    If PivotKey < Array[xLess,0] Then Return @TRUE
    If PivotKey > Array[xLess,0] Then Return @FALSE
    Return PivotRow < Array[xLess,1]
#EndSubRoutine



dirlist = StrCat(ShortCutDir("Personal",0,@TRUE),"*.*")
infoarray = DirInfoToArray(dirlist, 1|2)
ArrayFilePutCSV('C:\temp\beforesort.txt', infoarray)

newarray = ArraySort(infoarray, 4)
ArrayFilePutCSV('C:\temp\aftersort.txt', newarray)
Exit
OR

Try this:

theArray = DirInfoToArray(Param1)
ArrayFilePutCSV("tmptmp",theArray)
Pause("Original array",FileGet("tmptmp"))
FileDelete("tmptmp")

str = ""
n0 = theArray[0,0]
For I = 1 To n0
    str = StrCat(str,theArray[I,2],@TAB,I,@LF)
Next
str = ItemSort(StrSub(str,1,StrLen(str)-1),@LF)
finalStr = ""
For I = 1 To n0
    idx = ItemExtract(2,ItemExtract(I,str,@LF),@TAB)
    finalStr = StrCat(finalStr,theArray[idx,0],@TAB,theArray[idx,2],@CRLF)
Next
Pause("At end...",finalStr)

Article ID:   W17383
File Created: 2014:07:18:09:50:34
Last Updated: 2014:07:18:09:50:34