Home > other >  Create shortcut to cmd to execute a batch file in Inno Setup
Create shortcut to cmd to execute a batch file in Inno Setup

Time:09-17

I use a batch script start.bat to run my program. I create a shortcut for this purposes as shown below:

[Icons]
Name: "{group}\{#MyAppName}"; Filename: "{app}\start.bat"; \
    IconFilename: "{app}\{#MyAppIcoName}"; Tasks: desktopicon
Name: "{userdesktop}\{#MyAppName}"; Filename: "{app}\start.bat"; \
    IconFilename: "{app}\{#MyAppIcoName}"; Tasks: desktopicon

In order for the user to be allowed to drag the shortcut to a batch file to the taskbar (Windows 10) I have to modify the shortcut path from E:\Soft\MyProgram\start.bat to cmd /c E:\Soft\MyProgram\start.bat. But I can't just change the path to Filename: "cmd /c {app}\start.bat"; it doesn't work. Any ideas?

CodePudding user response:

cmd /c E:\Soft\MyProgram\start.bat

The above command executes cmd.exe program with /c E:\Soft\MyProgram\start.bat as its parameters.

In Inno Setup Icons section, you specify the parameters using Parameters parameter:

[Icons]
Name: "{userdesktop}\{#MyAppName}"; Filename: "cmd"; \
    Parameters: "/c E:\Soft\MyProgram\start.bat"; \
    IconFilename: "{app}\{#MyAppIcoName}"; Tasks: desktopicon
  • Though instead of hard-coding cmd, you should use {cmd} constant.
  • Similarly, I assume that instead of E:\Soft\MyProgram, you should use {app}.
  • Wrap the path to double quotes, in case it contains spaces.
  • In some cases, the batch file might be designed to only work when executed from its directory. For that add WorkingDir parameter.
[Icons]
Name: "{userdesktop}\{#MyAppName}"; Filename: "{cmd}"; \
    Parameters: "/c ""{app}\start.bat"""; WorkingDir: "{app}" \
    IconFilename: "{app}\{#MyAppIcoName}"; Tasks: desktopicon

CodePudding user response:

As Martin suggested https://stackoverflow.com/a/69083021/13116250 here is a solution to modify script:

Name: "{group}\{#MyAppName}"; Filename: {cmd}; Parameters: "/c cd /d ""{app}"" && launch.bat"; IconFilename: "{app}\{#MyAppIcoName}"; Tasks: desktopicon
Name: "{userdesktop}\{#MyAppName}"; Filename: {cmd}; Parameters: "/c cd /d ""{app}"" && launch.bat"; IconFilename: "{app}\{#MyAppIcoName}"; Tasks: desktopicon

You should first cd to the app directory and then run a batch file.

  • Related