Wix- How to copy a directory to install folder

Jack He picture Jack He · Oct 24, 2014 · Viewed 27.5k times · Source

I have a Bin folder with many files in my project. For now, I only know how to add specific files to install folder with the code below.

<File Source='$(var.Debug)Myapplication.exe' Id='file4028276A0CAD84E3BDE449237AE31A5F' />  

However, I want to move a whole directory to the install path. For example, move the whole folder "Bin" to the install path "C:\Myapplication".

What should I do then?

Thanks in advance!

Answer

Ryan J picture Ryan J · Oct 25, 2014

Sounds like what you want to use is the WiX tool heat, which is used to "harvest" a directory (or individual files) and create a WiX fragment file that you can use in your project.

For example, you have a directory you want to harvest, and it may include subdirectories, but there's a lot of files, and you want it all... heat will do that for you.

Consider this trivial structure:

Somedir
    |
    |---A file.txt
    |---An init file.ini
    |---another file.txt
    |---subfolder
            |
            |---a subfolder file.txt

If you use heat to harvest this directory, the tool will generate the fragment file for you that you can use as a component reference in your project without having to specify the files one at a time or use a workaround solution.

For example, the following heat command will process this directory (from one level up in this example)

heat dir somedir -o MyHarvestedStuff.wxs -scom -frag -srd -sreg -gg -cg MyComponentGroupId -dr BIN_DIR_REF

Where:

dir = harvest a directory
somedir = directory you want to harvest
-o MyHarvestedStuff.wxs = the output fragment file
-scom -sfrag -srd -sreg = Suppress COM elements, fragments, root directory as element, registry harvesting (these options will create a grouping that most applications can use)
-gg = generate GUID's now (this will put the GUID's into your output file, rather than using a wildcard "*". The advantage here is you can use non-standard TARGETDIR, which would not qualify for autogenerated GUID's)
-cg MyComponentGroupId = component group. this is what you will use in your feature set to include these files
-dr BIN_DIR_REF = this is the directory reference for which to place the files in your final package.

The resulting XML looks like this (this was run without -gg to avoid posting real GUID's)

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="BIN_DIR_REF">
            <Directory Id="dirF065D7446868E03DB0B296EBADA4E4A1" Name="subfolder" />
        </DirectoryRef>
    </Fragment>
    <Fragment>
        <ComponentGroup Id="MyComponentGroupId">
            <Component Id="cmp739547000B47E975B0452D876AF7810B" Directory="BIN_DIR_REF" Guid="PUT-GUID-HERE">
                <File Id="fil09B311A6D1EABD9E94DFA5C83F59C548" KeyPath="yes" Source="SourceDir\A file.txt" />
            </Component>
            <Component Id="cmp84C8400F892D39B05EE3021CCEEAA14F" Directory="BIN_DIR_REF" Guid="PUT-GUID-HERE">
                <File Id="fil11A22646343997D26AC54171A62DFF4C" KeyPath="yes" Source="SourceDir\an init file.ini" />
            </Component>
            <Component Id="cmpFA266FC6F3269CB5D9E42C38FC995117" Directory="BIN_DIR_REF" Guid="PUT-GUID-HERE">
                <File Id="filA545B6E4B63B8211E982917FC78F6EB4" KeyPath="yes" Source="SourceDir\another file.txt" />
            </Component>
            <Component Id="cmp2EC5C1618A59F47B7BDE800EB9AA8688" Directory="dirF065D7446868E03DB0B296EBADA4E4A1" Guid="PUT-GUID-HERE">
                <File Id="filB0CD0B02385137DC806112E293083459" KeyPath="yes" Source="SourceDir\subfolder\a subfolder file.txt" />
            </Component>
        </ComponentGroup>
    </Fragment>
</Wix>

In your project file, you'd have something like this under your root <Directory> element:

<Directory Id="BIN_DIR_REF" Name="bin">
    <!-- anything else you might put here...-->
</Directory>

And in your feature group:

<Feature Id="Complete" Level="1">
    ...
    <ComponentGroupRef Id="MyComponentGroupId" />         
    ...
</Feature>

Tying it all together...

  1. Supply your new fragment file to candle along with your other file(s)
  2. Supply the .wixobj file that candle produces to light
  3. Resolve the SourceDir reference in your new fragment file with a WiX preprocessor variable or with the -b option to light

    Ex: light ... -b "path to my directory that I harvested" ...

Don't let the length of the answer deter you from exploring this solution, it works quite well and it's pretty simple. Now if you want to exclude anything from that directory, that's a whole other story...