How to run a ".bat" file during installation? - batch-file

In a Setup project the executable files such as ".exe , .dll , .js , .vbs" are acceptable but there is no way to run a .bat file in a Custom Action.
The question is how to run the *.bat files during installation?

Well, after much searching and trial and error I have solved this. I'm not sure if this is the best way, but it works.
Here's the scenario: I have an application I would like to deploy via a Visual Studio Setup project. In addition to my application files, I would like to create a subdirectory in the target directory that contains a batch (.bat) file. I would like this file to run at the end of the installation process.
Here's what you do:
Create a setup project and configure as you normally would, including the subdirectory in which you'll place your batch file (you can just place it in the Application Folder directly if you don't want it in a subdirectory).
In the "File System" view (right-click on the project in Solution Explorer->View->File System), add the batch file you want to execute and cmd.exe (C:\Windows\System32\cmd.exe)
Open the "Custom Actions" view (right-click on the project in Solution Explorer->View->Custom Actions)
Right-click on "Commit" and choose "Add Custom Action"
Navigate to and select cmd.exe.
Open the properties panel for the newly created custom action.
Delete /Commit from the Arguments property.
Enter: /c "[TARGETDIR]subdirectoryname\batchfile.bat" in the Arguments property, where subdirectoryname should be replaced by the name of your subdirectory (if you put the batch file in a subdirectory like I did... if you didn't, the value should be /c "[TARGETDIR]batchfile.bat") and batchfile.bat should be the filename of your batch file.
That's it. The batch file will now be executed once the rest of the installation process is completed.
Here's an example for the sake of clarity:
My batch file: blah.bat
My subdirectory: mydir
The value of the Arguments for my custom action targeting cmd.exe would then be
/c "[TARGETDIR]mydir\blah.bat"

One other way to reach the same result is put a .vbs file in custom actions that runs the correspondent .bat file.
The following code is the "RunRegisterComponents.vbs" I put in setup application folder. Of course I put [TARGETDIR] as .vbs parameter in Visual Studio property window.
dim WshShell
Set WshShell = CreateObject("WScript.Shell")
' Read the "CustomActionData" property holding the install directory.
dim programDir
programDir= property("CustomActionData")
' Make the batch full file name and parameter
commandString = chr(34) & programDir & "RegisterComponents.bat" & chr(34) & " " & chr(34) &
programDir& chr(34)
' Set the current directory
WshShell.CurrentDirectory = programDir
' Run batch.
ret = WshShell.Run (commandString, 0, 0)
That is as I set my custom actions:
I hope this can help you!

Check this article (article is deprecated), even though it is in VB.NET it applies to C# as well. The most important part is (translated to C#) creating a new Class Library, and adding a new Installer Class with the following content: As stated in the article you can then create a new custom action with a reference to your just created project.
override void Commit(IDictionary savedState)
{
base.Commit(savedState);
System.Diagnostics.Process.Start("myApp.bat","your bat arguments");
}
Now we are adding batch file to your installer project. Create a setup project and configure as you normally would, including the subdirectory in which you'll place your batch file (you can just place it in the Application Folder directly if you don't want it in a subdirectory).
In the "File System" view (right-click on the project in Solution Explorer->View->File System), add the batch file you want to execute.
Build the installer project.

If you are trying to run a batch file that have relative paths during the installation process, that will fail for sure. That's because the batch file will take into account the directory where the installer is running, and not where the files were being installed. Use installer builders that copies batch files into temporary directory.

Related

Creating Batch file to Run a Blue Prism Process

I need to create a .bat file in order to run my Blue Prism process via CMD.
The commands are as follows (in order):
cd C:\Program Files\Blue Prism Limited\Blue Prism Automate
AutomateC.exe /user umang abcd1234 /run MyDemoProcess
Pardon if my question looks too basic as I am new to this stuff.
In order to run any process from a batch file, please follow the below steps:
1) Open a notepad and type the below command :
"C:\Program Files\Blue Prism Limited\Blue Prism Automate\AutomateC.exe" /user umang abcd1234 /run "MyDemoProcess"
2) Save the file with .bat extension e.g. Test.bat. Once the saveAs window appears, please enter Test.bat in the FileName textbox and in SaveAs Type dropdown, please select All Files so that it takes the .bat extension.
3) Now whenever you will open the batch file it will trigger your process.
Note Please make sure your runtime resource is connected to the app server and you are able to see your runtime resource running otherwise the above batch file won't work

Command Line Installation with SCCM 2012

I have a few applications that I am trying to deploy with SCCM 2012 but the installations are failing through the application catalog. So what I have for the deployment type is a script installer. I have "cmd.exe" (Without quotations) in the Installation program field and "Installer.bat" in the installation start in field.
When I look at the ccmcache folder, all the contents over that application are there but the following error displays the Software Center:
0x8007010B(-217024629)
I have done some reading online and the "10B" is a common command line error for invalid directory. I have tested the batch file when hard coding a path but my question is, how can I edit the batch file or SCCM to pull from the CCMCache path where the files are downloaded to on the local client? Currently the Batch File is simply:
#echo off
ApplicationName.exe
Do I need to edit the file to cd into the CCMCache folder where the files are placed? How can I get the batch file to run the executable that is downloaded to the CCMCache folder?
Thank You!
You need to have the full path to the installation in your script
#echo Off
\\path to .exe
The way the command is written will not be able to find the .exe file. You need to add the full unc path to the .exe into your .cmd file. You should have your installation .exe and .cmd file in the same location on the distribution share
Recommended Solution:
Before starting, since you are only launching an exe with your batch file, I would recommend just using your ApplicationName.exe as your command line parameter in SCCM instead of using a batch. This will eliminate the need to engineer any further.
Modifying the existing solution to work:
If you do still want to use a batch file, keep a few things in mind. The syntax you are using to launch the batch file will not work. I would recommend just using the batch file name "installer.bat" as your command line. If you still want to preface the batch with the cmd.exe, you absolutely need to use the /c switch with it
cmd.exe /c installer.bat
If you don't use /c the console host will only open to a promopt and not execute your batch.
This is not an ideal solution though because using "cmd.exe /c" will set your working directory to the location of cmd.exe (ie "C:\windows\system32") and since your content is staged in ccmcache, you will need to specify its location within your batch. To do this, you would use %~dp0 variable which gives you the directory the current batch is running from. This means modifying your batch to read
#echo off
%~dp0ApplicationName.exe

How to use the dot command (.) in my custom command?

I have written a simple batch file mycmd.bat, to open up explorer. I've added the file in the appropriate path. The content of the file is:
explorer
Now I want to use this bat file to open up any directory. So I navigate to the required directory and run my command:
D:\MyFolder>mycmd .
If I have Visual Studio Code installed then if I wanted to open the directory with VS Code then I'd do:
D:\MyFolder>code .
I want similar approach for my custom command.
Now if I run D:\MyFolder>mycmd . it opens up my Libraries folder. Not the folder I am at right now which is D:\MyFolder.
(For me Libraries is the default folder that opens up when I open explorer by clicking the yellow explorer icon at my taskbar.)
You do not need a batch-file for that. Navigating to the directory and typing (into the commandline I assume you to take to navigate) explorer . opens the explorer view of that folder.
An alternative if you are running this in a batch-file you could also use explorer %cd%. The environmental variable cd stores the path of the current callers (Thanks for the correction by WasteD!) directory.
So it uses the value from where you called the file i.e. the directory you are currently in or after you used cd anotherFolder it will use this new path.
To change your "custom command" you could also add "%~1" to the same line explorer currently stands in:
explorer "%~1"
This will take the first argument of your batch-file -> in this case . without potential surreounding quotes and add it after the explorer command as first argument.

How do i combine a bat file with an MSI?

I have an MSI Installer that I created in WIX that i would like to be run from a .bat file, but I need it to be in one MSI file. How would i do that? for example i have a .bat file that does this
MD C:\TEMP\BATS
START /W msiexec /i Installer.msi /l*v C:\TEMP\BATS\INSTALLERLOG.txt
SLEEP 5
DEL C:\TEMP\INSTALLERLOG.txt
RD C:\TEMP
I want to create an .msi or .exe file that contains both the .bat file and the .msi file and will unpackage the two files and run the .bat file. Is this possible? I am doing it just to force Logging that will be displayed to the user through a custom action if the installer fails some how.
It sounds like you are caught by the technical details of the counterparts of your solution. Try to forget what you have for a moment and think of what you actually need.
As far as I can tell from your code snippet, the bat file creates a temporary directory, runs MSI package installation with verbose logging option, and then deletes the generated log file... Sounds a bit of no sense to me...
This is whatI would do in your case:
Get rid of that bat file
Ask yourself what its purpose is. If it is there to modify the target system, then revise this logic and move it to your MSI package following all the best practices you can find. If it has to to some service work, e.g. prepare parameters for the main MSI package, then consider authoring a bootstrapper.
You can try IExpress to generate a single EXE file. Another solution would be to use a self-extracting EXE archive which can launch a specific file when it finishes extracting.

inno setup - batch change InstallDircation

I am trying to run a batch file right before the installation begins,
Just after the user chooses the component he wants to install.
fortunately, I don't build it from basic, I have a ready iss file which depending on the component runs a batch file that you have created already.
In the batch file I trying to change the directory of the installer to specific one.
I tried to do so as I set INSTALLBINDIR and appfolder to my directory but it haven't work.
Do you know the variables I need to set so the installer will install where I want?
Edit:
I found out that I don't need to use a batch file.
To fixed a position to file you just need to put your Directory in the DestDir in [files]
You can't change the install directory directly from a sub script/program run from the installer.
You will need to set the WizardForm.DirEdit.Text in [Code] to the new value.
Alternatively, you can use a {code:...} constant for the DefaultDirName directive to get a suitable value to start with.

Resources