Archive: File command, is this possible?


File command, is this possible?
I've posted this before but I can't find it.

I have a directory structure as follows:
MAIN_DIR
---Client1
---Client2
---Client3

I !define which client the installer is being compiled for.

If there is a file called test.txt in MAIN_DIR and a file called test.txt in the Client1 directory, I want to include the one from Client1 only.

If there is a file called test.txt in MAIN_DIR and not in the Client1 directory, I want to include the one from MAIN_DIR.

In other words, if there is a file in the defined Client directory, it should override the one in the MAIN_DIR.

Is this possible?

Thanks.


Did you try IfFileExists command?

You should now also jump labels or Goto instruction.


IfFileExists, Delete and Rename commands are going to work for you in this case EXCEPT if you want to include it inside the installer as a File command.


I've done something very much like this before.

!macro ClientFile FILE_NAME
!system 'if exist ${CLIENT_DIR}\${FILE_NAME} \
echo File ${CLIENT_DIR}\${FILE_NAME} >$%TMP%\temp.nsh \
else \
echo File ${MAIN_DIR}\${FILE_NAME} >$%TMP%\temp.nsh'
!include $%TMP%\temp.nsh
!macroend


!insertmacro ClientFile test.txt

I've not tested the above, but it was something along those lines.

Thanks eccles.

This is what I ended up with.

All of the files in question end up in one temporary file which is then !includ(ed)

${kCLIENT} is a !define that is set up in the BAT file that calls the installer.




# -----------------------------------------------------------------------------------------------------------
# MACROs for checking if the file should come from the specific client directory of the main directory.
# -----------------------------------------------------------------------------------------------------------

# The name of the temporary file.
!define CLIENT_TEMP_FILE "$%TMP%\temp.nsh"

# Initialize. Delete any existing temp file.
!macro InitClientFile
!system 'del /q "${CLIENT_TEMP_FILE}"'
!macroend

# Put a SetOutPath line before a list of files.
!macro AddPathClientFile THE_PATH
!system 'echo. >>"${CLIENT_TEMP_FILE}"'
!system 'echo SetOutPath "${THE_PATH}" >>"${CLIENT_TEMP_FILE}"'
!macroend

# Accept the top-level directory and filename and decide whether to take the file
# from the CLIENT sub-directory or the main top-level directory.
!macro ClientFile FILE_DIRECTORY FILE_NAME
!system 'if exist "${FILE_DIRECTORY}\${kCLIENT}\${FILE_NAME}" \
(echo File "${FILE_DIRECTORY}\${kCLIENT}\${FILE_NAME}" >>"${CLIENT_TEMP_FILE}") \
else \
echo File "${FILE_DIRECTORY}\${FILE_NAME}" >>"${CLIENT_TEMP_FILE}"'
!macroend

# Add these file to the installer script.
!macro IncludeClientFile
!include "${CLIENT_TEMP_FILE}"
!macroend

# -----------------------------------------------------------------------------------------------------------
# END - MACROs for checking if the file should come from the specific client directory of the main directory.
# -----------------------------------------------------------------------------------------------------------


To use it in the installer:


!insertmacro InitClientFile

!insertmacro AddPathClientFile "$ApplicationDir\myDir"
!insertmacro ClientFile "..\myDir" "aaa.txt"
!insertmacro ClientFile "..\myDir" "bbb.txt"
!insertmacro ClientFile "..\myDir" "ccc.txt"

!insertmacro AddPathClientFile "$ApplicationDir\myDir2"
!insertmacro ClientFile "..\myDir2" "xxx.txt"
!insertmacro ClientFile "..\myDir2" "yyy.txt"
!insertmacro ClientFile "..\myDir2" "zzz.txt"

!insertmacro IncludeClientFile

I suggest you add it to the NSIS Archive (if you haven't already)!