Bottom of This Page |
Print Dir or Directory Printer / Udskriv bibliotek eller Biblioteksudskriver
Description: Print a directory (folder)'s file names to a text file. / Beskrivelse: Udskriv et biblioteks (mappes) filnavne til en tekstfil.
Overview - Function(s)... / Oversigt - Funktion(er)...
Developed and tested under Microsoft Office Word 2003 - English version and for some functions only or also under Microsoft Office Excel 2003 - English version. / Udviklet og testet under Microsoft Office Word 2003 - engelsk version og for nogle funktioner kun eller også under Microsoft Office Excel 2003 - engelsk version.
' All variables must be declared before use.
Option Explicit
' Option Base 0|1: Used at module level to declare
' the default lower bound for array subscripts.
' Used at the beginning of a module to specify the
' default lower bound for arrays dimensioned
' within the module. Default base for arrays is 0.
Option Base 0
Public Function doPrintDir(sPathAndFileNameIn As String, sPathAndFileNameOut As String, bGenerateOutputFileModeAppend As Boolean) As Boolean
On Error GoTo Err_doPrintDir
' Print Dir or Directory Printer.
' Print a directory (folder)'s file names to a text file.
'
' Note: All parameters (variables) passed to this function must
' be valid - no error checking is performed.
'
' sPath: full Path and File Name (i. e. wild card(s)) for the Input files.
' sPath2: full Path and File Name for the Output file.
' Note: A filename can not contain the following characters: \/:*?"<>|
' bGenerateOutputFileModeAppend = True|False ' True [Mode: Append] or False [Mode: Overwrite].
' Define variables.
Dim tmpStr As String ' Temporary text.
Dim tmpFileNameStr As String
Dim intOutFileNumber As Integer
Dim IntValue As Long ' Iterator for loops.
Dim bDoSortListItems As Boolean
Dim sUseSortOrder As String
Dim bInSortMixLCaseAndUCaseLetters As Boolean
Dim StartLbound As Long
Dim EndUbound As Long
' Define and Initialize variables for a Dynamic Array - Start.
Dim IntDynamicArrayValue As Long
Dim sFound As String
Dim CurUbound As Long
Dim StartUbound As Long
StartUbound = 50
'Dim TextLineDynamicArrayList(StartUbound) As String
Dim TextLineDynamicArrayList As Variant
' Option Base is used at module level to declare
' the default lower bound for array subscripts.
ReDim TextLineDynamicArrayList(StartUbound)
CurUbound = StartUbound
IntDynamicArrayValue = -1
sFound = "" ' Found text.
' Define and Initialize variables for a Dynamic Array - End.
If IsNull(sPathAndFileNameIn) = True Then
doPrintDir = False
Exit Function
End If
If IsNull(sPathAndFileNameOut) = True Then
doPrintDir = False
Exit Function
End If
' Initialize variables.
tmpStr = "" ' Temporary text.
tmpFileNameStr = ""
' Note: 'True' [Mode: Do sort the list items] or 'False' [Mode: Do not sort the list items].
' Bemaerk: 'True' (Sand) [Maade: Sorter list elementerne] eller 'False' (Falsk) [Maade: Sorter ikke list elementerne].
bDoSortListItems = True ' See remark above. / Se bemaerkning ovenfor.
' Note: Sort order selected by specifying the sort criteria 'Sort Ascending [A-Z]' (Ascending) or
' 'Sort Descending [Z-A]' (Descending) [Default: Ascending].
' Bemaerk: Sorteringsraekkefoelge valgt ved at bestemme sorteringskriteriet 'Sorter stigende [A-Z]' (Stigende) eller
' 'Sorter faldende [Z-A]' (Faldende) [Standard: Stigende].
sUseSortOrder = "Ascending" ' See remark above. / Se bemaerkning ovenfor.
' Note: 'True' [Mode: In sort do mix lowercase and uppercase letters] or 'False' [Mode: In sort do not mix lowercase and uppercase letters].
' Bemaerk: 'True' (Sand) [Maade: I sortering bland smaa og store bogstaver] eller 'False' (Falsk) [Maade: I sortering bland ikke smaa og store bogstaver].
bInSortMixLCaseAndUCaseLetters = True ' See remark above. / Se bemaerkning ovenfor.
doPrintDir = False
If FileExists(sPathAndFileNameIn) = False Then
MsgBox prompt:="Entered path do not exists or there is no file in the directory (folder)!", _
Title:="Errors in the input !", buttons:=vbCritical
Exit Function
End If
' ***** Dynamic Array Start.
' ***** Insert Data in Dynamic Array Start.
' Dir Function returns filename with specified extension. If more
' than one match file exists, the first file found is returned.
sFound = Dir$(sPathAndFileNameIn, 0) ' Retrieve the first entry.
Do While Len(sFound) <> 0 ' Start the loop.
' Add Data to the Dynamic Array.
' ***** Repeated output line(s) or paragraph(s) start *****
' *
' Make an Array line out of each file name.
IntDynamicArrayValue = IntDynamicArrayValue + 1 ' Update the counter variable.
tmpStr = sFound
TextLineDynamicArrayList(IntDynamicArrayValue) = tmpStr
If IntDynamicArrayValue = CurUbound Then
CurUbound = CurUbound + StartUbound
'ReDim Preserve TextLineDynamicArrayList(CurUbound) As String
ReDim Preserve TextLineDynamicArrayList(CurUbound)
End If
' *
' ***** Repeated output line(s) or paragraph(s) end *****
' Call Dir Function again without arguments to return the
' next match file in the same directory.
sFound = Dir$ ' Get next entry.
Loop
' ***** Insert Data in Dynamic Array End.
If IntDynamicArrayValue <> -1 Then
'ReDim Preserve TextLineDynamicArrayList(IntDynamicArrayValue) As String
ReDim Preserve TextLineDynamicArrayList(IntDynamicArrayValue)
Else
'ReDim TextLineDynamicArrayList() As String
ReDim TextLineDynamicArrayList(0)
End If
If IntDynamicArrayValue = -1 Then
' Found no selected file(s).
' Note: TextLineDynamicArrayList: Must contain at least one found file.
MsgBox prompt:="In " & Chr$(34) & sPathAndFileNameIn & Chr$(34) & " no matching file(s) were found" & ".", _
Title:="Info", buttons:=vbInformation + vbOKOnly
GoTo Exit_doPrintDir
End If
' Note: 'True' [Mode: In sort do mix lowercase and uppercase letters] or 'False' [Mode: In sort do not mix lowercase and uppercase letters].
' Bemaerk: 'True' (Sand) [Maade: I sortering bland smaa og store bogstaver] eller 'False' (Falsk) [Maade: I sortering bland ikke smaa og store bogstaver].
If bInSortMixLCaseAndUCaseLetters = False Then ' See remark above. / Se bemaerkning ovenfor.
' Use Standard Sort Algorithm.
'TextLineDynamicArrayList() = doBubbleSortMyList(TextLineDynamicArrayList(), bDoSortListItems, sUseSortOrder, True) ' Bubble Sort My List.
Call doBubbleSortMyList(TextLineDynamicArrayList, bDoSortListItems, sUseSortOrder, True) ' Bubble Sort My List.
Else
' First:
' Use Standard Sort Algorithm.
'TextLineDynamicArrayList() = doBubbleSortMyList(TextLineDynamicArrayList(), bDoSortListItems, sUseSortOrder, True) ' Bubble Sort My List.
Call doBubbleSortMyList(TextLineDynamicArrayList, bDoSortListItems, sUseSortOrder, True) ' Bubble Sort My List.
' Second:
' Use Customized Sort Algorithm.
'TextLineDynamicArrayList() = doBubbleSortMyList(TextLineDynamicArrayList(), bDoSortListItems, sUseSortOrder, False) ' Bubble Sort My List.
Call doBubbleSortMyList(TextLineDynamicArrayList, bDoSortListItems, sUseSortOrder, False) ' Bubble Sort My List.
End If
' ***** Dynamic Array End.
' Note: TextLineDynamicArrayList: Must contain at least one found file.
' Sorted List.
' Initialize variables.
StartLbound = LBound(TextLineDynamicArrayList)
EndUbound = UBound(TextLineDynamicArrayList)
intOutFileNumber = FreeFile ' Unique file number is found. ' Unikt filnummer findes.
' This function can only be used just before an Open sentence. FreeFile
' returns the next available file number, but do not reserve it.
' Denne funktion kan kun bruges lige foran en Open-saetning. FreeFile
' returnerer det naeste tilgaengelige filnummer, men reserverer det ikke.
If bGenerateOutputFileModeAppend = True Then
' True [Mode: Append].
Open sPathAndFileNameOut For Append As #intOutFileNumber
Else
' False [Mode: Overwrite].
Open sPathAndFileNameOut For Output As #intOutFileNumber
End If
Print #intOutFileNumber, ' Print blank line to file.
Print #intOutFileNumber, CStr(Now)
Print #intOutFileNumber, "Input file: " & Chr$(34) & sPathAndFileNameIn & Chr$(34) & "."
Print #intOutFileNumber, "Output file: " & Chr$(34) & sPathAndFileNameOut & Chr$(34) & "."
If (EndUbound + 1) = 1 Then ' Correct for zero-based index.
tmpStr = "Found " & CStr(EndUbound + 1) & " file." ' Correct for zero-based index.
Else
tmpStr = "Found " & CStr(EndUbound + 1) & " files." ' Correct for zero-based index.
End If
Print #intOutFileNumber, tmpStr
tmpStr = "::" & " " & String$(16, "*") & " " & "Running " & _
"Print Dir" & " " & String$(16, "*") ' Fill with "*".
Print #intOutFileNumber, tmpStr
Print #intOutFileNumber, ' Print blank line to file.
' ***** Repeated output line(s) or paragraph(s) start *****
' *
For IntValue = StartLbound To EndUbound Step 1
' Write the sorted Array line(s) back to line(s) in the text file.
tmpFileNameStr = CStr(TextLineDynamicArrayList(IntValue))
Print #intOutFileNumber, tmpFileNameStr
Next IntValue
' *
' ***** Repeated output line(s) or paragraph(s) end *****
'Print #intOutFileNumber, ' Print blank line to file.
Close #intOutFileNumber
doPrintDir = True
Exit_doPrintDir:
Exit Function
Err_doPrintDir:
MsgBox Err.Number & ": " & Err.Description
Resume Exit_doPrintDir
End Function
Public Function FileExists(sPath As String) As Boolean
If Dir(sPath) > "" Then
FileExists = True
Else
FileExists = False
End If
End Function
'Public Function doBubbleSortMyList(ByVal SortList(), bDoSortListItems As Boolean, sUseSortOrder As String, bUseStandardSortAlgorithm As Boolean)
Public Function doBubbleSortMyList(ByRef SortList As Variant, bDoSortListItems As Boolean, sUseSortOrder As String, bUseStandardSortAlgorithm As Boolean)
On Error GoTo Err_doBubbleSortMyList
' Bubble Sort My List.
' This function assumes it's passed an Array or a Dynamic Array,
' and bubble sorts the list elements in the passed array, which
' is returned as an 'Array'.
' Bubble Sort.
' The array-oriented sort routine bubble sort greatest virtue
' is its simplicity. It is very fast on nearly sorted items, but
' its general performance is relatively poor. The bubble sort
' algorithm is very simple: successive sweeps are made through
' the items to be sorted, and on each sweep the largest item is
' moved closer to the top, rising slowly like a bubble - hence
' the name. Because each sweep places one item into its final
' and correct position, the next sweep need not re-examine this
' item. For the sorting of the items is used an array of 'n'
' items. An analysis of the order of this algorithm is
' straightforward: it makes 'n' passes through the items, and
' on each pass it performs 'n'-1 comparisons and possibly this
' number of swaps. Thus, the algorithm's running time will be
' proportional to 'n'('n'-1), or more simply just 'n'2. The code
' takes advantage of the fact that if no swaps are made on any
' given pass, the items are sorted and the sort can terminate.
'
' Note that if an array's items (elements) are in text string
' variables and all consist of integer numbers or floating-point
' numbers you can with advantage use the built-in function
' 'CInt()', CLng()' or 'CDbl()' to parse the string arguments
' to the appropriate number type variables from the array before
' a sort comparison is performed - otherwise the sort order of
' the numbers will not be correct.
'
' Note: All parameters (variables) passed to this function must
' be valid - no error checking is performed.
'
' SortList(): Array to sort - Note: Variable passed must be a
' valid array object whose property ('length') contains the
' number of items (elements) in the 'dense' array, which is
' one in which each item (element) has an integer value. Base
' must be an integer with a value of '0' (default; zero-based index)
' or '1' (index base 1).
' startingIndexBaseInteger: The 'dense' array's starting index - Integer
' with a value of '0' (default; zero-based index) or '1' (index base 1).
' sortDirectionString: The direction of the sort - String
' ('Ascending' [A-Z, zero to 9] (default) or 'Descending' [Z-A, 9 to zero]).
'
' Define variables.
Dim IntValue As Long
Dim IntIntValue As Long
Dim startingIndexBaseInteger As Integer
Dim sortDirectionString As String
Dim SortOrder As String
Dim StartLbound As Long
Dim EndUbound As Long
Dim swapped As Boolean
Dim tmpStr As String ' Temporary text.
'If IsNull(SortList()) = True Then
If IsNull(SortList) = True Then
'doBubbleSortMyList = SortList()
Exit Function
End If
If bDoSortListItems = False Then
'doBubbleSortMyList = SortList()
Exit Function
End If
' Initialize variables.
startingIndexBaseInteger = 0 ' default; zero-based index. Note: This function uses the default value.
'StartLbound = LBound(SortList())
StartLbound = LBound(SortList)
'EndUbound = UBound(SortList())
EndUbound = UBound(SortList)
swapped = False ' Stores state for if a swap is made - Boolean value.
tmpStr = "" ' Temporary text.
If EndUbound < 1 Then
' Make sure there is at least two items (elements) in the array - otherwise
' return the passed variable (array object) unprocessed - no sorting necessary.
'doBubbleSortMyList = SortList()
Exit Function
End If
' Make sure of the variable value if 'nothing' is the contents of the input or
' it is not an integer with a value of '0' (default; zero-based index) or
' '1' (index base 1) - otherwise use the default value.
If IsNumeric(startingIndexBaseInteger) = True Then
startingIndexBaseInteger = CInt(startingIndexBaseInteger) ' Force string to a number.
Else
startingIndexBaseInteger = 0
End If
If startingIndexBaseInteger <> 0 And startingIndexBaseInteger <> 1 Then
startingIndexBaseInteger = 0 ' Default value (zero-based index).
End If
If IsNull(sUseSortOrder) = True Then
sortDirectionString = ""
Else
sortDirectionString = sUseSortOrder
End If
' Sort order selected by the value of 'sortDirectionString' specifying the sort criteria.
' [Danish] Sorteringsraekkefoelge valgt ved vaerdien af 'sortDirectionString' bestemmende sorteringskriteriet.
If LCase(sortDirectionString) = LCase("Ascending") Or LCase(sortDirectionString) = LCase("Stigende") Then
' Sort criteria: 'Sort Ascending [A-Z, zero to 9]' (Ascending).
' [Danish] Sorteringskriterie: Sorter stigende [A-Z, nul til 9] (Stigende).
SortOrder = "Ascending"
ElseIf LCase(sortDirectionString) = LCase("Descending") Or LCase(sortDirectionString) = LCase("Faldende") Then
' Sort criteria: 'Sort Descending [Z-A, 9 to zero]' (Descending).
' [Danish] Sorteringskriterie: Sorter faldende [Z-A, 9 til nul] (Faldende).
SortOrder = "Descending"
Else
' In all other cases chose the default setting [Default: Ascending].
' [Danish] I alle andre tilfaelde vaelg standard indstilling [Standard: Stigende].
SortOrder = "Ascending"
End If
' Sort array items (elements) - only if there is more than one item (element) in the array.
' The 'dense' array's property ('UBound()') contains the number of items (elements) in
' the array, which must be starting with index 0 (zero-based index) or 1 (index base 1).
' Perform the actual sort of the items (elements) in the array based on the value
' of the variable 'SortOrder' (string).
If SortOrder = "Ascending" Then
' Sort Ascending [A-Z, zero to 9] (default).
' Bubble sort an array - Ascending.
If bUseStandardSortAlgorithm = True Then
' Use Standard Sort Algorithm.
swapped = False ' Stores state for if a swap is made - Boolean value.
' Make steadily shorter passes...
For IntValue = EndUbound To StartLbound Step -1 ' Start the loop.
' On each pass, sweep *largest* element to end of array.
swapped = False ' Not done sort swap.
For IntIntValue = StartLbound To (EndUbound - 1) Step 1
If SortList(IntIntValue) > SortList(IntIntValue + 1) Then
tmpStr = SortList(IntIntValue)
SortList(IntIntValue) = SortList(IntIntValue + 1)
SortList(IntIntValue + 1) = tmpStr
swapped = True ' Done sort swap.
End If
Next IntIntValue
If swapped = False Then
Exit For ' If no swaps, we have finished.
End If
Next IntValue
Else
' Use Customized Sort Algorithm.
swapped = False ' Stores state for if a swap is made - Boolean value.
' Make steadily shorter passes...
For IntValue = EndUbound To StartLbound Step -1 ' Start the loop.
' On each pass, sweep *largest* element to end of array.
swapped = False ' Not done sort swap.
For IntIntValue = StartLbound To (EndUbound - 1) Step 1
If LCase(SortList(IntIntValue)) > LCase(SortList(IntIntValue + 1)) Then
tmpStr = SortList(IntIntValue)
SortList(IntIntValue) = SortList(IntIntValue + 1)
SortList(IntIntValue + 1) = tmpStr
swapped = True ' Done sort swap.
End If
Next IntIntValue
If swapped = False Then
Exit For ' If no swaps, we have finished.
End If
Next IntValue
End If
ElseIf SortOrder = "Descending" Then
' Sort Descending [Z-A, 9 to zero].
' Bubble sort an array - Descending.
If bUseStandardSortAlgorithm = True Then
' Use Standard Sort Algorithm.
swapped = False ' Stores state for if a swap is made - Boolean value.
' Make steadily shorter passes...
For IntValue = EndUbound To StartLbound Step -1 ' Start the loop.
' On each pass, sweep *largest* element to end of array.
swapped = False ' Not done sort swap.
For IntIntValue = StartLbound To (EndUbound - 1) Step 1
If SortList(IntIntValue) < SortList(IntIntValue + 1) Then
tmpStr = SortList(IntIntValue)
SortList(IntIntValue) = SortList(IntIntValue + 1)
SortList(IntIntValue + 1) = tmpStr
swapped = True ' Done sort swap.
End If
Next IntIntValue
If swapped = False Then
Exit For ' If no swaps, we have finished.
End If
Next IntValue
Else
' Use Customized Sort Algorithm.
swapped = False ' Stores state for if a swap is made - Boolean value.
' Make steadily shorter passes...
For IntValue = EndUbound To StartLbound Step -1 ' Start the loop.
' On each pass, sweep *largest* element to end of array.
swapped = False ' Not done sort swap.
For IntIntValue = StartLbound To (EndUbound - 1) Step 1
If LCase(SortList(IntIntValue)) < LCase(SortList(IntIntValue + 1)) Then
tmpStr = SortList(IntIntValue)
SortList(IntIntValue) = SortList(IntIntValue + 1)
SortList(IntIntValue + 1) = tmpStr
swapped = True ' Done sort swap.
End If
Next IntIntValue
If swapped = False Then
Exit For ' If no swaps, we have finished.
End If
Next IntValue
End If
Else
' Do nothing.
'MsgBox("Do nothing.", (0 + 64), "Info") ' Only for the purpose of debugging.
'MsgBox prompt:="Do nothing.", _
Title:="Info", buttons:=vbInformation + vbOKOnly ' Only for the purpose of debugging.
End If
'doBubbleSortMyList = SortList()
Exit_doBubbleSortMyList:
Exit Function
Err_doBubbleSortMyList:
'MsgBox "Error " & Err & ": " & Error$ & Chr$(13) & _
'"In line: " & Erl & "." & Chr$(13) , (0 + 16), "Error / Fejl"
MsgBox Err.Number & ": " & Err.Description
Resume Exit_doBubbleSortMyList
End Function
Conventions used for: Source code syntax highlighting. / Regler brugt til: Kildekode syntaks fremhævning.
Word: / Word:
Excel: / Excel:
MsgBox prompt:="Print Dir succeeded: " & doPrintDir("C:\Temp\*", "C:\wizard\PrintDir.txt", True) & ".", _
Title:="Info", buttons:=vbInformation + vbOKOnly
Conventions used for: Source code syntax highlighting. / Regler brugt til: Kildekode syntaks fremhævning.
Word: / Word:
Excel: / Excel:
The code might need some minor tweaks to run in your application. / Koden kan behøve nogle mindre ændringer for at kunne afvikles i dit anvendelsesområde.
Licence: Free to use, but please
share improvements.
No warranty - use at own risk. /
Licens: Fri brug, men
del venligst forbedringer.
Ingen garanti - brug på eget ansvar.
Warning: Don't run the script files
without reading them first!
Total absence of any guarantee, warranty, or
responsibility for script files, the script(s), the files they
may produce, or the effects the script(s) or script-produced
files may have. The script(s) is, after all, plain text. The
burden is on the person using the script(s) to examine
the script source code and determine whether or not a script is
usable and safe. Operating systems and browsers are constantly
changing. What works today may not work tomorrow!
Advarsel: Kør ikke script-filerne
uden at læse dem først!
Totalt fravær af nogen form for garanti,
garanti eller ansvar for script-filer, scriptet(scriptene), de
filer, de kan producere eller de virkninger, scriptet(scriptene)
eller scriptproducerede filer kan have. Scriptet(Scriptene) er,
trods alt, almindelig tekst. Byrden er på brugeren af
scriptet(scriptene) til at undersøge script kildekoden og
afgøre, hvorvidt et script er brugbart og sikkert.
Operativsystemer og browsere er under konstant forandring. Hvad
fungerer i dag, fungerer muligvis ikke i morgen!
|