PDA

View Full Version : Appending files



Bunta
10-16-2013, 08:52 AM
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

Bunta
10-16-2013, 08:55 AM
Um, apologies, I don't know why it lost all the nice formatting it had in preview...

Kenneth Hobs
10-16-2013, 12:27 PM
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)

Bunta
10-16-2013, 01:45 PM
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.

Tommy
10-16-2013, 02:06 PM
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

snb
10-16-2013, 03:21 PM
The same result, somewhat lesser code.


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

Bunta
10-17-2013, 01:09 AM
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.

snb
10-17-2013, 02:33 AM
You can't be serious.
Did you test the code and compare it to the result you are expecting ?

Bunta
10-17-2013, 03:58 AM
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

snb
10-17-2013, 05:07 AM
Is the real foldername 'G:\XYZ' or does it contain a space in it's name ?
Which Windows version do you use ?

Tommy
10-17-2013, 05:11 AM
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

snb
10-17-2013, 05:44 AM
In that case use:


Sub M_snb()
Shell "cmd /c copy ""G:\XYZ\*.txt"" G:\newfile.txt", 0
End Sub

Tommy
10-17-2013, 07:22 AM
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.

snb
10-17-2013, 07:53 AM
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

Tommy
10-17-2013, 08:01 AM
WOW took me a few to figure out what you were doing but that is pretty slick.

snb
10-17-2013, 08:32 AM
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

snb
10-17-2013, 01:00 PM
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.