WBStudio.ForEachCollection
;----------------------------------------------------------------------------------------------------------------------
; Interactive Utility for use in WinBatch Studio only.
; Converts from Visual Basic Control Structure:
;    "For Each ... In ... ... Next"
; to WinBatch Control Structure:
;    "hEnum = ObjectCollectionOpen(objecthandle) ... While ... EndWhile"
;----------------------------------------------------------------------------------------------------------------------
; For easy use just insert two lines into WSPOPUP.MNU, e.g.:
; _WBStudioForEachCollection \ {F7}
;     Call("W:\WBT\UDF\WBStudioForEachCollection.wbt","")
;
; Modify menu entry, filename, folderpath and hotkey to your needs.
;
; For undoing of replacements use standard hotkey Ctrl-Z
; or the symbol resp. item from WinBatch Studio menu.
;
; Search starts at current cursor position in forward direction.
;----------------------------------------------------------------------------------------------------------------------
; Detlev Dalitz.20020706.20020717
;----------------------------------------------------------------------------------------------------------------------

iCount = 0
iCurrentLine = wGetLineNo()
iCurrentCol  = wGetColNo()

wfPattern   = "for +each +(.+) +in +(.+)"
wfForward   = @TRUE
wfMatchCase = @FALSE
wfRegExp    = @TRUE
wfWrap      = @FALSE
wfResult    = wFind(wfPattern,wfForward,wfMatchCase,wfRegExp,wfWrap)

If !wfResult Then Goto CANCEL

Pause("WBStudioForEachCollection","Create WIL 'ObjectCollectionOpen' Loop?")

ParseData (wGetWord()) ; we need param3 and param5 as var names

iStartLine = ItemExtract(1,wSelInfo(),@TAB)
iStartCol  = ItemExtract(2,wSelInfo(),@TAB)
iIndent    = iStartCol + 3

wHome()
wStartSel()
wEnd()
wEndSel()
sLineHead = wGetWord()
wDelete()

wfPattern   = " *next *"
wfForward   = @TRUE
wfMatchCase = @FALSE
wfRegExp    = @TRUE
wfWrap      = @FALSE
wfResult    = wFind(wfPattern,wfForward,wfMatchCase,wfRegExp,wfWrap)

wHome()
wStartSel()
wEnd()
wEndSel()
sLineFoot = wGetWord()
wDelete()

wEnd()
wStartSel()
wHome()
wGotoLine(iStartLine)
wEndSel()
wCut()

wGotoCol(iStartCol)
wInsString(";")
wInsLine(StrTrim(sLineHead))
wGotoCol(iStartCol)
wInsLine('hEnum_%param3% = ObjectCollectionOpen(%param5%)')
wInsLine('While 1')
wGotoCol(iIndent)
wInsLine('%param3% = ObjectCollectionNext(hEnum_%param3%)')
wInsLine('If !%param3% Then Break')
iLineBody = wGetLineNo()
wNewLine()
wGotoCol(iIndent)
wInsLine('ObjectClose(%param3%)')
wGotoCol(iStartCol)
wInsLine('EndWhile')
wInsLine('ObjectCollectionClose(hEnum_%param3%)')
wInsLine('Drop(%param3%,hEnum_%param3%)')
wGotoCol(iStartCol)
wInsString(";")
wInsString(StrTrim(sLineFoot))
wGotoLine(iLineBody)
wHome()
wPaste()
iCount = 1

:CANCEL
wClearSel()
wGotoLine(iCurrentLine)
wGotoCol(iCurrentCol)
Message("WBStudioForEachCollection",StrCat("Replaced items: ",iCount))

Drop(iCount,iCurrentCol,iCurrentLine,iIndent,iLineBody,iStartCol,iStartLine)
Drop(sLineHead,sLineFoot,wfForward,wfMatchCase,wfPattern,wfRegExp,wfResult,wfWrap)
DropWild("param*")

If (IntControl(77,80,0,0,0) > 0) Then Return
Exit
;----------------------------------------------------------------------------------------------------------------------



; --- test area ---

; Visual Basic control structure

    If Files.Count <> 0 Then
         For Each File In Files
            S = S & GenerateFileInformation(File)
         Next
    End If


; WinBatch control structure

    If Files.Count <> 0 Then
         ;For Each File In Files
         hEnum_File = ObjectCollectionOpen(Files)
         While 1
            File = ObjectCollectionNext(hEnum_File)
            If !File Then Break

            S = S & GenerateFileInformation(File)

            ObjectClose(File)
         EndWhile
         ObjectCollectionClose(hEnum_File)
         Drop(File,hEnum_File)
         ;Next
    End If
;----------------------------------------------------------------------------------------------------------------------