Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: FileRead example needs additional asterisk
handleread = FileOpen("c:\temp\sample.txt", "read") linecount = 1 while 1 line = FileRead( handleread ) if line == "*EOF*" then break Pause("Line: ":linecount, line) linecount = linecount + 1 endwhile FileClose(handleread)
filename = "c:\temp\sample.txt" arrFile = ArrayFileGet (filename , "" , 0) linecount = ArrInfo(arrFile,6) For x = 0 to linecount-1 line = arrFile[x] Pause("Line: ":x, line) Next
Say you had a file with the following contents ...
; this is sample.txt "SLIDEMAST1",08/20/9615:37,380,144,441,93,791,72,41,22154,22154,22154 "SLIDEMAST1" ,08/20/9615:37,16,72,93,380,144,441,93,791,72,41,22154... and you want to remove the quotes and spaces, and separate the date and time with a space.
;====================snip========================== handleread=fileopen("sample.txt", "read") handlewrite=fileopen("sample22.txt", "write") while 1 line=FileRead(handleread) if line == "*EOF*" then break len=strlen(line) line=strreplace(line, " ", "") line=strreplace(line, '"SLIDEMAST1"', 'SLIDEMAST1') ;replace quotes with nothing line=strreplace(line, "/", " ") ;replace slash with space filewrite(handlewrite, line) endwhile fileclose(handleread) fileclose(handlewrite) ;====================snip==========================
Example:
;====================snip========================== handle = FileOpen("autoexec.bat", "READ") line="" while line != "*EOF" line = FileRead(handle) endwhile ;====================snip==========================Add an asterisk into the While line on the other side of "*EOF". The following line is correct:
while line != "*EOF*"NOTE that FileRead can only read alphanumeric characters in a file; it cannot read hex characters.
;====================snip========================== handleread=fileopen("comments.txt", "read") handlewrite=fileopen("newcomments.txt", "write") while 1 line=FileRead(handleread) if line == "*EOF*" then break line=strreplace(line, @lf, @crlf) filewrite(handlewrite, line) endwhile fileclose(handleread) fileclose(handlewrite) ;====================snip==========================
Is there a sample that demonstrates how to do this? At the present time I am removing all lines one at a time except the target line which takes too long. There must be a better method.
;====================snip========================== fnamein="c:\temp\sourcefile.txt" fnameout="C:\temp\printfile.txt" inny=FileOpen(fnamein,"READ") outy=FileOpen(fnameout,"WRITE") keyword="Snozzle" while 1 line=FileRead(inny) if line=="*EOF*" then break if strindexnc(line,keyword,0,@fwdscan)==0 then continue FileWrite(outy,line) endwhile FileClose(inny) FileClose(outy) Message("All Done. Print file in",fnameout) ;====================snip==========================
; Select file in which information needs to picked out. Types="All Files|*.*|WIL Files| *.wbt;*.mnu|Text Files|*.txt|" fw1=AskFileName("Select FireWall-1 FWinfo Text File", "C:\", Types, "", 1) Message("File selected was", fw1) ; go down to line containing "c:\winnt\fw\conf\objects.c" ; Copy line after all the way to the line with dashes ;"-------------". beginning = "C:\WINNT\fw\conf\objects.c" objfile=FileOpen(fw1, "READ") outfile=FileOpen("c:\winnt\fw\objects.c","WRITE") found=0 while 1 line=FileRead(objfile) if line=="*EOF*" then break if found==0 if strindexnc(line,beginning,0,@fwdscan)!=0 found=1 FileWrite(outfile,line) ; may not want this line endif else FileWrite(outfile,line) if strindex(line,"-------------",0,@fwdscan)!=0 found=0 endif endif endwhile FileClose(objfile) FileClose(outfile)
keyword="Snozzola" fnamein="c:\temp\sourcefile.txt" fnameout="C:\temp\fixedfile.txt" inny=FileOpen(fnamein,"READ") outy=FileOpen(fnameout,"WRITE") while 1 line=FileRead(inny) foundit=0 if line=="*EOF*" then break mychar=StrSub( line, 1, 1 ) if mychar == keyword then foundit=1 if foundit==1 then FileWrite(outy,line) endwhile FileClose(inny) FileClose(outy) Message("All Done. Fixed file in",fnameout)
oldline3="sdsdsdsdsdsds" ;garbage oldline2="ssdfdftewss" ; different garbage while 1 line=FileRead(handle) if line=="*EOF*" then break count=0 if line==oldline2 count=count+1 if line==oldline3 count=count+1 endif endif Message("Count is",count") oldline3=oldline2 oldline2=line endwhile
Slightly modified filecopy example from help file should do it for standard text files. If the file is REAL BIG it might take a while.
; the hard way to copy an ASCII file testfile1=FileLocate("win.ini") testfile2=strcat(FilePath(testfile1),"winini.bak") old = FileOpen(testfile1, "READ") new = FileOpen(testfile2, "WRITE") x=FileRead(old) ; discard first line while @TRUE ; Loop till break do us end x = FileRead(old) If x == "*EOF*" Then Break FileWrite(new, x) endwhile FileClose(new) FileClose(old) Message("FileClose",strcat(testfile1,@crlf,"backed up to",@crlf,testfile2))
fnamein="c:\temp\infile.txt" fnameout="C:\temp\outfile.txt" inny=FileOpen(fnamein,"READ") outy=FileOpen(fnameout,"WRITE") keyword1="Description" keyword2="IP Address" while 1 line=FileRead(inny) if line=="*EOF*" then break a=strindexnc(line,keyword1,0,@fwdscan) b=strindexnc(line,keyword2,0,@fwdscan) if a!=0 FileWrite(outy,line) else if b!=0 FileWrite(outy,line) else continue endif endif endwhile FileClose(inny) FileClose(outy)
The easy way...
fn="C:\someplace\logfile.txt" fnout="C:\someplace\logfileout.txt" line="" handlewrite=FileOpen(fnout,"WRITE") handle=FileOpen(fn,"READ") while 1 lastline=line line=FileRead(handle) if line=="*EOF*" then break endwhile FileWrite(handlewrite,lastline) FileClose(handlewrite) FileClose(handle) Message("LastLine",lastline)
junk,d,activ,recfees junk,d,activ,orcpin junk,l,actup,orcpin junk,apples,oranges,grapefruitWB Code:
fnamein="c:\temp\zinfile.txt" fnameout="C:\temp\zoutfile.txt" inny=FileOpen(fnamein,"READ") outy=FileOpen(fnameout,"WRITE") ;keywords=cases below ; Set the variable "editor", based on the pushbutton that was pressed while 1 line=FileRead(inny) if line=="*EOF*" then break item2=ItemExtract(2,line,",") item3=ItemExtract(3,line,",") item4=ItemExtract(4,line,",") lineval=strcat(item2,",",item3,",",item4) gosub myswitch endwhile :myswitch For i=1 to 4 ;this will be the number of cases you have Switch i case 1 if line=="*EOF*" then break keywords1 = "d,activ,recfees" if lineval==keywords1 FileWrite(outy,strcat(line,",","121-205002")) endif i=2 ;break case 2 if line=="*EOF*" then break keywords2 = "d,activ,orcpin" if lineval==keywords2 FileWrite(outy,strcat(line,",","121-205003")) endif i=3 ;break case 3 if line=="*EOF*" then break keywords3 = "l,actup,orcpin" if lineval==keywords3 FileWrite(outy,strcat(line,",","121-205004")) endif i=4 ;break case 4 if line=="*EOF*" then break if lineval != keywords1 && lineval != keywords2 && lineval != keywords3 FileWrite(outy,line) endif endswitch Next Return FileClose(inny) FileClose(outy) Message("All Done. Output file in",fnameout)
BoxOpen("File Finder", "Initiating...") Ahandle=fileopen("FILEA.DAT", "read") Dhandle=fileopen("FILES.LOG", "write") while @true Aline=FileRead(Ahandle) if Aline == "*EOF*" then break Afilename=ItemExtract(1,Aline,",") Bhandle=fileopen("FILEB.DAT", "read") while @true Bline=FileRead(Bhandle) if Bline == "*EOF*" then break Bfilename=ItemExtract(1,Bline,",") if Bfilename == Afilename BoxTitle(Afilename) BoxText(Bfilename) Message("File Finder", "File Found") ;TURN ON FOR DEBUGGING else BoxTitle(Afilename) BoxText(Bfilename) Message("File Finder", "File Not Found") ;TURN ON FOR DEBUGGING endif endwhile fileclose(Bhandle) Chandle=fileopen("FILEC.DAT", "read") while @true Cline=FileRead(Chandle) if Cline == "*EOF*" then break Cfilename=ItemExtract(1,Cline,",") if Cfilename == Afilename BoxTitle(Afilename) BoxText(Cfilename) Message("File Finder", "File Found") ;TURN ON FOR DEBUGGING else BoxTitle(Afilename) BoxText(Cfilename) Message("File Finder", "File Not Found") ;TURN ON FOR DEBUGGING endif endwhile fileclose(Chandle) endwhile fileclose(Ahandle) fileclose(Dhandle)
The first script needs to search for this text EXIT! - then delete that entire line.
The second script deals with e-mail addresses We can search for the @ symbol then edit each line as follows
21 0:11:27 1997: joesmith@smith.com redeemed 3425678Delete all text prior to the e-mail address and delete all text after the e-mail address then move on to the next line.
Note: the pre and post text is different on every line.
infile="c:\incoming\aaaa.txt" outfile="c:\incoming\bbbb.txt" inny=FileOpen(infile,"READ") outy=FileOpen(outfile,"WRITE") count=0 BoxOpen(count,"initializing") while 1 line=FileRead(inny) if line=="*EOF*" then break if strindexNC(line,"exit!",0,@fwdscan)!=0 then continue ptr=strindex(line,"@",0,@fwdscan) if ptr==0 then continue prevptr=strindex(line," ",ptr,@backscan)+1 nextptr=strindex(line," ",ptr,@fwdscan)-1 email=StrSub(line,prevptr,nextptr-prevptr+1) count=count+1 boxtext(email) BoxTitle(count) FileWrite(outy,email) endwhile FileClose(inny) FileClose(outy) Message("All","Done")
a=FileOpen("C:\Temp\filein.txt","READ") b=FileOpen("C:\Temp\fileout.txt","WRITE") while 1 line=FileRead(a) if line=="*EOF*" then break if line=="" then continue FileWrite(b,line) endwhile FileClose(a) FileClose(b)
Article ID: W13073
Filename: FileRead Examples.txt
File Created: 2013:04:19:08:28:18
Last Updated: 2013:04:19:08:28:18