Hi, I want to delete the first line and the last line of a text file. De length of the first line and the last line are < 250. The lines in between (actual records to be imported with a dataport) are > 250. Any suggestions ? Thanx in Advance. Theo
Ah, one of those questions that gives the feeling of being challenged and makes an old programmers heart jump. Like that. [img]/forum/images/icon_smile_big.gif" border=“0”> Hope you don’t mind getting a solution, instead of a suggestion? Few problems arise when you want to do file manipulations like this. You can’t remove a part out of the beginning of a file; you have to copy to a new file, skipping the part you don’t want. Removing a part at the end of a file is easier, there you can use the TRUNCate command. So, we need to skip the first line. That’s done by reading the first line in textmode into a text variable. Then we switch to reading in binary mode and fetch chunks of 2000 bytes at a time (the maximum you can use with a binary variable). These chunks of data are copied into a new file. When all data is copied, we have to remove the last line. For that, we search backward in the last read binary chunk for the second last CR (ASCII 13). Not the very last, because that is the one at the end of the file. When found, the destination file is truncated at that point. And voila… the destination file is trimmed as you wanted. In code this looks like: Variables:
Name DataType Subtype Length
Sourcefile File
SourceFileName Text 200
DestinationFile File
DestinationFileName Text 200
TextLine Text 250
BinaryChunk Binary 2000
PositionInFile Integer
ReadASCII Text 1
PostionInBinary Integer
The C/AL code:
SourceFileName := 'c:\testfile.txt';
DestinationFileName := 'c:\outputfile.txt';
//Skip first line in Source File (must be < 250 chars)
Sourcefile.OPEN(SourceFileName);
Sourcefile.TEXTMODE := TRUE;
Sourcefile.READ(TextLine);
PositionInFile := Sourcefile.POS;
Sourcefile.CLOSE;
//Reopen Source file for binary read
Sourcefile.OPEN(SourceFileName);
Sourcefile.TEXTMODE := FALSE;
Sourcefile.SEEK(PositionInFile);
//Create destination file to copy content
IF EXISTS(DestinationFileName) THEN
ERASE(DestinationFileName);
DestinationFile.CREATE(DestinationFileName);
DestinationFile.OPEN(DestinationFileName);
DestinationFile.TEXTMODE := FALSE;
//Read rest of file in Binary chunks, write to destination file
WHILE Sourcefile.POS < Sourcefile.LEN DO BEGIN
CLEAR(BinaryChunk);
Sourcefile.READ(BinaryChunk);
DestinationFile.WRITE(BinaryChunk);
IF Sourcefile.POS < Sourcefile.LEN THEN
PositionInFile := Sourcefile.POS;
IF (Sourcefile.LEN - Sourcefile.POS < 250) THEN BEGIN
Sourcefile.SEEK(Sourcefile.LEN - 250);
PositionInFile := Sourcefile.POS;
END;
END;
//Find the second last CR in the binary chunk
PostionInBinary := 2000; //Length of BinaryChunk variable
CLEAR(ReadASCII);
REPEAT
ReadASCII[1] := BinaryChunk[PostionInBinary];
PostionInBinary -=1;
UNTIL (ReadASCII[1] = 13) AND
((PositionInFile + PostionInBinary) < (Sourcefile.LEN -2)) ;
//Truncate destination file to remove last line
DestinationFile.SEEK(PositionInFile - STRLEN(TextLine) + PostionInBinary);
DestinationFile.TRUNC;
DestinationFile.CLOSE;
Sourcefile.CLOSE;
(Added a push-back of the file pointer when the last chunk would be less than 250 characters - then the CR of the second last line could be missed) Hope this works for you. Groeten uit Holland John Edited by - John Tegelaar on 2002 Feb 28 03:32:25