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

File Version and Dir Mgmt

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

File Info to Delimited Text File

 Keywords:  file info directory tab delimited text file 

This script requres two files: ArrayToDlmStr_WW.WIL and SaveCatalog_WW.WIL

ArrayToDlmStr_WW.WIL

#DefineFunction ArrayToDlmStr_WW ( paArray , pColumnDelimiter , pRowDelimiter , pStartRow , pStartCol )

;   ----------------------------------------------------------------------------
;
;   Function Name:      ArrayToDlmStr_WW
;
;   Synopsis:           Create a delimited string from the elements of an array.
;
;   Dependencies:       The following functions from the specified library
;                  module (include) files are called.
;
;
;   Arguments:         paArray           = Array to transform into a string.
;
;                                           Array paArray must be an array of
;                                           one or two dimensions. Arrays of
;                                           three or more dimensions are
;                                           unsupported.
;
;                       pColumnDelimiter  = Delimiter to insert between cells of
;                                 the same row of a rectangular array,
;                                 or of all the cells of a vector. If
;                                           delimiter is an empty string, @tab
;                                           is used.
;
;                  pRowDelimiter     = Delimiter to insert between rows of
;                                  a rectangular array. If delimiter
;                                           is an empty string, @lf is used.
;
;                  pStartRow         = Index of row of matrix, or element
;                                           of vector, from which to start.
;
;                                           If pStartRow is less than zero or
;                                           greater than the number of rows in
;                                           the matrix, start from row zero.
;
;                  pStartCol         = Index of column of matrix from which
;                                           to start.
;
;                                           Start column applies to ALL rows.
;
;                                           If pStartCol is less than zero or
;                                           greater than the number of columns
;                                           in the matrix, start from column
;                                           zero.
;
;   Returns:            rDlmStr           = Delimited string, or emptry string,
;                                  if the array is invalid or contains
;                                           three or more dimensions.
;
;   Notes:               1)   The last element of a vector is not terminated.
;
;                  2)   The last row of a matrix is not terminated.
;
;   Author:             David A. Gray of Simple Soft Services, Inc.,
;                  d/b/a WizardWrx, Irving, Texas, USA.
;
;   Copyright:          2009, Simple Soft Services, Inc., d/b/a WizardWrx,
;                  Irving, Texas, USA. All rights reserved world wide.
;
;   Date Written:       Saturday, 14 March 2009.
;
;    Revision History
;
;   Date       Who Version Synopsis
;   ---------- --- ------- -----------------------------------------------------
;   2009/03/14 DAG 1.00    Initial version, developed for WebInfoXfr.WBT.
;
;   2009/03/16 DAG 1.01    Rename from ArrayToDelimitedString_WW to
;                          ArrayToDlmStr_WW, and add symbolic constants for all
;                          critical tests.
;
;   ----------------------------------------------------------------------------

   Dummy                = IntControl ( 73 , 1 , 0 , 0 , 0 )         ; Enable error trapping.

    ARRINFO_DIMENSIONS      = 0      ; Number of dimensions in the array
    ARRINFO_IS_VALID      = -1   ; Checks if array is valid. Returns @TRUE if valid, @FALSE if invalid.
   ARRINFO_DIM_1         = 1      ; number of elements in dimension 1 (or 0 if unused)
   ARRINFO_DIM_2         = 2      ; number of elements in dimension 2 (or 0 if unused)
   ARRAY_MIN_INDEX_WW      = 0      ; The initial array index must be at least zero.
   ARRAY_IS_VECTOR_WW      = 1      ; Array is a vector. That is, it has but one dimension.
   ARRAY_IS_MATRIX_WW      = 2      ; Array is a matrix. That is, it has two dimensions.
   MSGBOX_ICON_CRITICAL   = 16
   NULL_STRING_P6C         = ''

   rDlmStr      = NULL_STRING_P6C

   If pColumnDelimiter == NULL_STRING_P6C
      ColDlm   = @TAB
   Else
      ColDlm   = pColumnDelimiter
   EndIf

   If pRowDelimiter == NULL_STRING_P6C
      RowDlm   = @LF
   Else
      RowDlm   = pRowDelimiter
   EndIf

   If ArrInfo ( paArray , ARRINFO_IS_VALID )
      NDims            = ArrInfo ( paArray , ARRINFO_DIMENSIONS )
      Switch NDims
         Case ARRAY_IS_VECTOR_WW
            NItems      = ArrInfo ( paArray , ARRINFO_DIM_1 )
            LastItem   = NItems - 1

            If pStartRow < ARRAY_MIN_INDEX_WW || pStartRow > LastItem
               StartItem   = ARRAY_MIN_INDEX_WW
            Else
               StartItem   = pStartRow
            EndIf

            For iCurrItem = StartItem To LastItem
               If iCurrItem < LastItem
                  rDlmStr   = StrCat ( rDlmStr , paArray [ iCurrItem ] , ColDlm )
               Else
                  rDlmStr   = StrCat ( rDlmStr , paArray [ iCurrItem ] )
               EndIf
            Next         ; iCurrItem

            Break

         Case ARRAY_IS_MATRIX_WW
            NRows      = ArrInfo ( paArray , ARRINFO_DIM_1 )
            NCols      = ArrInfo ( paArray , ARRINFO_DIM_2 )
            LastRow      = NRows - 1
            LastCol      = NCols - 1

            If pStartRow < ARRAY_MIN_INDEX_WW || pStartRow > LastRow
               StartRow   = ARRAY_MIN_INDEX_WW
            Else
               StartRow   = pStartRow
            EndIf

            If pStartCol < ARRAY_MIN_INDEX_WW || pStartCol > LastCol
               StartCol   = ARRAY_MIN_INDEX_WW
            Else
               StartCol   = pStartCol
            EndIf

            For iCurrRow = StartRow To LastRow
               For iCurrCol = StartCol To LastCol
                  If iCurrCol < LastCol
                     rDlmStr      = StrCat ( rDlmStr , paArray [ iCurrRow , iCurrCol ] , ColDlm )
                  Else
                     If iCurrRow < LastRow
                        rDlmStr   = StrCat ( rDlmStr , paArray [ iCurrRow , iCurrCol ] , RowDlm )
                     Else
                        rDlmStr   = StrCat ( rDlmStr , paArray [ iCurrRow , iCurrCol ] )
                     EndIf
                  EndIf
               Next      ; iCurrCol
            Next         ; iCurrRow

            Break

         Case NDims         ; Additional dimensions are unsupported.
            Break
      EndSwitch            ; NDims
   EndIf                  ; Argument paArray must be a valid WIL array.

   Goto ArrayToDlmStr_WW_End   ; Jump over error handler to exit.

:wberrorhandler

    WILErrorCode   = LastError ( )
    WILErrorString   = IntControl ( 34 , WILErrorCode , 0 , 0 , 0 )

    LogMsg         = StrCat ( 'ERROR: Function ArrayToDlmStr_WW FAILED.' , @LF , 'WIL Error code ' , WILErrorCode , ' - ' , WILErrorString , @LF , 'Line causing error:' , @LF , wberrorhandlerline )
   Dummy         = Pause ( FileRoot ( IntControl ( 1004 , 0 , 0 , 0 , 0 ) ) , LogMsg  )

:ArrayToDlmStr_WW_End

   If IsDefined ( LogMsg )
      Return LogMsg
   Else
      Return rDlmStr
   EndIf

#EndFunction


SaveCatalog_WW.WIL

#DefineFunction SaveCatalog_WW ( pDirToCatalog , pCatalogFilespec , pCatalogFQFN )

;   ----------------------------------------------------------------------------
;
;   Function Name:      SaveCatalog_WW
;
;   Synopsis:           Gather all file info for a directory into a tab
;                  delimited text file.
;
;   Dependencies:       The following functions from the specified library
;                  module (include) files are called.
;
;                       Function Name    Name of Library Module  Notes
;                       ===============  ======================  ===============
;                       ArrayToDlmStr_WW ArrayToDlmStr_WW.WIL    UNArrayize
;
;   Arguments:         pDirToCatalog     = Directory for which to construct a
;                                 catalog. If the string is blank, the
;                                 CWD is assumed.
;
;                       pCatalogFilespec  = File specification mask, identifying
;                                 files to include in the catalog. If
;                                 the string is blank, all files are
;                                 included.
;
;                  pCatalogFQFN      = Fully qualified name of catalog file.
;
;   Returns:            rFileCount        = Files in catalog, if successful.
;                                 Otherwise, a message string, from a
;                                 run-time error, is returned.
;
;   Notes:               None.
;
;   Author:             David A. Gray of Simple Soft Services, Inc.,
;                  d/b/a WizardWrx, Irving, Texas, USA.
;
;   Copyright:          2009, Simple Soft Services, Inc., d/b/a WizardWrx,
;                  Irving, Texas, USA. All rights reserved world wide.
;
;   Date Written:       Saturday, 14 March 2009.
;
;    Revision History
;
;   Date       Who Version Synopsis
;   ---------- --- ------- -----------------------------------------------------
;   2009/03/14 DAG 1.00    Initial version, developed for WebInfoXfr.WBT.
;
;   2009/03/16 DAG 1.01    Trim trailing spaces from the column labels, so that
;                          they work as column headings in a delimited ASCII
;                          file, especially when imported into Microsoft Excel.
;
;   2009/03/17 DAG 1.02    Update documentation of dependencies. The code is
;                          unchanged.
;
;   ----------------------------------------------------------------------------

   Dummy             = IntControl ( 73 , 1 , 0 , 0 , 0 )         ; Enable error trapping.

   rFileCount         = 0

   ALL_FILES_MSDOS_P6C   = '*.*'
   ARRAY_ORIGIN_ROW   = 0
   ARRAY_ORIGIN_COL   = 0
   ARRINFO_DIM_1      = 1

    FILEINFO_COL_FILE_NAME         = 0            ; Contains file name, which is fully qualified if flags & FILEINFO_FLAGS_RETURN_FQFN == @true
    FILEINFO_COL_FILE_SIZE         = 1            ; Contains file size, which is returned as a Huge number if flags & FILEINFO_FLAGS_HUGE_FILE_SIZE == @true
    FILEINFO_COL_FILE_MOD_DATE     = 2            ; Contains file last modified date, in YmdHms format
    FILEINFO_COL_FILE_ACC_DATE     = 3            ; Contains file last accessed date, in YmdHms format
    FILEINFO_COL_FILE_CRE_DATE     = 4            ; Contains file created date, in YmdHms format
    FILEINFO_COL_FILE_ATTR_STRING  = 5            ; Contains file attribute string in "RASH" format, same as returned by FileAttrGet
    FILEINFO_COL_FILE_ATTR_BITMASK = 6            ; Contains file attribute bit mask, same as returned by FileAttrGetEx

    FILEINFO_COL_LABELS            = ArrDimension ( FILEINFO_COL_FILE_ATTR_BITMASK + 1)
    FILEINFO_COL_LABELS [ FILEINFO_COL_FILE_NAME ]         = 'Name'
    FILEINFO_COL_LABELS [ FILEINFO_COL_FILE_SIZE ]         = 'Size'
    FILEINFO_COL_LABELS [ FILEINFO_COL_FILE_MOD_DATE ]     = 'Date Last Modified'
    FILEINFO_COL_LABELS [ FILEINFO_COL_FILE_ACC_DATE ]     = 'Date Last Accessed'
    FILEINFO_COL_LABELS [ FILEINFO_COL_FILE_CRE_DATE ]     = 'Date Created'
    FILEINFO_COL_LABELS [ FILEINFO_COL_FILE_ATTR_STRING ]  = 'Attribute String'
    FILEINFO_COL_LABELS [ FILEINFO_COL_FILE_ATTR_BITMASK ] = 'Attribute Bitmask'

    FILEINFO_FLAGS_DEFAULT         = 0
    FILEINFO_FLAGS_HUGE_FILE_SIZE  = 1            ; Affects type of values returned for FILEINFO_COL_FILE_SIZE and FILEINFO_META_FILE_TOTAL_SIZE
    FILEINFO_FLAGS_RETURN_FQFN     = 2            ; Affects values returned by FILEINFO_FLAGS_RETURN_FQFN

    FILEINFO_META_ROW              = 0            ; The first row (row zero) contains meta data.
   FILEINFO_FIRST_DATA_ROW        = 1            ; The first data row is row index 1.
    FILEINFO_META_FILE_COUNT       = 0            ; - Column 1 (array index value 0) contains the count of files found
    FILEINFO_META_FILE_TOTAL_SIZE  = 1            ; - Column 2 (array index value 1) contains the total size of files found

   MSGBOX_ICON_CRITICAL           = 16
   NULL_STRING_P6C                 = ''

   If pDirToCatalog == NULL_STRING_P6C
      CatDir         = DirGet ( )
   Else
      CatDir         = pDirToCatalog
   EndIf

   If pCatalogFilespec == NULL_STRING_P6C
      CatalogSpec      = ALL_FILES_MSDOS_P6C
   Else
      CatalogSpec      = pCatalogFilespec
   EndIf

   aCatalog         = FileInfoToArray ( StrCat ( CatDir , CatalogSpec ) , FILEINFO_FLAGS_DEFAULT )
   hCatalog         = FileOpen ( pCatalogFQFN , 'Write' )
   If hCatalog
      HeaderRow      = ArrayToDlmStr_WW ( FILEINFO_COL_LABELS , @TAB , @CRLF , ARRAY_ORIGIN_ROW , ARRAY_ORIGIN_COL )
      FileWrite ( hCatalog , HeaderRow )
      DetailRows      = ArrayToDlmStr_WW ( aCatalog , @TAB , @CRLF , FILEINFO_FIRST_DATA_ROW , ARRAY_ORIGIN_COL )
      FileWrite ( hCatalog , DetailRows )
      hCatalog      = FileClose ( hCatalog )
      rFileCount      = aCatalog [ FILEINFO_META_ROW , FILEINFO_META_FILE_COUNT ]
   Else
      LogMsg         = StrCat ( 'ERROR: Function SaveCatalog_WW FAILED' , @LF , 'Unable to create file ' , pCatalogFQFN )
   EndIf

   Goto SaveCatalog_WW_End                              ; Jump over error handler to exit.

:wberrorhandler

    WILErrorCode   = LastError ( )
    WILErrorString   = IntControl ( 34 , WILErrorCode , 0 , 0 , 0 )

    LogMsg         = StrCat ( 'ERROR: Function SaveCatalog_WW FAILED.' , @LF , 'WIL Error code ' , WILErrorCode , ' - ' , WILErrorString , @LF , 'Line causing error:' , @LF , wberrorhandlerline )
   Dummy         = Pause ( FileRoot ( IntControl ( 1004 , 0 , 0 , 0 , 0 ) ) , LogMsg  )

   If IsDefined ( hCatalog )
      If hCatalog
         hCatalog   = FileClose ( hCatalog )
      EndIf
   EndIf

:SaveCatalog_WW_End

   If IsDefined ( LogMsg )
      Return LogMsg
   Else
      Return rFileCount
   EndIf

#EndFunction

Article ID:   W18262
Filename:   File Info to Delimited Text File.txt
File Created: 2009:03:17:11:18:42
Last Updated: 2009:03:17:11:18:42