Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to manipulate the text in a file?

Status
Not open for further replies.

Terence

Technical User
Feb 22, 2001
30
US
HI All , i have a file that has all of the text like this:

0216:59:03 am 08/04/01 datadatadatadatadatadatadatadat
0216:59:10 am 08/04/01 datadatadatada6tadatadatadatad

i want to change this to another file to look like this:

6:59:03 am 08/04/01 datadatadatadatadatadatadatadat

6:59:10 am 08/04/01 datadatadatada6tadatadatadatad

I have not been able to figure out how to do this...... help would be appreciated.

:cool:
 
Open the file for input.
Open a second file for output.
Read in a line from the first file.
Parse that line, change it- whatever you want.
Write the line to the new file.

Loop through to the end of the first file, fixing and writing to the new file.

close both
Kill the first file
Rename the 2nd file to the name the first file had.

 
I can figure the opening and closing of the file, just need help on the Parse part of the file to get the results i need.
 
Use split to break them into a array and read each array element to do what you want..
 
The addition of split in VB6 makes parsing much easier. Before split came along one had to rely on INSTR and a good LOOP and REDIM.

Split has that nasty problem of multiple elements for your data section.

[tt]
for n = 4 to ubound(splitArray)
data = data & " " & splitArray(n)
next n
[/tt]

Finding the nth space is easy too... (quik, durtie, and untested...
[tt]
function iSpace(n,SearchString)
dim i as integer
i = 0 ' Is a good place to start
if (0 < n) then
do while 0 < n
i = instr(i+1, SearchString, SPACE)
n = n-1
loop
end if
iSpace = i
End Function
[/tt]
iSpace is 0 if it does not find the nth space, otherwise it is the position of the nth space.. For you
[tt]
data = mid$(YourRecord, iSpace(3, YourRecord)+1)
[/tt]

There is still another boogyman for this method. Multiple delimiters in the data being treated as one. Your input sample deosn't show this, but the output does.

To handle that situation you need to create a process to strip the field from the left and also return the remaining portion. You'll then be able to TRIM the remainder to eliminate the duplicate delimiters. It runs kind of like this:
[tt]
Function Parse(sIn) as String
dim i
i = instr(1, sIn, &quot; &quot;)
if i then ' Have more
Parse = Left(sIn, i-1) ' return token
sIn = TRIM(mid(sIn, i)) ' and remainder of string
else
Parse = sIn
sIn = &quot;&quot;
endif
end function
[/tt]

Another monkey wrench... What happens when you want to parse on tab, space, and comma? Split's coisin, REPLACE is handy here. REPLACE comma and tabs with space and proceed as above.

Finally allow for your delimiters to be escaped (treated as data) or quoted and as such also treated as data?

I'll leave that to another.

Wil Mead
wmead@optonline.net

 
Well it has taken some thought but i have been able to wade through the mess and get most of what i need. I just need to figre out how to put spaces in the formula and i think i will have it. Thanks for all the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top