Archive: Read In two differnt files


Read In two differnt files
I am needing the ReadFileLine to be able to handle 2 differnt txt files. if this helps both files will be the same length (same amount of lines) and i need to read in one line from one files and then read on the same line of the other one so that i can compare them.

*******Visual Example*******


need that so i can link to a webpage with one and also know the structure on the windows machine.

think i got the answer
ok so i rewrote the function ReadFileLine to accept 2 differnt files. See Attachment for Function ReWrite

Usage for the file:


StrCpy "5" $R0; set the length of files
StrCpy $1 0;reset the linecount

Loop:
Intop $1 $1 + 1 ;Sets line number to be read below
StrCmp $1 $R0 End ;End of File

Push $1 ;line number to read from
Push "text1.txt" ;First text file to read
Push "text2.txt" ;2nd text file to read
Call ReadFileLine2
Pop $0 ;output from file 2
Pop $1 ;Output from file 1
;;;Put some code of your own here;;;
Goto Loop


Hope this helps someone out there. if it was aready written somewhere else sorry i couldn't find it.

-Solwolf

Endless Loop?
Can anyone see why this would continue to repeat these 3 lines :
"hope it gets here"
"file.exe TimeStamp 1"
"file.exe TimeStamp 2"
(note no print out after Timestamp1 or 2)
***$R4 prints out C:\myProgram\temp\file.exe****


StrCpy $R4 $EXEDIR$0 ;String 2 output
IfFileExists $R4 0 +2
MessageBox MB_OK|MB_ICONINFORMATION "hope not here"
;Goto Loop
MessageBox MB_OK|MB_ICONINFORMATION "hope it gets here"

GetFileTime "$R4" $R5 $R6
MessageBox MB_OK|MB_ICONINFORMATION "$R4 TimeStamp 1 $R5"
MessageBox MB_OK|MB_ICONINFORMATION "$R4 TimeStamp 2 $R6"

Goto Loop

Note: I'm on the terminal server right now, which is Linux, so I don't have access to the NSIS manual, and I always forget which order the parameters go in FileRead. Sorry. If this code doesn't work, try swapping the variables in both FileRead commands.

FileOpen $0 "$INSTDIR\path\to\File 1" r
FileOpen $1 "$INSTDIR\path\to\File 2" r
loop:
ClearErrors
FileRead $2 $0
IfErrors done
FileRead $3 $1
IfErrors done
StrCmp $2 $3 Match NoMatch
Match:
; Code to run if both lines are the same
Goto loop
NoMatch:
; Code to run if the lines are different
Goto loop
done:
FileClose $1
FileClose $0


-dandaman32

yeah that seems like another way to pull 2 different files but that wasn't my problem....

my problem was that i Had


StrCpy "5" $R0; set the length of files
StrCpy $1 0;reset the linecount

Loop:
Intop $1 $1 + 1 ;Sets line number to be read below
StrCmp $1 $R0 End ;End of File

Push $1 ;line number to read from
Push "text1.txt" ;First text file to read
Push "text2.txt" ;2nd text file to read
Call ReadFileLine2
Pop $0 ;output from file 2
Pop $1 ;Output from file 1

StrCpy $R3 $1 12 -12 ;outputs timestamp from file 1
StrCpy $R4 $EXEDIR$0 ;String 2 output
IfFileExists $R4 0 +2
MessageBox MB_OK|MB_ICONINFORMATION "hope not here"
;Goto Loop
MessageBox MB_OK|MB_ICONINFORMATION "hope it gets here"

GetFileTime "$R4" $R5 $R6
MessageBox MB_OK|MB_ICONINFORMATION "$R4 TimeStamp 1 $R5"
MessageBox MB_OK|MB_ICONINFORMATION "$R4 TimeStamp 2 $R6"

Goto Loop


i set little msgboxes through out the program to see if i was getting the right files and such. (which all returned what i was wanting them to) File two outputs a windows formated directory and File one has the Http version of the file and the timestamp from the most recent file.

so i attach it to the exedir and get the absolute path to that file on the computer and i run iffileexsists before i do anything with it cause if it isn't there then i just download a new one. if it is there then i compare the timestamp of that file that is located on their computer. if different then i download a new one.

**but before i started putting in download statments i just poped up messages which didn't display anything for the timestamp. and it was looping infinitly through the iffileexsists section (see post #3 above)

if this helps any this is what is going on in my program and once i toss in a display after my inetload it is saying "File Open Error" i tried looking through the stack and i was getting OK being stored in stack so i thought it was from the inets above (which work!) so i just poped off the variable each time i called it but it still sticks on first file. if you need to see what the variables are printing out just leave a post i can toss in a few boxes and print out here what it is saying i really need to work so i can get back to tweaking my game.


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;Functions ;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function LineSum
Start:
Exch $0
Push $1
Push $2
ClearErrors
IfFileExists $0 0 Error
StrCpy $2 0
FileOpen $0 $0 r
IfErrors Error
FileRead $0 $1
IfErrors +3
IntOp $2 $2 + 1
Goto -3
FileClose $0
StrCpy $0 $2
Goto End
Error:
SetErrors
StrCpy $0 ''
End:
Pop $2
Pop $1
Exch $0
FunctionEnd

Function ReadFileLine2
Intialize:
Exch $0 ;file
Exch
Exch $1 ;file 2
Exch 2
Exch $2
Push $3
Push $4
Push $5
Open:
FileOpen $3 $0 r
FileOpen $4 $1 r
StrCpy $5 0
Loop:
IntOp $5 $5 + 1
ClearErrors
FileRead $3 $0
FileRead $4 $1
IfErrors +2
StrCmp $5 $2 0 Loop
FileClose $3
FileClose $4
End:
Pop $5
Pop $4
Pop $3
Pop $2
Exch $1
Exch
Exch $0
FunctionEnd

Function ConnectInternet
Start:
Push $R0
ClearErrors
Check:
Dialer::AttemptConnect
IfErrors NoIE3
Pop $R0
StrCmp $R0 "online" Connected
MessageBox MB_OK|MB_ICONSTOP "Cannot connect to the internet."
Quit
NoIE3:
MessageBox MB_OK|MB_ICONINFORMATION "Please connect to the internet now."
Connected:
Pop $R0
FunctionEnd
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;Sections ;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Section
CheckForGame:
IfFileExists "$EXEDIR\game.exe" CheckConnection EoF
CheckConnection:
Call ConnectInternet
GetNewIni:
InetLoad::load "http://www.awebsitethatdoesntexsist.com/game/update.ini" "$EXEDIR\update.ini"
Pop $0
InetLoad::load "http://www.awebsitethatdoesntexsist.com/game/winupdate.ini" "$EXEDIR\winupdate.ini"
Pop $0
GetTotalLines:
Push "update.ini"
Call LineSum
Pop $R0 ;line count of file
ResetCounter:
StrCpy $1 0
NextLine:
IntOp $1 $1 + 1 ;increment Count
StrCmp $1 $R0 EoF
Push $1 ;line # from counter
Push "update.ini" ;file 1
Push "winupdate.ini" ;file 2
Call ReadFileLine2
Pop $0 ;output from file 1
Pop $1 ;Output from file 2
StrCpy $R2 $1 12 -12 ;Newest Timestamp
StrCpy $R3 $1 -13 ;http format
StrCpy $R4 $EXEDIR$0 ;windows format
CheckForFile:
IfFileExists $R4 ValidateFile NoFileFound
NoFileFound:
InetLoad::load /popup "http://www.awebsitethatdoesntexsist.com/game/$R3" "$R4" /END
Pop $0
StrCmp $0 "OK" DownloadOk
MessageBox MB_OK|MB_ICONEXCLAMATION "Download Error, click OK to abort installation - $0" /SD IDOK
Abort
DownloadOk:
Goto NextLine
ValidateFile:
GetFileTime "$R4" $R5 $R6
MessageBox MB_OK|MB_ICONINFORMATION "$R4 TimeStamp 1 $R5"
MessageBox MB_OK|MB_ICONINFORMATION "$R4 TimeStamp 2 $R6"
Goto NextLine
EoF:
Exec "$EXEDIR\game.exe"
Delete "$EXEDIR\update.ini"
Delete "$EXEDIR\winupdate.ini"
Quit
SectionEnd

Check the value of $R4 with a MessageBox.

-Stu


Right before "CheckForFile:" $R4 it has C:\my Program\game\game.exe


$R3 has just plain game.exe
**which is what it should be.

*** I also check it after inetload to see if for somereason they were getting cleared in teh process and they still had their values as expected


and $R3 is just plain game.exe

**Note i tried deleteing this post
but didn't have rights to.


Make sure the folder "C:\my Program\game" is present so that files can be written into the folder.

-Stu


yeah they are present because i am using the $EXEDIR

StrCpy $R4 $EXEDIR$0 ;windows format


really don't know why this would be the problem but i ran another message box and i did XXXXXXX$R4'XXXXXXXXX' and it kicked the last Xs to the next line denoting a space afterwords so i went and hard coded it and it would download so it is diffently the destination directory.
**i hard coded $EXEDIR\game.exe

this is my line in the ini file that i pull from and parse:
game.exe 1159226184 (no space after the timestamp)
and i use this line of code to parse just the file:
StrCpy $R3 $1 -13

IS that the reason why i am getting a space because i only count 10 places of numbers and 1 space after exe so i guess maybe i should put -11 playing with the numbers now....

-Solwolf


maybe if there was a trim function i could just use that.


Success!


StrCpy $R2 $1 12 -12 ;Newest Timestamp
StrCpy $R3 $1 -13 ;http format
StrCpy $R4 $EXEDIR$0 ;windows format


and once i played with them awhile i figured out that this code was trimming all the space except the timestamp one


StrCpy $2 $1 12 -12 ;Newest Timestamp with spaces at end
StrCpy $R2 $2 -2 ;filtered spaces for timestamp
StrCpy $R3 $1 -13 ;http format
StrCpy $R4 $EXEDIR$0 -2 ;windows format


And now it works!