Link to home
Start Free TrialLog in
Avatar of gdunn59
gdunn59

asked on

When combining several text files into one need to know how to only have the column headers in the combined file once

I used the code from another posting to combine several text files into one, but need to know how to only have the column headers only combine once at the very top of the text file.

I have attached the coded that I used to create two separate script files in the "Send To" folder.

Thanks,
gdunn59
Create a text file and name it APPEND DIRS.VBS (or any file with a .vbs extension).  Inside, paste the following:

Const ForReading = 1
Output = InputBox("Enter Path, Filename and Extension of Appended Output."&chr(13)&"Press cancel or leave blank to search for directory") 
if output="" then 
 Set sa = CreateObject("Shell.Application")
 Set shFolder = sa.BrowseForFolder(0, "Select Folder to Save Output File", 0, "")
 if shFolder is nothing then wscript.quit
 Set shFolderItem = shFolder.Items.Item
 folderName = shFolderItem.Path
 Output = InputBox("Enter Filename and Extension of Appended Output") 
 if output="" then wscript.quit
 output = foldername & "\" & output
end if
Set c = CreateObject("Scripting.FileSystemObject")
Set d = c.CreateTextFile(output)
For Each strArgument in Wscript.Arguments
 set fld=c.getfolder(strArgument)
 for each fl in fld.files
  Set b = c.OpenTextFile(fl, ForReading)
  readerline=b.readall
  d.WriteLine readerline
 next
Next


Next create another text file and call it APPENDER.VBS (or any .vbs file), paste the following in it:

Const ForReading = 1
Output = InputBox("Enter Path, Filename and Extension of Appended Output."&chr(13)&"Press cancel or leave blank to search for directory") 
if output="" then 
 Set sa = CreateObject("Shell.Application")
 Set shFolder = sa.BrowseForFolder(0, "Select Folder to Save Output File", 0, "")
 if shFolder is nothing then wscript.quit
 Set shFolderItem = shFolder.Items.Item
 folderName = shFolderItem.Path
 Output = InputBox("Enter Filename and Extension of Appended Output") 
 if output="" then wscript.quit
 output = foldername & "\" & output
end if
Set c = CreateObject("Scripting.FileSystemObject")
Set d = c.CreateTextFile(output)
For Each strArgument in Wscript.Arguments
 if right(strArgument,4)=".xls" then 
  msgbox "Will Not Work With Excel Files.  Please Convert to Text First"
  wscript.quit
 end if
 Set b = c.OpenTextFile(strargument, ForReading)
 readerline=b.readall
 d.WriteLine readerline
Next

Open in new window

Avatar of Donald Maloney
Donald Maloney
Flag of United States of America image

For the first file I would read in all the data.
For the second and future files after line 42 set an indicator.
After line 49 test the indicator e'g = true and skip the write line, set the indicator false and continue
Of course this will work only if tghe first line of every other file is a header line.
ELse test for Header information then skip the write on that line
Avatar of gdunn59
gdunn59

ASKER

Not sure how to include what your saying into the code.

Could you please provide this information?

Thanks,
gdunn59
ASKER CERTIFIED SOLUTION
Avatar of Donald Maloney
Donald Maloney
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gdunn59

ASKER

donaldmaloney,

I tried the solution you posted, and I get the following error when executing:

     Line: 1
     Char: 19
     Expected end of statement


Thanks,
gdunn59
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gdunn59

ASKER

I did not use the lines of code that had your subname, I just pasted starting with Dim TestForHeader As Boolean   ' Sets up a test field as true/false
Hi did you just add/replace as in my last post?
Avatar of gdunn59

ASKER

Yes.  Here is what I have (see code).


Dim TestForHeader As Boolean   ' Sets up a test field as true/false
TestForHeader = True    ' Sets it as true
'Const ForReading = 1
output = InputBox("Enter Path, Filename and Extension of Appended Output." & Chr(13) & "Press cancel or leave blank to search for directory")
If output = "" Then
 Set sa = CreateObject("Shell.Application")
 Set shFolder = sa.BrowseForFolder(0, "Select Folder to Save Output File", 0, "")
 If shFolder Is Nothing Then wscript.Quit
 Set shFolderItem = shFolder.Items.Item
 foldername = shFolderItem.Path
 output = InputBox("Enter Filename and Extension of Appended Output")
 end if
 If output = "" Then wscript.Quit
 output = foldername & "\" & output
 end if
End If
Set c = CreateObject("Scripting.FileSystemObject")
Set d = c.CreateTextFile(output)
For Each strargument In wscript.Arguments
 Set fld = c.getfolder(strargument)
 For Each fl In fld.files
  Set b = c.OpenTextFile(fl, ForReading)
  readerline = b.readall
  If Not TestForHeader Then     'Tests TEst Fiels so if TRUE it will skip the first line read 
  d.WriteLine readerline
  Else
  TestForHeader = False  " So if it was true it is set to fals so all following inputs will be written
  End If
 Next
Next

Open in new window

Avatar of RobSampson
Try changing this section:
Set d = c.CreateTextFile(output)
For Each strArgument in Wscript.Arguments
	set fld=c.getfolder(strArgument)
	for each fl in fld.files
		Set b = c.OpenTextFile(fl, ForReading)
		readerline=b.readall
		d.WriteLine readerline
	Next
Next

Open in new window


to this, which determine whether there's already been some text written to the file, and then if so, skips the first line of each subsequent file.
Set d = c.OpenTextFile(output, 8, true)
For Each strArgument in Wscript.Arguments
	set fld=c.getfolder(strArgument)
	for each fl in fld.files
		Set b = c.OpenTextFile(fl, ForReading)
		If blnExists = True Then b.SkipLine
		readerline=b.readall
		d.WriteLine readerline
	Next
Next

Open in new window


Regards,

Rob.
Rob,

I think you need a reset to blnExists  = false someplace else running that routine will skip every line or not depending how it is initialized.
Don
Oh, you're right.  Change this (from your original code):
Set d = c.CreateTextFile(output)
For Each strArgument in Wscript.Arguments
	set fld=c.getfolder(strArgument)
	for each fl in fld.files
		Set b = c.OpenTextFile(fl, ForReading)
		readerline=b.readall
		d.WriteLine readerline
	Next
Next 

Open in new window


to this:
If c.FileExists(output) = True Then
   blnExists = True
Else
   blnExists = False
End If
Set d = c.OpenTextFile(output, 8, true)
For Each strArgument in Wscript.Arguments
	set fld=c.getfolder(strArgument)
	for each fl in fld.files
		Set b = c.OpenTextFile(fl, ForReading)
		If blnExists = True Then b.SkipLine
		readerline=b.readall
		d.WriteLine readerline
	Next
Next 

Open in new window


Regards,

Rob.
gdunn59

OK I see that in My VB code I use
Dim TestForHeader As Boolean  
and the "A" in the line is the 19th character
The Variablet can be set up on the fly so just  comment out that line in my code.
But I am not sure why that would kill the code since all variables should be declared
and Rob does not set    blnExists          as  a declared  True/False
blnExists = True
ROb  any Idea why the declaration would kill the routine?

Don

@donaldmaloney

>>any Idea why the declaration would kill the routine?

This is a VBScript environment.  Although variables can (and should) be declared, all variables are of VARIANT data type.
aikimark

OK Ty
Dhaest:,

I believe my answer was correct.

Don
Avatar of gdunn59

ASKER

I never got it to work.  Thanks anyway.
@gdunn59

We thought you'd walked away from this question.  Welcome back.  We can stop  the automatic question closure process if you need an solution.

What did you try? (please post code)

What error messages did you receive?

What did the output file look like? (expected vs. actual)
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.