WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

File Operations

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Delete First Few Lines of a File


Question:

I need to take a file (Normal text file)and remove the first 6 lines. I then need to save the file.I know what tools to use, but, can't figure out how to do this.

Answer:

If running WinBatch 2003G or newer:
; I will assume that there are more than 6 lines in the file.
; If that is not always the case, this will need some changes.

filename = "C:\Program Files\WinBatch\TestFiles\mpenkower.txt"   ; change to your filename
file = FileGet(filename)                          ; Place file into a variable
file = StrReplace(file,@CRLF,@TAB)                ; Change delimiter

For ii = 1 To 6
  file = ItemRemove(1,file,@TAB)
Next

file = StrReplace(file,@TAB,@CRLF)                ; Change delimiter back to CRLF
ret = FilePut(filename,file)                      ; Write file back out


If running 2003F or older use this code:


#DefineFunction RemoveFirstLines(file,revfile,count)
;Find file size
sz = FileSize(file)
if sz==0 then Return @false
;Allocate binary buffer to that size
bb = BinaryAlloc(sz)
;Read file into buffer
BinaryRead(bb,file)
;Search buffer for LF (line feed). Repeat 5 more times.
ptr = 0
For x = 1 to 6
	ptr = BinaryIndexEx(bb, ptr, @lf, @fwdscan, @false)
	if ptr == -1 then Return @false
	ptr = ptr+1
Next
;Write buffer to new file starting where the search found the 7th line start.
datasize = BinaryEodGet(bb)-ptr-1
BinaryWriteEx(bb, ptr, revfile, 0, datasize)
Return 1
#EndFunction 

origfile = "C:\temp\test.txt"
revisedfile = "C:\temp\testout.txt"
linecount = 6

ret = RemoveFirstLines(origfile,revisedfile, linecount)
if ret then Message("Success",StrCat("Removed first ",linecount," lines"))
else Message("Failure",StrCat("Unable to remove first ",linecount," lines"))

;{If necessary, add code here to overwrite original file}

exit

Article ID:   W15944
File Created: 2004:03:30:15:41:54
Last Updated: 2004:03:30:15:41:54