38

I need a batch file which will do the following:

1. Open CMD and navigate to a location C:/Users/...../program.exe
2. Run the program.exe with an additional command to point it to a config file:
e.g. "program.exe C:/Users/..../configFile.bgi"

How can I do this?

I tried this but with no luck:

start "C:\Users\Ben\Desktop\BGInfo\bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi"
pause

Update

I've used the solution provided by Ganesh (below) and came up with this:

cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi

I've tested it on a local machine (changing the directories) but on the server (with the directory above) it does not work...

The folder directory with batch file: enter image description here

The error enter image description here

ben_dchost
  • 875
  • 4
  • 13
  • 24

3 Answers3

33

in batch file abc.bat

cd c:\user\ben_dchost\documents\
executible.exe -flag1 -flag2 -flag3 

I am assuming that your executible.exe is present in c:\user\ben_dchost\documents\ I am also assuming that the parameters it takes are -flag1 -flag2 -flag3

Edited:

For the command you say you want to execute, do:

cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe dc_bginfo.bgi
pause

Hope this helps

  • Just tested it on a remote server and it doesn't appear to like the script (I'm in process of editing my question so you can see what I've done) – ben_dchost Jan 12 '15 at 12:08
  • if you have spaces in your path\filename, you have to enclose it in double quotes. Else `cd c:\program files\whatever` will try to change to `c:\program` which doesn't exist. – Stephan Jan 12 '15 at 12:21
  • i tried creating a `.bat` file with the contents `cd E:\XXXX\Installers\blender\blender-3.1.0-windows-7\blender-3.1.0-windows-7\`
    `blender.exe`
    but it does not work. but if i manually go into the folder with file explorer i can open the file. apparently this method does not work on windows 7. But @Mofi method works.
    – Harry McKenzie Jul 16 '22 at 11:09
7

You can use

start "" "%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"

or

start "" /D "%USERPROFILE%\Desktop\BGInfo" bginfo.exe dc_bginfo.bgi

or

"%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"

or

cd /D "%USERPROFILE%\Desktop\BGInfo"
bginfo.exe dc_bginfo.bgi

Help on commands start and cd is output by executing in a command prompt window help start or start /? and help cd or cd /?.

But I do not understand why you need a batch file at all for starting the application with the additional parameter. Create a shortcut (*.lnk) on your desktop for this application. Then right click on the shortcut, left click on Properties and append after a space character "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi" as parameter.

Mofi
  • 46,139
  • 17
  • 80
  • 143
4

Found another solution for the same. It will be more helpful.

START C:\"Program Files (x86)"\Test\"Test Automation"\finger.exe ConfigFile="C:\Users\PCName\Desktop\Automation\Documents\Validation_ZoneWise_Default.finger.Config"

finger.exe is a parent program that is calling config solution. Note: if your path folder name consists of spaces, then do not forget to add "".

Ganesh Mondal
  • 41
  • 1
  • 2
  • Yes, `"` are necessary as explained by the help output on running `cmd /?` in a command prompt window. It is necessary to enclose a file or folder or any other argument string containing a space or one of these characters ``&()[]{}^=;!'+,`~<>|`` in `"` to be interpreted as one argument string with these characters interpreted literally. But the entire file/folder name must be enclosed in `"` and not just parts of it. So really correct is `"C:\Program Files (x86)\Test\Test Automation\finger.exe"` and the posted file name is of incorrect syntax which `cmd.exe` corrects on execution of `start`. – Mofi Apr 07 '22 at 16:47
  • Learn from `cmd.exe` the correct syntax by using the file name completion of `cmd`. Type in a command prompt window `C:\Pro` and press key __TAB__ and displayed is `"C:\Program Files"`. Press once more key __TAB__ and displayed is `"C:\Program Files (x86)"`. Press again key __TAB__ and displayed is `C:\ProgramData`. Press twice __TAB__ and continue typing after displayed `"C:\Program Files"` with `\Test\Tes` and press key __TAB__. The displayed string changes to `"C:\Program Files (x86)\Test\Test Automation"`. The automatic correction can be seen wonderfully using the file name completion. – Mofi Apr 07 '22 at 16:49
  • Continue typing with `\fin` and press __TAB__ and there is finally displayed `"C:\Program Files (x86)\Test\Test Automation\finger.exe"`. That is the correct specified fully qualified file name according to the Microsoft documentation about [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file). The character `"` is a not allowed character inside a file/folder name as described by Microsoft. This is the reason why `cmd.exe` can detect the syntax error caused by `"` inside a file/folder name string and corrects wrong specified argument string. – Mofi Apr 07 '22 at 16:50
  • But with using the correct syntax of enclosing entire file name in `"`, the command __START__ interprets now `"C:\Program Files (x86)\Test\Test Automation\finger.exe"` as optional console window title and the command line without `""` after __START__ does not work anymore without an explicitly specified empty window title string between the command __START__ and the fully qualified file name of the Windows GUI executable to start. It is a bad concept to make use of an invalid syntax and the automatic syntax correction of `cmd` to get a command line working which is in real of invalid syntax. – Mofi Apr 07 '22 at 16:50