![]() |
Technical ArticlesWinBatch: Constants for Use with WIL Function FileInfoToArray |
David A. Gray, MBA
Chief Wizard, WizardWrx
Version 2004B of the WinBatch compiler introduces a neat new function,
FileInfoToArray, that returns essentially everything about the file that is kept in
its directory entry. For Unix and Perl programmers, this is equivalent to the information returned
by the stat function, run over an expanded file specification. This function returns
information gathered via Windows File Storage Subsystem API function GetFileAttributesEx,
transformed into strings that are more suitable for human consuption, yet still useful for
computation.
WIL include file FileInfo_CONSTANTS_P6C.WIL makes WIL function
FileInfoToArray easier to use by defining a small set of variables that can be treated
as constants. Use these constants to retrieve values from the array returned by
FileInfoToArray, as shown in the following segment of working code from a program
that I created to generate MD5 digests for a list of files.
; +------------------------------------------------------------------------------------------------------------------------------------+
; | Expand file specification into a list, and gather complete file statistics from the file system. |
; +------------------------------------------------------------------------------------------------------------------------------------+
aFileInfo = FileInfoToArray ( FileSpec , FILEINFO_BOTH_P6C )
If ( ArrInfo ( aFileInfo , 1 ) - 1 ) != aFileInfo [ FILEINFO_SUMMARY_ROW_P6C , FILEINFO_NBR_FILES_P6C ]
If !fQuietMode
LogMsg = StrCat ( 'ABORTING!' , @LF , 'WIL function FileInfoToArray returned inconsistent data.' )
Dummy = MessageBox_P6C ( WindowCaption , LogMsg , MSGBOX_ICON_CRITICAL )
RetCode = 112
EndIf
Goto MainEnd
EndIf
If !aFileInfo [ FILEINFO_SUMMARY_ROW_P6C , FILEINFO_NBR_FILES_P6C ]
If !fQuietMode
LogMsg = StrCat ( 'There are no files that match specification "' , FileSpec , '" - no action taken' )
Dummy = MessageBox_P6C ( WindowCaption , LogMsg , MSGBOX_ICON_EXCLAMATION )
RetCode = 113
EndIf
Goto MainEnd
EndIf
; +------------------------------------------------------------------------------------------------------------------------------------+
; | Open log file. |
; +------------------------------------------------------------------------------------------------------------------------------------+
If FileExist ( OutFileName )
hOutFile = FileOpen ( OutFileName , 'append' )
If !hOutFile
LogMsg = StrCat ( 'ABORTING!' , @LF , @LF , 'Error opening output file ' , OutFileName )
Dummy = MessageBox_P6C ( WindowCaption , LogMsg , MSGBOX_ICON_CRITICAL )
RetCode = 114
Goto MainEnd
EndIf
Else
hOutFile = FileOpen ( OutFileName , 'write' )
If !hOutFile
LogMsg = StrCat ( 'ABORTING!' , @LF , @LF , 'Error opening output file ' , OutFileName )
Dummy = MessageBox_P6C ( WindowCaption , LogMsg , MSGBOX_ICON_CRITICAL )
RetCode = 115
Goto MainEnd
EndIf
Dummy = FileWrite ( hOutFile , StrCat ( 'FileName' , @TAB , 'MD5' ) )
EndIf
; +------------------------------------------------------------------------------------------------------------------------------------+
; | Load the MD5 digester. |
; +------------------------------------------------------------------------------------------------------------------------------------+
hMD5Library = DllLoad ( StrCat ( ProgramDir , 'MD5Digest.dll' ) )
; +------------------------------------------------------------------------------------------------------------------------------------+
; | Roll through the list, digesting and logging each file. |
; +------------------------------------------------------------------------------------------------------------------------------------+
For J = 1 To aFileInfo [ FILEINFO_SUMMARY_ROW_P6C , FILEINFO_NBR_FILES_P6C ]
FQFN = aFileInfo [ J , FILEINFO_FILE_NAME_P6C ]
MD5Digest = DllCall ( hMD5Library , lpstr:'MD5File' , lpstr:FQFN )
Dummy = FileWrite ( hOutFile , StrCat ( FileBaseName ( FQFN ) , @TAB , MD5Digest ) )
Next ; J
; +------------------------------------------------------------------------------------------------------------------------------------+
; | Clean up and report results. |
; +------------------------------------------------------------------------------------------------------------------------------------+
hOutFile = FileClose ( hOutFile )
Note: The code shown above uses a custom DLL that I created, based upon
MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm, Copyright (C) 1991-2,
RSA Data Security, Inc. Created 1991. All rights reserved. I chose to use this DLL in place of the
native WIL BinaryCheckSum function because the string returned by the WIL function is
incompatible with industry standards. The cause of the incompatibility is that the WIL function
inserts hyphens, presumably for readability, that cause the returned value to fail a match against
published MD5 digests.
The References section below contains links to a colorized listing of the include file and a Zip file containing the file, ready to import into your code.
FileInfo_CONSTANTS_P6C.WIL.FileInfo_CONSTANTS_P6C.WIL.