Girish
Girish

Reputation: 399

WIX invoking Batch file in InstallExecution Sequence

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>

Upvotes: 8

Views: 23049

Answers (4)

mystack
mystack

Reputation: 5492

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>

Upvotes: 1

Girish
Girish

Reputation: 399

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>

Upvotes: 10

Alexey Ivanov
Alexey Ivanov

Reputation: 11848

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

Upvotes: 1

Yan Sklyarenko
Yan Sklyarenko

Reputation: 32250

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.

Upvotes: 4

Related Questions