Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 395
Default reading and replacing just the first line of a txt or csv file XL2

We have a report that is generated from "System A" and the column headers are
in mixed case. We are uploading the file into "System B" which requires all
column headers to be in uppercase.

The end result is that I just need to read the first line, change it to
uppercase, and paste it back into the file over the original first line. My
code (so far, and not working) is below, and here are some general questions
to get me on track:

1. If I recall correctly, when reading text files there is some way to
specify whether the file is being opened for reading, writing, or both (I'd
need both). The OpenTextFile help shows reading OR appending, but not both.
Do I need to close the file and re-open it in the other mode?

2. the OpenTextFile paramater description "appending" implies that any new
text will be added to the end of the file, rather than replacing just the
first line. Is there a different approach I should use to overwrite the first
line, instead of append? like writeline(1)...?

3. Is this the right approach in the first place, or should I just read the
whole dang file, change the first line, and then just write it all to a new
file? That just seems wasteful...

Sub ChangeHeaders()

Dim oFSO As New FileSystemObject
Dim oFS

Dim FileName As Variant
Dim Sep As String
FileName = Application.GetOpenFilename(FileFilter:="Text File (*.txt),*.txt")
If FileName = False Then
Exit Sub
End If

Set oFS = oFSO.OpenTextFile(FileName)

sText = oFS.ReadLine
sText = UCase(sText)
oFS.WriteLine (sText) '<-- errors, probably because file was open for reading

End Sub

Many thanks,
Keith

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 114
Default reading and replacing just the first line of a txt or csv fileXL2

Yes, just create a new file. At least that way if anything goes wrong
you can just compare the two versions.

Tim

On Apr 7, 1:49*pm, ker_01 wrote:
We have a report that is generated from "System A" and the column headers are
in mixed case. We are uploading the file into "System B" which requires all
column headers to be in uppercase.

The end result is that I just need to read the first line, change it to
uppercase, and paste it back into the file over the original first line. My
code (so far, and not working) is below, and here are some general questions
to get me on track:

1. If I recall correctly, when reading text files there is some way to
specify whether the file is being opened for reading, writing, or both (I'd
need both). The OpenTextFile help shows reading OR appending, but not both.
Do I need to close the file and re-open it in the other mode?

2. the OpenTextFile paramater description "appending" implies that any new
text will be added to the end of the file, rather than replacing just the
first line. Is there a different approach I should use to overwrite the first
line, instead of append? like writeline(1)...?

3. Is this the right approach in the first place, or should I just read the
whole dang file, change the first line, and then just write it all to a new
file? That just seems wasteful...

Sub ChangeHeaders()

Dim oFSO As New FileSystemObject
Dim oFS

Dim FileName As Variant
Dim Sep As String
FileName = Application.GetOpenFilename(FileFilter:="Text File (*.txt),*.txt")
If FileName = False Then
* * Exit Sub
End If

Set oFS = oFSO.OpenTextFile(FileName)

sText = oFS.ReadLine
sText = UCase(sText)
oFS.WriteLine (sText) '<-- errors, probably because file was open for reading

End Sub

Many thanks,
Keith


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Start reading text file at line 3 Billy B Excel Programming 2 June 27th 07 07:26 AM
Reading a text file line by line stressman Excel Programming 3 October 16th 05 05:29 AM
Reading a text file line by line Foss[_2_] Excel Programming 4 March 16th 05 04:01 PM
Excel 2000 Hanging while reading large file with Line Input Jacques Brun Excel Programming 4 February 21st 04 05:05 PM


All times are GMT +1. The time now is 03:18 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"