FileAppend of Second File doesn't Appear to be Working
Keywords: fileappend
Question:
I am glueing two files together using FileAppend. When I opens the file in DOS, the 2nd file isn't there.I'm using WinBatch version 5, and I'm having difficulty doing the FileWrite(). I do a FileWrite() in the Append mode with a small text value to a file called config.tst. The write takes effect, as indicated by an increase in file size, a change in the file time-date stamp, and as viewed via AskFileText() and Message().
However, when I try to view config.tst from within the DOS editor, the appended text is not visible. Viewing from AskFileText(), the appended text value is preceded by what looks similar to a lowercase i, but without the dot on top. The config.tst file is a regular ASCII file, as far as I know.... Why is this occurring? What can I do to overcome it?
Answer:
First here's some background. In the early days of DOS, before there where such refinements as ""File Size"", files were some multiple of a disk sector size (usually about 256 bytes back then). In order to determine where the end of text was in an ascii file, a CTRL-Z character was inserted at the end of text. A few old editors still place the CTRL-Z at the end of a text file, and some programs filter it out.WinBatch, being of the new generation of programs designed for new operating systems, and quite heedless of the legacy of problems inherited from DOS 1.0, is quite blind to the CTRL-Z character and treats it like any of the other ascii characters. This is quite common with Windows based programs. So there is a CTRL-Z at the end of the file. When WinBatch appends data to the file, it goes behind the CTRL-Z. Your DOS editor sees the CTRL-Z and thinks "AHA, End of Data")
If you simply cannot get rid of the CTRL-Z, you can pre-process the file before opening it for append with the following code....
; Assuming your file is "YOURFILE.TXT" TheFile="YOURFILE.TXT" ; Puts your filename into a variable TheFileSize=FileSize(YourFile) ; Figures out file size TheBuf=BinaryAlloc(TheFileSize) ; Allocates a work buffer BinaryRead(TheBuf,YourFile) ; Sucks file into buffer Offset=BinaryIndex(TheBuf,0,num2char(26),@FWDSCAN) ; Find the Ctrl-Z BinaryEODSet(BinBuf,Offset) ; Sets end of buffer at CTRL-Z BinaryWrite(TheBuf,YourFile) ; Writes a little smaller file back out BinaryFree(TheBuf) ; Frees Buffer Handle=FileOpen(.... "APPEND") etcor.... Try...RunHideWait("command.com","/c copy fil2+fil1 fil2")You need to set the command.com shortcut (or a copy of it that you run instead of command.com (and its a PIF file - look at it in DOS)) to "CLOSE WINDOW ON EXIT" and "RUN AS A WINDOW"
Article ID: W12887Filename: FileAppend of Second File not Working.txt