How to pass string to concatenate routine.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: How to pass string to concatenate routine.

  1. #1
    Join Date
    Apr 2007
    Posts
    3

    How to pass string to concatenate routine.

    First, let me say I am not a skilled programmer!

    I am trying to write some VB6 code that creates a string with the files names in a directory and then passes it to a concatenate routine to create one text file containing the contents of each file in the directory.

    I am using the “ListFiles” and “ListFilesPriv” functions to create a variable of the file names and the “ConcatenateFiles” routine to create the new text file with the contents of each file in the directory.

    My problem is I cannot seem to pass the string of file names created with “ListFiles” and “ListFilesPriv” functions to the “ConcatenateFiles” routine! I can manually type the file names into the code and it works fine, but if I try and pass a string containing all the file names to the “ConcatenateFiles” routine, it does not work! It is driving me crazy!

    Here is a copy of the test code I am trying to get working (I know it is messy, I will clean it up after I get it working )

    Can anyone tell me why I can the “ConcatenateFiles” routine to work if I manually enter the file names as follows?
    ConcatenateFiles "K:\DSN-QA\VB Projects\KMM S-C INTERFACE v2.0\Database\data.txt", "", "K:\DSN-QA\VB Projects\KMM S-C INTERFACE v2.0\Database\Log\03-02-2007_16-57-03_KM32G02003AEE_00D0E09448DC.csv", "K:\DSN-QA\VB Projects\KMM S-C INTERFACE v2.0\Database\Log\03-06-2007_10-01-09_KM32G030004FA_00D0E09214C1.csv"
    However, not if I try passing the file names with a string like the following.
    ConcatenateFiles "K:\DSN-QA\VB Projects\KMM S-C INTERFACE v2.0\Database\data.txt", "", temp4
    Thanks in advance for any help or advice you can give me!

    sjh

    --------------------------------------------------------------------------
    Dim temp
    Dim temp1
    Dim temp2
    Dim temp3
    Dim temp4
    Dim temp5$
    Dim temp6
    Dim quote$
    Dim comma$

    ' list all the files in a directory
    ' if NESTEDDIRS = True it lists a whole directory tree
    '
    ' returns a 1-based array containing all the listed files

    Function ListFiles(ByVal Path As String, Optional ByVal NestedDirs As Boolean) _
    As String()
    Dim fso As New Scripting.FileSystemObject
    Dim fld As Scripting.Folder
    Dim fileList As String

    ' get the starting folder
    Set fld = fso.GetFolder(Path)
    ' let the private subroutine do all the work
    fileList = ListFilesPriv(fld, NestedDirs)
    ' convert to a string array
    ' (the first element will be a null string)
    ListFiles = Split(fileList, ",")

    End Function

    ' private procedure that returns a file list
    ' as a comma-delimited list of files

    Function ListFilesPriv(ByVal fld As Scripting.Folder, _
    ByVal NestedDirs As Boolean) As String
    Dim fil As Scripting.File
    Dim subfld As Scripting.Folder

    ' list all the files in this directory
    For Each fil In fld.Files
    ListFilesPriv = ListFilesPriv & "," & fil.Path
    Next

    ' if requested, search also subdirectories
    If NestedDirs Then
    For Each subfld In fld.SubFolders
    ListFilesPriv = ListFilesPriv & ListFilesPriv(subfld, NestedDirs)
    Next
    End If

    End Function

    Private Sub Command1_Click()
    TestGetAllFiles
    temp = Len(temp2)
    temp3 = temp - 4
    temp1 = Left(temp2, temp3)
    temp4 = Trim(temp1)
    temp5$ = "K:\DSN-QA\VB Projects\KMM S-C INTERFACE v2.0\Database\data.txt"
    temp6 = ""
    Debug.Print "K:\DSN-QA\VB Projects\KMM S-C INTERFACE v2.0\Database\data.txt", "", "K:\DSN-QA\VB Projects\KMM S-C INTERFACE v2.0\Database\Log\03-02-2007_16-57-03_KM32G02003AEE_00D0E09448DC.csv", "K:\DSN-QA\VB Projects\KMM S-C INTERFACE v2.0\Database\Log\03-06-2007_10-01-09_KM32G030004FA_00D0E09214C1.csv"
    ConcatenateFiles "K:\DSN-QA\VB Projects\KMM S-C INTERFACE v2.0\Database\data.txt", "", temp4
    End Sub


    Sub TestGetAllFiles()
    Dim varFileArray As Variant
    Dim lngI As Long
    Dim strDirName As String

    Const NO_FILES_IN_DIR As Long = 9
    Const INVALID_DIR As Long = 13

    On Error GoTo Test_Err

    strDirName = "K:\DSN-QA\VB Projects\KMM S-C INTERFACE v2.0\Database\Log"
    varFileArray = ListFiles(strDirName)
    For lngI = 1 To UBound(varFileArray)
    Debug.Print varFileArray(lngI) & ", "
    temp2 = temp2 + varFileArray(lngI) + ", "
    Next lngI

    Test_Err:
    Select Case Err.Number
    Case NO_FILES_IN_DIR
    MsgBox "The directory named '" & strDirName _
    & "' contains no files."
    Case INVALID_DIR
    MsgBox "'" & strDirName & "' is not a valid directory."
    Case 0
    Case Else
    MsgBox "Error #" & Err.Number & " - " & Err.Description
    End Select
    End Sub


    ' Concatenate a variable number of text files into a single result file
    '
    ' Params:
    ' - ResultFile: the complete path of the result file you want to create
    ' - Separator: a string that is written when a file is added to the result
    ' file.
    ' Note: this string can contain the #FilePath# tag that will be replaced
    ' with the path of the file being added
    ' - SourceFiles: a sequence of files whose content will be concatenated
    '
    ' Example:
    ' ConcatenateFiles "D:\res.txt", "------ NEW FILE: #FilePath# ------",
    ' "D:\1.txt", "D:\2.txt", "D:\3.txt"


    Private Sub ConcatenateFiles(ByVal ResultFile As String, _
    ByVal Separator As String, ParamArray SourceFiles() As Variant)

    Dim fso As New FileSystemObject
    Dim fsSourceStream As TextStream
    Dim fsResStream As TextStream
    Dim sSeparator As String
    Dim i As Integer

    On Error Resume Next

    ' create a new file
    Set fsResStream = fso.OpenTextFile(ResultFile, ForWriting, True)

    ' for each source file in the input array
    For i = 0 To UBound(SourceFiles)
    ' add the separator first (replacing the special tag for the file path)
    sSeparator = Replace(Separator, "#FilePath#", SourceFiles(i))
    'sSeparator = Replace(Separator, "", SourceFiles(i))
    'fsResStream.Write sSeparator & vbCrLf
    fsResStream.Write sSeparator
    ' open the file in read mode
    Set fsSourceStream = fso.OpenTextFile(SourceFiles(i), ForReading)
    ' add its content + a blank line to the result file
    'fsResStream.Write fsSourceStream.ReadAll & vbCrLf
    fsResStream.Write fsSourceStream.ReadAll
    ' close this source file
    fsSourceStream.Close
    Next i

    fsResStream.Close
    End Sub

    Private Sub Form_Load()
    quote$ = Chr(34)
    comma$ = Chr(44)
    End Sub

  2. #2
    Join Date
    Aug 2005
    Posts
    85
    Well if you look at the example;

    Code:
    ' Example:
    ' ConcatenateFiles "D:\res.txt", "------ NEW FILE: #FilePath# ------",
    ' "D:\1.txt", "D:\2.txt", "D:\3.txt"
    You can see that the files must be presented either as individual filenames separated by a comma, or you can actually send an array. You have dimensioned Temp4 as a variant but not as an array.

    From a quick look at your code, it seems to me that you could send 'ListFiles' directly to the 'ConcatenateFiles' routine.

    Code:
    ' Example:
    ' ConcatenateFiles "D:\res.txt", "------ NEW FILE: #FilePath# ------",
    ' ListFiles(ByVal Path As String, Optional ByVal NestedDirs As Boolean)
    Obviously I have not tried this, but I cannot see why it wouldn't work.

  3. #3
    Join Date
    Apr 2007
    Posts
    3
    Thanks for the reply!

    I do not understand how to pass the ListFiles to the 'ConcatenateFiles' routine.

    I am using the 'TestGetAllFiles' sub to build the string with all the file names, I then try to use the string created with 'TestGetAllFiles' (temp2) in the 'ConcatenateFiles' routine, but it does not work if there is more than one file in the directory!

    Any further advice you could provide would be greatly appreciated!

    Regards, sjh

  4. #4
    Join Date
    Aug 2005
    Posts
    85
    Hi, one of us is missing what's required. As I understand it, you want to get all the filenames of textfiles in a given directory, then you want to pass them to 'ConcantenateFiles' which will open each one and add it's contents to another file.

    If I am right, then look at the structure of the 'ConcatenateFiles' routine. It expects an array of filenames and that is what the output of 'ListFiles' is, an array of Filenames! So my last example in my previous post should work.

    However to make it clearer I will point the example at your Log directory, which needs to be full of text files only, csv should be ok. Try;

    Code:
    ConcatenateFiles "K:\DSN-QA\VB Projects\KMM S-C INTERFACE v2.0\Database\data.txt", _
    "", ListFiles("K:\DSN-QA\VB Projects\KMM S-C INTERFACE v2.0\Database\Log")

    EDIT:

    I have now actually tried it and you have to change the input param of Concatenatefiles to;

    Code:
    Private Sub ConcatenateFiles(ByVal ResultFile As String, _
        ByVal Separator As String, SourceFiles() As String)
    Steve.
    Last edited by Steve Grant; 04-15-2007 at 04:49 AM.

  5. #5
    Join Date
    Apr 2007
    Posts
    3
    Thank you, thank you, thank you! I thought it was a problem with the input parameters, but didn't know how to change them correctly.

    Again, thank you so much for helping me out!

    Best Regards, sjh

Similar Threads

  1. Create a vCard
    By admol in forum .NET
    Replies: 0
    Last Post: 02-27-2007, 10:30 AM
  2. SQL DMO Restores in VB?
    By ObiWan in forum VB Classic
    Replies: 3
    Last Post: 05-23-2006, 10:35 AM
  3. Replies: 1
    Last Post: 06-05-2001, 06:12 AM
  4. VB/C Array parameters
    By Gastao Woelfert in forum VB Classic
    Replies: 2
    Last Post: 09-01-2000, 11:36 AM
  5. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 0
    Last Post: 08-10-2000, 09:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center