Consulting

Results 1 to 17 of 17

Thread: Appending files

  1. #1
    VBAX Newbie
    Joined
    Oct 2013
    Posts
    5
    Location

    Appending files

    Hi all,I'm struggling to trim a line of output when appended it to another file. The code combines the files and does not output the first line of the line files to be appended but for the second line I only wish for the first seven characters to be included followed by the last character or a ">".Is there a way to do this? I'm not an expert but if someone can nudge me in the right direction I'm happy to experiment and reserch it.Thanks very much
    Sub combine()
    ' Find all the text files in folder to be processed
    myfolder = "ABC"
    myfile = Dir(myfolder & "*.txt")
    If Len(myfile) = 0 Then Exit Sub   ' no txt files found
    Open myfolder & "newfile.txt" For Append As 2
    Do While Len(myfile) > 0    
    Open myfolder & myfile For Input As 1        
    filestr = Input(LOF(1), #1)        
    Close 1        
    'remove first line        
    filestr = Mid(filestr, InStr(filestr, vbNewLine) + 2)        
    Print #2, filestr        
    myfile = Dir
    Loop
    Close 2
    End Sub
    Last edited by Bunta; 10-16-2013 at 01:42 PM. Reason: Couldn't post to begin with so didn't put the full msg!

  2. #2
    VBAX Newbie
    Joined
    Oct 2013
    Posts
    5
    Location
    Um, apologies, I don't know why it lost all the nice formatting it had in preview...

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Code tags are standard vbbulletin forum software features.

    Type code tags and paste your code between them or in the enhanced editor, click the # key to insert code tags. In this example, replace () with [].

    (code)MsgBox "Hi"(/code)

  4. #4
    VBAX Newbie
    Joined
    Oct 2013
    Posts
    5
    Location
    Thanks Kenneth. For some I could find advanced when posting. I had some trouble initially posting as well as it wouldn't let due to either a URL or a forbidden word. Not sure which but got it sorted. Tidied it up now.

  5. #5
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Hi Bunta,

    have you tried:
    Sub combine()     ' Find all the text files in folder to be processed
        myfolder = "ABC"
        myfile = Dir(myfolder & "*.txt")
        If Len(myfile) = 0 Then Exit Sub ' no txt files found
        Open myfolder & "newfile.txt" For Append As 2
        Do While Len(myfile) > 0
            Open myfolder & myfile For Input As 1
            filestr = Input(LOF(1), #1)
            Close 1
             'remove first line
            filestr = Mid(filestr, InStr(filestr, vbNewLine) + 2)
            Print #2, Left(filestr, 7) & ">"
            myfile = Dir
        Loop
        Close 2
    End Sub
    Last edited by Tommy; 10-16-2013 at 02:09 PM. Reason: Formatting issue

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    The same result, somewhat lesser code.

    sub M_snb()
       shell "cmd /c Dir G:\ABC\*.txt /b >G:\ABC\newfile.txt",0
    End Sub

  7. #7
    VBAX Newbie
    Joined
    Oct 2013
    Posts
    5
    Location
    Thanks Tommy that does exactly what I described. However, being an idiot, I forgot to mention that after it had done what I wanted it to, I still want to append everything else in the file after line 2 as well. I just have an issue with line 2 and what it contains, that I need to modify it. Really sorry I forgot to mention that. snb, thanks for your code but, I have to be honest, I don't fully understand the commands being run. I'm sure it works fine but as I don't have time to go reaserach it and also get the full append working, I'm going to stick with my current method as I can understand it. Thanks for the help though.

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    You can't be serious.
    Did you test the code and compare it to the result you are expecting ?

  9. #9
    VBAX Newbie
    Joined
    Oct 2013
    Posts
    5
    Location
    Yes I did, as you came back about it. I used the code below, only substituted XYZ for my directory (my drive is also G:\ so I left that in). It doesn't append the files. I have four .txt files in the directory and I expected to see a file called "newfile.txt" with all the data in. To get back on topic, is there anyway to ouput the rest of the file after line 2? I have tried adding filestr after Print #2, Left(filestr, 7) & ">" but it prints all of line two again as well.
     Sub Combine()    Shell "cmd /c Dir G:\XYZ\*.txt /b >G:\XYZ\newfile.txt", 0    End Sub
    Last edited by Bunta; 10-17-2013 at 04:35 AM.

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Is the real foldername 'G:\XYZ' or does it contain a space in it's name ?
    Which Windows version do you use ?

  11. #11
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    I don't have any files to test with but this should work.

    snb all I got with your code was a list of txt files in that directory. Not all txt files appended into one.

    Sub combine() ' Find all the text files in folder to be processed    myfolder = "ABC"
        myfile = Dir(myfolder & "*.txt")
        If Len(myfile) = 0 Then Exit Sub ' no txt files found
        Open myfolder & "newfile.txt" For Append As 2
        Do While Len(myfile) > 0
            Open myfolder & myfile For Input As 1
            filestr = Input(LOF(1), #1)
            Close 1
             'remove first line
            filestr = Mid(filestr, InStr(filestr, vbNewLine) + 2)
            Print #2, Left(AllTheFile(1), 7) & ">"
            'get the rest of the file
            filestr = Mid(filestr, InStr(filestr, vbNewLine) + 2)
            Print #2, filestr
            myfile = Dir
        Loop
        Close 2
    End Sub

  12. #12
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    In that case use:

    Sub M_snb()
        Shell "cmd /c copy ""G:\XYZ\*.txt"" G:\newfile.txt", 0
    End Sub
    Last edited by snb; 10-17-2013 at 06:43 AM.

  13. #13
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    snb,
    That copies all the files into one. But it does not remove the first line of each file, take the first 7 characters of the second line and append a ">" character to it and then write the rest of the file.
    LOL I was testing it just to see how you did it. I would have figured you needed a >> but I was incorrect.

  14. #14
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    sub M_snb()
      sn=split(createobject("wscript.shell").exec("cmd /c Dir ""G:\XYZ\*.txt"" /b").stdout.readall,vbcrlf)
    
      with createobject("scripting.filesystemobject")
         for each it in sn
           c01= .opentexfile("G:\XYZ\" & it).readall
           c00 = c00 & mid(c01,len(split(c01,vbcrlf)(0))+2,7) & ">" & mid(c01,len(split(c01,vbcrlf)(0))+10)
         next
         .createtextfile("G:\XYZ\newfile.txt").write mid(c00,2)
      end with
    End Sub
    Last edited by snb; 10-17-2013 at 08:32 AM.

  15. #15
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    WOW took me a few to figure out what you were doing but that is pretty slick.

  16. #16
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    If you like freefiles:

    Sub M_snb()
        sn = Split(CreateObject("wscript.shell").exec("cmd /c Dir ""G:\OF\*.txt"" /b").stdout.readall, vbCrLf)
         
        Open "G:\OF\newfile.txt" For Append As #1
            For Each it In sn
                Open "G:\OF\" & it For Input As #2
                    c01 = Input(LOF(2), 2)
                Close #2
    
                Print #1, Mid(c01, Len(Split(c01, vbCrLf)(0))+2, 7) & ">" & Mid(c01, Len(Split(c01, vbCrLf)(0)) + 10)
            Next
        Close #1
    End Sub

  17. #17
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    I guess the topicstarter (TS) wants to integrate several txt files with the same structure.
    I also guess the first line contains the fieldnames, that won't have to be replicated.
    I also assume the fieldnamerow contains some unique textstring that can be used to filter that row out.

    In that case we can easily use:
    Sub M_snb() 
        Shell "cmd /c copy ""G:\XYZ\*.txt"" G:\newfile.txt", 0 
    End Sub
    we can remove the fieldnamesrow afterwards using (if it contains the word 'date')
    sub M_remove()
      with createobject("scripting.filesystemobject")
         .createtextfile(G:\newfile_001.txt")write join(filter(split(.opentextfile(G:\newfile.txt").readall,vbcrlf),"date",false),vbcrlf)
      end with
    End Sub
    Since the TS was rather scarce in providing information I have no idea what should be the purpose of the ">" after the seventh character of each file.

Posting Permissions

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