Archive: Anyway to tidy this up?


Anyway to tidy this up?
I think this works but it just doesn't look right the +3 0 look wrong

!macro FileTrans DIR File
IfFileExists "$INSTDIR\${DIR}\${File}" +3 0
CreateDirectory "$INSTDIR\${DIR}\"
CopyFiles /Silent "$9${DIR}\${File}" "$INSTDIR\${DIR}\${File}"
!macroend

Basically if the file exist it should skip create dir and copy file and carry on to the next !insertmacro FileTrans "blah" "blah"

And obviously if the file doesn't exist it should copy the file from CD to the install dir

But is the code right or wrong? If not do I use Logiclib.nsh and use ${if} and so on, something like this

!macro FileTrans DIR File
${If} ${FileExists} "$INSTDIR\${DIR}\${File}"
???What goes here though???
${Else}
CreateDirectory "$INSTDIR\${DIR}\"
CopyFiles /Silent "$9${DIR}\${File}" "$INSTDIR\${DIR}\${File}"
${EndIf}
!macroend

Would someone be willing to tidy the code up please to state if its right please

Thank You


you can leave it empty, but its probably better to flip the if and use ${Unless} instead of ${If}


That's doing the opposite of what I would like it to do

That's saying because the file exist it creates a dir and copies the file

I want it to see if the files exists, if it exists it skips copying the files from the CD to install dir


I've just had another thought could I do this and how

If the file exists in the folder
copy the file to a backup folder
but
if the file doesn't exist in the folder
copy the file from the cd to the folder

How would I go about doing this, some visible code would be greatly appreciated not just a reference to the manual

Thank you

[Edit]

This should work wont it?

!macro FileTrans DIR File
IfFileExists "$INSTDIR\${DIR}\${File}" 0 +3
CreateDirectory "$INSTDIR\${BackupDir}\${DIR}\"
CopyFiles /Silent "$INSTDIR\${DIR}\${File}" "$INSTDIR\${BackupDir}\${DIR}\${File}"
${if} ${FileExists} "$INSTDIR\${DIR}\${File}"
${Else}
CreateDirectory "$INSTDIR\${DIR}\"
CopyFiles /Silent "$9${DIR}\${File}" "$INSTDIR\${DIR}\${File}"
${EndIf}
!macroend


Do just like Anders said before:

${If} Something
A
${Else}
B
${EndIf}

Can be also written as:

${Unless} Something
B
${Else}
A
${EndUnless}

Greets
Rafał


Ahh!!

I had it

${Unless} Something
B
${Else}
A
${EndIf}

So I was wrong

but This doesn't work the way I want it to either

${Unless} Something
B
${Else}
A
${EndUnless}

So I'm sticking with

!macro FileTrans DIR File
${if} ${FileExists} "$INSTDIR\${DIR}\${File}"
${Else}
CreateDirectory "$INSTDIR\${DIR}\"
CopyFiles /Silent "$9${DIR}\${File}" "$INSTDIR\${DIR}\${File}"
${EndIf}
!macroend

with the added addition of

!macro FileTrans DIR File
IfFileExists "$INSTDIR\${DIR}\${File}" 0 +3
CreateDirectory "$INSTDIR\${BackupDir}\${DIR}\"
CopyFiles /Silent "$INSTDIR\${DIR}\${File}" "$INSTDIR\${BackupDir}\${DIR}\${File}"

${if} ${FileExists} "$INSTDIR\${DIR}\${File}"
${Else}
CreateDirectory "$INSTDIR\${DIR}\"
CopyFiles /Silent "$9${DIR}\${File}" "$INSTDIR\${DIR}\${File}"
${EndIf}
!macroend

I've tested it a few time now and it works for what I want it to do, which is

If file exist
copy file to backup
if the file doesn't exist
copy file from CD to install dir

So what's the difference between ${Unless} and ${If} ?

There is no explanation in the manual


Howdy again

${Unless} means literally ${IfNot} but of course there is no ${IfNot} in logicLib, anyway logicLib is much better than assembler-like Gotos.

You may rewrite your macro a bit, like following:

!macro FileTrans DIR File
${If} IfFileExists "$INSTDIR\${DIR}\${File}"
CreateDirectory "$INSTDIR\${BackupDir}\${DIR}\"
CopyFiles /Silent "$INSTDIR\${DIR}\${File}" "$INSTDIR\${BackupDir}\${DIR}\${File}"
${Else}
CreateDirectory "$INSTDIR\${DIR}\"
CopyFiles /Silent "$9${DIR}\${File}" "$INSTDIR\${DIR}\${File}"
${EndIf}
!macroend

or using ${Unless} other way around:

!macro FileTrans DIR File
${Unless} IfFileExists "$INSTDIR\${DIR}\${File}"
CreateDirectory "$INSTDIR\${DIR}\"
CopyFiles /Silent "$9${DIR}\${File}" "$INSTDIR\${DIR}\${File}"
${Else}
CreateDirectory "$INSTDIR\${BackupDir}\${DIR}\"
CopyFiles /Silent "$INSTDIR\${DIR}\${File}" "$INSTDIR\${BackupDir}\${DIR}\${File}"
${EndUnless}
!macroend

Hope I didn't mess anything :-)

Greets
Rafał


Ok thanks I'll try both

Thanks again

Nope get an error

!insertmacro: macro "_If" requires 4 parameter(s), passed 3!

It'll be to do with this line

${If} IfFileExists "$INSTDIR\${DIR}\${File}"


IfFileExists is not a LogicLib macro, it's a built command. You are looking for ${FileExists}.

!macro FileTrans DIR File
${Unless} ${FileExists} "$INSTDIR\${DIR}\${File}"
CreateDirectory "$INSTDIR\${DIR}"
CopyFiles /Silent "$9${DIR}\${File}" "$INSTDIR\${DIR}\${File}"
${EndIf}
!macroend

Ok then is there a way to incorporate the highlighted part any better or is it ok as it is? It works as it should



!macro FileTrans DIR FILE
IfFileExists "$INSTDIR\${DIR}\${FILE}" 0 +3
CreateDirectory "$INSTDIR\${BACKDIR}\${DIR}\"
CopyFiles /Silent "$INSTDIR\${DIR}\${FILE}" "$INSTDIR\${BACKDIR}\${DIR}\${FILE}"

${Unless} ${FileExists} "$INSTDIR\${DIR}\${FILE}"
CreateDirectory "$INSTDIR\${DIR}"
CopyFiles /Silent "$9${DIR}\${FILE}" "$INSTDIR\${DIR}\${FILE}"
${EndIf}
!macroend

Thanks to both of you


Ok, this time without stupid mistakes:

!macro FileTrans DIR File
${If} ${FileExists} "$INSTDIR\${DIR}\${File}"
CreateDirectory "$INSTDIR\${BackupDir}\${DIR}\"
CopyFiles /Silent "$INSTDIR\${DIR}\${File}" "$INSTDIR\${BackupDir}\${DIR}\${File}"
${Else}
CreateDirectory "$INSTDIR\${DIR}\"
CopyFiles /Silent "$9${DIR}\${File}" "$INSTDIR\${DIR}\${File}"
${EndIf}
!macroend

or using ${Unless} other way around:

!macro FileTrans DIR File
${Unless} ${FileExists} "$INSTDIR\${DIR}\${File}"
CreateDirectory "$INSTDIR\${DIR}\"
CopyFiles /Silent "$9${DIR}\${File}" "$INSTDIR\${DIR}\${File}"
${Else}
CreateDirectory "$INSTDIR\${BackupDir}\${DIR}\"
CopyFiles /Silent "$INSTDIR\${DIR}\${File}" "$INSTDIR\${BackupDir}\${DIR}\${File}"
${EndUnless}
!macroend

Should work now.
Greets Rafał