8

I am new to WIX and I am trying to invoke a batch file from my WIX installer. MyBatch.bat contains a simple Copy of a Text file from c:\ to D:\

I am unable to invoke the batch file. Below is my code.

Please help me out.

  <?xml version="1.0" encoding="UTF-8"?>
  <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <?define ProductName='Test Product' ?>
  <?define ProductVersion='1.0.0.0' ?>
  <?define ProductCode='b7bc7c6f-9a4e-4973-be84-eca8e3427c97'?>
  <?define UpgradeCode='06a81104-1e30-463d-87e1-e8a79b4c682a'?>
  <?define Manufacturer='Respond'?>
  <Product Id="$(var.ProductCode)" Language="1033" Version='$(var.ProductVersion)' Name='Respond'
  Manufacturer='$(var.Manufacturer)' UpgradeCode='$(var.UpgradeCode)'>
    <Package InstallerVersion='200' Compressed='yes' />
    <Media Cabinet='media1.cab' EmbedCab='yes' Id ='1' />
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id='ProgramFilesFolder'>
        <Directory Id='INSTALLDIR' Name='$(var.ProductName)'>
          <Component Id='ProductComponent' Guid='b11556a2-e066-4393-af5c-9c9210187eb2'>
            <File Id='Calc' DiskId='1' Source='C:\WINDOWS\system32\calc.exe'/>
            <File Id='my_batch_script' Name='MyBatch.bat' DiskId='1' Source='MyBatch.bat'   />
          </Component>
        </Directory>
      </Directory>
      <Directory Id='ProgramMenuFolder'>
        <Directory Id='ApplicationProgramsFolder' Name='$(var.ProductName)'>
          <Component Id='MainExecutable' Guid='EDED00D8-2465-46CA-86D6-B20DE921EFA6'>
            <Shortcut Id='ShortcutCalc' Description='$(var.ProductName)' Name ='Calculator of Windows'
            Target='[INSTALLLOCATION]Calc.exe' WorkingDirectory='INSTALLLOCATION'/>
            <RemoveFolder Id='ApplicationProgramsFolder' On='uninstall'/>
            <RegistryValue Root='HKCU' Key='Software\$(var.Manufacturer)\$(var.ProductName)' Type ='multiString'
            Name='installed' Value='1' KeyPath='yes'/>
          </Component>
        </Directory>
      </Directory>
    </Directory>

    <Feature Title='SetupProject1' Level='1' Id ='AppFeature'>
      <ComponentRef Id='ProductComponent' Primary='yes'/>
      <ComponentRef Id='MainExecutable'/>
    </Feature>

    <Property Id='WIXUI_INSTALLDIR' Value='INSTALLDIR' ></Property>
    <WixVariable Id='WixUILicenseRtf' Overridable='yes' Value='License.rtf'/>
    <?include CustomActions.wxi?>
    <UI>
      <?include UISequence.wxi?>
      <?include Dialogs.wxi?>
    </UI>

    <CustomAction Id="BatchCmd" Property="ThePath" Value="[INSTALLDIR]MyBatch.bat" />
    <CustomAction Id="BatchRun" Property="ThePath" ExeCommand='"[INSTALLDIR]MyBatch.bat"' Return='asyncWait' Impersonate="yes"/>

    <InstallExecuteSequence>
      <Custom Action="BatchCmd" After="InstallFiles" >Not Installed </Custom>
      <Custom Action="BatchRun" After="BatchCmd">Not Installed </Custom>
    </InstallExecuteSequence>
  </Product>

</Wix>
Girish
  • 389
  • 2
  • 4
  • 12

4 Answers4

9

This worked for me.

<CustomAction Id="test"
    ExeCommand="[INSTALLDIR]MyBatch.bat"
    Directory="INSTALLDIR" Execute="deferred" Return="asyncWait"/>

<InstallExecuteSequence>
    <Custom Action="test" After="InstallFiles">NOT Installed</Custom>
</InstallExecuteSequence>
it3xl
  • 2,372
  • 27
  • 37
Girish
  • 389
  • 2
  • 4
  • 12
  • can you add the text of the bat file to your answer? Just wondering what's inside... – Yan Sklyarenko Mar 06 '12 at 09:13
  • A good news. Any whitespaces are allowed in *INSTALLDIR* and in the `ExeCommand="[INSTALLDIR]MyBatch.bat"`. I.e. no need for the single quote trick `ExeCommand='"[INSTALLDIR]MyBatch.bat"'` – it3xl May 03 '18 at 14:45
4

It seems you're solving your problem with the wrong tool. If the file to copy is a part of your application, you should author a Component with this File inside. Otherwise, if you need just to copy some external file already present on a target system, you should author a CopyFile element, nesting it under Component element.

Yan Sklyarenko
  • 31,557
  • 24
  • 104
  • 139
1

Add your CustomAction defnition as given below. Make sure that Return="ignore" if you are not returning anything from bat file.

<CustomAction Id="RunBat" Directory="your_directory"
    ExeCommand='"c:\test\test.BAT"'
    Execute='deferred' Impersonate='no' Return='ignore'/>

Also in the installExecuteSequence sequence add the action before InstallFinalize

<InstallExecuteSequence>
    <Custom Action="RunBat" Before="InstallFinalize">
        NOT (REMOVE~="ALL")
    </Custom>
</InstallExecuteSequence>
it3xl
  • 2,372
  • 27
  • 37
mystack
  • 4,910
  • 10
  • 44
  • 75
  • I want to mentions that this works with whitespaces and without single quotes too `ExeCommand="C:\0 0 0\my batch file.bat some_param"` – it3xl May 03 '18 at 15:02
1

I agree with Yan, you solve your task the wrong way.


As for batch files, they are not executed on its own like an .exe, it is cmd.exe which reads the bat file and executes it line by line.

To run a batch file, you should run it this way:

cmd.exe /c batch.bat
Community
  • 1
  • 1
Alexey Ivanov
  • 11,541
  • 4
  • 39
  • 68
  • 1
    [ExeCommand="c:\test\test.BAT"](https://stackoverflow.com/questions/9563820/wix-invoking-batch-file-in-installexecution-sequence/45918334#45918334) really works – it3xl May 03 '18 at 11:42
  • NOT (REMOVE~="ALL") works for me. – hetptis Nov 08 '19 at 08:15