Archive: macro for patch Files


macro for patch Files
my macros:


!macro patchFileProd1 filename
Var /global fp_$filename
Var /global fp_patched_$filename
Var /global template_$filename
Var /global patched_$filename
/* Datei einlesen und schreiben*/
ClearErrors
FileOpen $fp_$filename '${copiedfiles_Path}\Templates\bin\$filename' r
IfErrors done_$filename
ClearErrors
SetOutPath '$INSTDIR\bin'
FileOpen $fp_patched_$filename '$filename' w
IfErrors not_patched_$filename
ClearErrors
loop_$filename:
FileRead $fp_$filename $template_$filename
IfErrors eof_$filename
!macroend

!macro patchFileProd2 filename
FileWrite $fp_patched_$filename $patched_$filename
iferrors 0 +2
DetailPrint '!!!!!!!! Konnte nicht in $filename schreiben!!!!!!!!!'
Goto loop_$filename

eof_$filename:
FileClose $fp_patched_$filename
FileClose $fp_$filename
Goto +4

not_patched_$filename:
DetailPrint '!!!!!!!! Konnte $filename nicht anlegen!!!!!!!!'
Goto +2

done_$filename:
DetailPrint '!!!!!!!! Konnte template_$filename nicht öffnen(lesen)!!!!!!!!'
!macroend


here the use of the macros

StrCpy $1 'diagcld_register.cmd'

!insertmacro patchFile1 $1
${StrRep} $patched_$filename "$template_$filename" "$DB" "new_DB"
!insertmacro patchFile2 $1



invalid characters in variable name "fp$filename", use only characters [a-z][A-Z][0-9] and '_'
Error in macro patchFileProd1 on macroline 1

What is wrong?

fp_$filename --> Error
fp$filename --> Error

Macro parameters should be reffered by ${parameter}, $parameter is wrong.

Is it intention to declare variables in macro?

It is interesting that second macro contains Goto which points to first macro.


yea i want to devide it, but now i have a better solution, thanks for help


ok other question same Problem

my script:


StrCpy $1 'diagcld_register.cmd'
!insertmacro patchFileProd '${copiedfiles_Path}\Templates\bin' '$INSTDIR\bin' $1 '0' '0' '0' '0' '0' '0' '0' '1'



my macro:

!macro patchFileProd templatePath zielpfad filename v1 v2 v3 v4 v5 v6 v7 v8
Var /global fp
Var /global fp_patched
#Var /global template
Var /global patched
/* Datei einlesen und schreiben */
ClearErrors
FileOpen $fp '${templatePath}\${filename}' r #
#Messagebox MB_OK "temp lesen"
IfErrors done_${filename}
ClearErrors
SetOutPath '${zielpfad}'
FileOpen $fp_patched '${filename}' w
IfErrors not_patched_${filename}
ClearErrors
loop_${filename}:
FileRead $fp $patched
IfErrors eof_${filename}

StrCmp ${v1} '1' 0 +2
Messagebox MB_OK "${v1}"
#${StrRep} $patched "$patched" "%DBListenerport%" "$ProduktivDB_Listenerport"
StrCmp ${v2} '1' 0 +2
Messagebox MB_OK "${v2}"
#${StrRep} $patched "$patched" "%Hostname%" "$ProduktivDB_Hostname"
StrCmp ${v3} '1' 0 +2
Messagebox MB_OK "${v3}"
#${StrRep} $patched "$patched" "%Server%" "$Servername"
StrCmp ${v4} '1' 0 +2
Messagebox MB_OK "${v4}"
#${StrRep} $patched "$patched" "%DBSID%" "$ProduktivDB_DBSID"
StrCmp ${v5} '1' 0 +2
Messagebox MB_OK "${v5}"
#${StrRep} $patched "$patched" "%User%" "$ProduktivDB_Mainuser"
StrCmp ${v6} '1' 0 +2
Messagebox MB_OK "${v6}"
#${StrRep} $patched "$patched" "%TNSname%" "$ProduktivDB_TNSname"
StrCmp ${v7} '1' 0 +2
Messagebox MB_OK "${v7}"
#${StrRep} $patched "$patched" "%Passwort%" "$ProduktivDB_MainuserPasswort"
StrCmp ${v8} '1' 0 +2
${StrRep} $patched "$patched" "%Pfad%" "$INSTDIR"
Messagebox MB_OK "${v8}"
FileWrite $fp_patched $patched
IfErrors 0 +2
DetailPrint '!!!!!!!! Konnte nicht in ${zielpfad}\${filename} schreiben!!!!!!!!!'
Goto loop_${filename}

eof_${filename}:
FileClose $fp_patched
FileClose $fp
Goto +4

not_patched_${filename}:
DetailPrint '!!!!!!!! Konnte ${filename} nicht anlegen!!!!!!!!'
Goto +2

done_${filename}:
DetailPrint '!!!!!!!! Konnte ${templatePath}\${filename} nicht öffnen(lesen)!!!!!!!!'
!macroend


why i have always

v1 = 1
v2 = 1
v3 = 1
v4 = 1
.
.
.

ok, i have a Problem and i cant solve it.

i have to patch some Files. And i want to write a macro.

my Problem is, if i use a Variable or a Label and use the macro more then one times, he says the variable or label is already declared. Then i used relative jumps, but then he writes "succsess123167succsess128167succsess183167succsess133167"
at the end of the File or in some tets he wrote only the first line and then only "succsess".

the original code is this:


!macro patchFileTest templatePath zielpfad filename

ClearErrors
FileOpen $R0 '${templatePath}\${filename}' r #
IfErrors done_test_${filename}
ClearErrors
SetOutPath '${zielpfad}'
FileOpen $R1 '${filename}' w
IfErrors not_patched_test_${filename}
ClearErrors
loop_test_${filename}:
FileRead $R0 $R3
IfErrors eof_test_${filename}

#StrCmp ${v1} '1' 0 +2
${StrRep} $R3 "$R3" "%DBListenerport%" "$TestDB_Listenerport"
#StrCmp ${v2} '1' 0 +2
${StrRep} $R3 "$R3" "%Hostname%" "$TestDB_Hostname"
#StrCmp ${v3} '1' 0 +2
${StrRep} $R3 "$R3" "%Server%" "$Servername"
#StrCmp ${v4} '1' 0 +2
${StrRep} $R3 "$R3" "%DBSID%" "$TestDB_DBSID"
#StrCmp ${v5} '1' 0 +2
${StrRep} $R3 "$R3" "%User%" "$TestDB_Mainuser"
#StrCmp ${v6} '1' 0 +2
${StrRep} $R3 "$R3" "%TNSname%" "$TestDB_TNSname"
#StrCmp ${v7} '1' 0 +2
${StrRep} $R3 "$R3" "%Passwort%" "$TestDB_MainuserPasswort"
#StrCmp ${v8} '1' 0 +2
${StrRep} $R3 "$R3" "%Pfad%" "$INSTDIR${TestDB}"

FileWrite $R1 $R3
IfErrors +2 0
Goto loop_test_${filename}
DetailPrint '!!!!!!!! Konnte nicht in ${zielpfad}\${filename} schreiben!!!!!!!!!'


eof_test_${filename}:
FileClose $R1
FileClose $R0
Goto +4

not_patched_test_${filename}:
DetailPrint '!!!!!!!! Konnte ${filename} nicht anlegen!!!!!!!!'
Goto +2

done_test_${filename}:
DetailPrint '!!!!!!!! Konnte ${templatePath}\${filename} nicht öffnen(lesen)!!!!!!!!'
!macroend


my last test was this:

!include StrFunc.nsh
${StrRep}

/* ProduktivDB */
!macro patchFileProd param1 param2 param3
!define templatePath ${param1}
!define zielpfad ${param2}
!define filename ${param3}
Messagebox MB_OK "${param1}"
Messagebox MB_OK "${param2}"
Messagebox MB_OK "${param3}"
Messagebox MB_OK "${templatePath}"
Messagebox MB_OK "${zielpfad}"
Messagebox MB_OK "${filename}"
call patchFile
!undef templatePath
!undef zielpfad
!undef filename
!macroend

Function patchFile
/* Datei einlesen und schreiben */
Messagebox MB_OK "${param1}"
Messagebox MB_OK "${param2}"
Messagebox MB_OK "${param3}"
Messagebox MB_OK "${templatePath}"
Messagebox MB_OK "${zielpfad}"
Messagebox MB_OK "${filename}"
ClearErrors
FileOpen $R0 '${templatePath}\${filename}' r #
IfErrors done_${filename} #+25
ClearErrors
SetOutPath '${zielpfad}'
FileOpen $R1 '${filename}' w
IfErrors not_patched_${filename} #+19
loop_${filename}:
ClearErrors
FileRead $R0 $R3
IfErrors eof_${filename} #+13

#StrCmp ${v1} '1' 0 +2
${StrRep} $R3 "$R3" "%DBListenerport%" "$ProduktivDB_Listenerport"
#StrCmp ${v2} '1' 0 +2
${StrRep} $R3 "$R3" "%Hostname%" "$ProduktivDB_Hostname"
#StrCmp ${v3} '1' 0 +2
${StrRep} $R3 "$R3" "%Server%" "$Servername"
#StrCmp ${v4} '1' 0 +2
${StrRep} $R3 "$R3" "%DBSID%" "$ProduktivDB_DBSID"
#StrCmp ${v5} '1' 0 +2
${StrRep} $R3 "$R3" "%User%" "$ProduktivDB_Mainuser"
#StrCmp ${v6} '1' 0 +2
${StrRep} $R3 "$R3" "%TNSname%" "$ProduktivDB_TNSname"
#StrCmp ${v7} '1' 0 +2
${StrRep} $R3 "$R3" "%Passwort%" "$ProduktivDB_MainuserPasswort"
#StrCmp ${v8} '1' 0 +2
${StrRep} $R3 "$R3" "%Pfad%" "$INSTDIR"

FileWrite $R1 $R3
IfErrors +2
Goto loop_${filename} #-15

#Messagebox MB_OK "!!!!!!!! Konnte nicht in ${zielpfad}\${filename} schreiben!!!!!!!!!"
DetailPrint '!!!!!!!! Konnte nicht in ${zielpfad}\${filename} schreiben!!!!!!!!!'

eof_${filename}:
FileClose $R1
FileClose $R0
Goto +4

not_patched_${filename}:
#Messagebox MB_OK "!!!!!!!! Konnte ${filename} nicht anlegen!!!!!!!!"
DetailPrint '!!!!!!!! Konnte ${filename} nicht anlegen!!!!!!!!'
Goto +2

done_${filename}:
#Messagebox MB_OK "!!!!!!!! Konnte ${templatePath}\${filename} nicht öffnen(lesen)!!!!!!!!"
DetailPrint '!!!!!!!! Konnte ${templatePath}\${filename} nicht öffnen(lesen)!!!!!!!!'
FunctionEnd



the Messageboxes are for test.

Can anybody help me?

The relative jumps don't work because you are trying to jump over a macro, and the relative jump ends up jumping inside the macro instead of over it.

Try using the LogicLib.nsh header. It will create jump labels that are unique even if the macro is used more than once.


ok i changed it , i use the jumps in a Function and now it works perfectly. LogicLib.nsh is the next whati will test. Thanks for help demiller9.