Results 1 to 11 of 11

Thread: Creating one text file from existing files

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2000
    Location
    Canada
    Posts
    34

    Question

    Hi,

    I really need help with this one! There are at least 2 or more batch files but with unknown names. Now, what I need to do is copy all the texts from each and every one of them and save them in one file.

    Any help or suggestion would be very much appreciated.
    Regards

  2. #2
    Fanatic Member
    Join Date
    Jan 1999
    Location
    UK
    Posts
    554

    You could do this, its a bit messy and probaly could be done in less code...

    How about

    opening each file and writing it to the one then open the next and writing that at the endo fht ethe one etc...

    open outfile for output as #1 (open for sequential input)
    'open first file
    open file1 for input as #2 (open for sequential input)
    do
    if not eof(2) then
    Line Input #2, buffer
    print #1,buffer
    endif
    loop
    close #2

    'open next file
    open file2 for input as #2 (open for sequential input)
    do
    if not eof(2) then
    Line Input #2, buffer
    print #1,buffer
    endif
    loop
    close #2

    etc etc
    ending with ...close #1

    DocZaf
    {;->


  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2000
    Location
    Canada
    Posts
    34
    Hi, thanks for the reply.

    But, I do not have the file name/s. That is my problem. All I know is there are 2 or more files with the same "known" extention but "unknown" file names. The file names will be created by the user but the extentions are the same.

    I really like to know if that is possible at all with VB.

    Thanks again for your time.
    Regards

  4. #4
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    This code will take all the .bat files in the current directory and make one .bat file called "myNewFile.Bat". Now even i am impressed that this works.

    Code:
    Dim fs, f, f1, fc
    Dim myReadFile, myNewFile
    Dim strFileRead As String
    
    Set fs = CreateObject("Scripting.FileSystemObject")
    'the new file made up of all the others
    Set myNewFile = fs.CreateTextFile("myNewFile.Bat", True)
    
    Set f = fs.GetFolder(CurDir)
    'get a list of files
    Set fc = f.Files
    
    For Each f1 In fc
      'if this is a .bat file
      If Right$(f1.Name, 3) = "bat" Then
        'get the file
        Set myReadFile = fs.OpenTextFile(f1.Name, 1)
        'loop to write each line
        While Not myReadFile.AtEndOfStream
          strFileRead = myReadFile.ReadLine
          myNewFile.WriteLine (strFileRead)
        Wend
        'close file
        myReadFile.Close
      End If
    Next
        
    myNewFile.Close
    
    Set f = Nothing
    Set f1 = Nothing
    Set fc = Nothing
    Set fs = Nothing
    Iain, thats with an i by the way!

  5. #5
    Guest
    Its refreshing to see someone use the FSO! Nice one!

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2000
    Location
    Canada
    Posts
    34

    Thumbs up

    Thank You! Thank You!

    I really appreciate all for your time on this question specially "lain17" with his code.

    I just came back from work and I have not had a chance to try it but I am positive that this is it.

    Thank You again.
    Regards

  7. #7
    Hyperactive Member Juan Carlos Rey's Avatar
    Join Date
    Aug 1999
    Location
    Mendoza, Argentina
    Posts
    301

    Simpler way??

    What about old plain DOS command copy?

    --------
    Copy *.bat YourFileName
    --------

    Not so elegant but simple, not?

  8. #8
    Fanatic Member
    Join Date
    Jan 1999
    Location
    UK
    Posts
    554
    While your in dos why not just pipe it


    print *.bat >> MYFile.Bat

    I take it that your not planning on running the batch file steel?

    Zaf Khan

  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2000
    Location
    Canada
    Posts
    34

    Talking

    When I saw the last one, I just sat back for a few minutes! I couldn't say anything! Oh, brother!!!!! I think I need a vacation!

    Well, of course it works!

    Thank you all, one more time.
    Regards

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2000
    Location
    Canada
    Posts
    34

    Smile

    Hi da_silvy,

    Your suggestion would work if I knew the filenames but I only know the file extentions. So, I cannot use "file1 = "c:\file1.bat" and etc., unfortunately. That is the problem. I am going to try Zaf Khan's VB code suggestion as well and will update you guys.

    Thanks for your time.
    Regards

  11. #11
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    sorry, here is a better way
    but you need a Common Dialog COntrol called cdlg1
    Code:
    Dim file1, file2, TargetFile, myString, myFile1, myFile2 As String
    On Error GoTo ending:
    cdlg1.CancelError = True
    cdlg1.DialogTitle = "Open File 1"
    cdlg1.Filter = "Batch Files|*.bat"
    cdlg1.FilterIndex = 1
    cdlg1.ShowOpen
    file1 = cdlg1.FileName
    cdlg1.DialogTitle = "Open File 2"
    cdlg1.ShowOpen
    file2 = cdlg1.FileName
    cdlg1.DialogTitle = "Save As"
    cdlg1.ShowSave
    TargetFile = cdlg1.FileName
    Open file2 For Input As #1 'open 2nd source file
    Do Until EOF(1) 'Repeat until all the file has been loaded
    Input #1, myString 'get the current line
    myFile2 = myFile2 & vbCrLf & myString 'add it to the "file" string
    Loop
    Close #1
    Open file1 For Input As #1 'see above etc. etc.
    Do Until EOF(1)
    Input #1, myString
    myFile1 = myFile1 & vbCrLf & myString
    Loop
    Close #1
    Open TargetFile For Output As #1
    Print #1, myFile1 & vbCrLf
    Print #1, myFile2
    Close #1
    Exit Sub
    ending:
    MsgBox "Unfortunately there was an error!", vbCritical, "Error"

    does this one work better? it lets the user choose the source 1, source 2 and the target


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width