Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Hello,
I really need assistance badly here so asking the gurus out there. I have created a text file which has the file header which has a length of say 100. The 100 length for the header is divided as follows for the Header of the text file.
 
NameFile fileCode Datefile Officercode --This is the header
DFirstMessage9102 92129 sjdjshd- 29819281 is 1ui finish ---1st message
FSecondMessage92129 sjdjshd- 29819281 is 1ui finish ---2nd message
 
For the above the, header has a character length of 100. This is divided into the header details as follows in the code extract wehn the file was created.
 
writeline("NameFile".PadRight(20) & "fileCode".PadRight(30) & Date.Now().Tostring.PadRight(30) & ("Officercode".PadRight(20))
 
Now my issue is I want read the above file and grab the date for the file whcih is the Datefile from the header, andl also I want to check if each message starts with a specific letter F in this case. Note that each message has a fixed length as that of the header like 1000 for each message. code extract used to create the message is as follows too.
 
writeline(message1.PadRight(1000))
WriteLine(message2.Padright1000)
 
Please assist how I could get the date from the file header and check if each message has a F infront before the message starts. If F exists then pull out the message, otherwise leave the message. So for this the second message will be pulled out only.
 

Thanks in advance
Posted 22-May-13 19:42pm
Comments
Sergey Alexandrovich Kryukov at 23-May-13 0:50am
   
Don't do it at all. Padding, hard-coded length of strings. Better explain what do you want to achieve, in terms of data and its purpose, not silly formatting of who-knows-what...
—SA
ekipongi at 23-May-13 0:55am
   
Thanks _SA for the reply anyway. I'm just showing how the text file has been created from teh code extracts above. The length for the Header is 100 which is padded for each header details to cover the 100 length given. My issue is How do I read such a file and grab the Datefile from the header.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There are a couple of problems here, which mean that perhaps you want to rethink how you are doing the file header.
The first is that PadRight fixes a minimum length for the string, not a maximum, so if your file name or code exceed 20 or 30 characters respectively, then your whole system goes to pieces, as you can't tell where one field ends and a new one begins.
 
The next is that DateTime.ToString is culture dependant: it will generate a date based on the settings the user has selected for his PC, and so your files may not be at all portable: a file written on one system will produce a different date (or an error) when read on a different PC.
 
If you want to use a file header line for a text file, then make it variable length, and separate the various parts with a character that isn't allowed in file names: '\t' or '?' for example. Then use a fixed data format such as
DateTime.Now.ToString("yyyyMMdd")
And just have it as a line of text:
20130523?myfile.txt?myCode?Ociffer
You can then retrieve the line very easily, and get the various parts with a simple string.Split operation.
  Permalink  
Comments
ekipongi at 23-May-13 1:46am
   
Thanks OrigionalGriff. The above padded sample file was sent to me. The code extracts above I posted is to clarify the question here. What I want to do is grab the date from the header in the above file sent to me. how could I do that? Please assist with any code sample to grab the date which starts at the 31 position and finishes at the 80 position?
OriginalGriff at 23-May-13 2:04am
   
Um.
Have you looked at the string.Substring method?
http://msdn.microsoft.com/en-us/library/system.string.substring(v=vs.71).aspx
ekipongi at 23-May-13 2:21am
   
seen the substring method but not really sure how I'd use that method to grab the date from the header. really need a help in the code to get this thing working. Thanks.
OriginalGriff at 23-May-13 2:32am
   
Read the header as a string - you know how to do that, I assume?
Then just call substring, giving it the index of the first character, and the number of characters you want.
So if your string is
MyFilename20130523Dibble
Then headerstring.Substring(10,8) will give you the eight characters starting at the eleventh (indexes are zero based)
Or "20130523"
ekipongi at 23-May-13 2:45am
   
Thank you so much OrigionalGriff. Got working but I'm stuck on the message bit as highlighted in the initial question posted above. How to check if F exists for each message.
OriginalGriff at 23-May-13 4:04am
   
Um.
Substring again?
Or perhaps string.StartsWith
Or treat the string as an array and check the first character...
 
There are so many ways this could be done and most of them are really, really basic. I get the feeling that either you aren't thinking about this for yourself at all, or you are trying to run before you can walk. You should know all this simple stuff before you start playing with FileStreams! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 476
1 Sergey Alexandrovich Kryukov 200
2 CHill60 195
3 TheRealSteveJudge 120
4 F-ES Sitecore 120
0 Sergey Alexandrovich Kryukov 9,061
1 OriginalGriff 7,920
2 Peter Leow 4,774
3 Kornfeld Eliyahu Peter 3,210
4 Maciej Los 2,176


Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 23 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100