简体   繁体   中英

Run exe from bat file with command line arguments

I am building an install package and have a install directory , (obviously could change if user needed/wanted). in a subdir in the root install dir is a bat file that copies files , then runs a program , then runs another program. it currently looks like this.

@echo off
xcopy %~d0\dir-files\record\*.* "%~dp0input" /Y
10th-f-Downsampling.exe start /wait

cd..
start Dashboard.exe 

the problem is that i need to add command line argument to the downsampling exe --max_old_space_size=3000

problem is in order to do this (under my current understanding of bat files which is limited) is to create a shortcut and input the command line argument in the usual windows shortcut way. I can't create a relative shortcut and am not sure how best to do this. any ideas anyone ?

Ok so i have so far managed to create a bat file that creates a shortcut in the same dir using this script ....

@echo off

set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"

echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "ShortcutName.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo oLink.TargetPath = "MyApp.exe" >> %SCRIPT%
echo oLink.WorkingDirectory ="" >> %SCRIPT%
echo oLink.Save >> %SCRIPT%

cscript /nologo %SCRIPT%

del %SCRIPT%

however I am not sure how to include the command line arguments into this file... if someone could give a thought that would be amazing.

the cmd line arguments are available to you in the form %0 , %1 , %2 . With %~d0 in your current code, you are pulling the letter drive from argument %0 .

This MS page can probably help you .

Weirdly enough, the link works if I click it, but does not for some users. Here's the path if you'd like to copy paste it in your preferred browser. As you can see in the link, this is pulled from WindowsXP documentation, that may explain why the link is flaky at best.

https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true

Edit It seems like you want to call a program and pass that program a parameter as you are calling it. And you wish to start the program from a bat file. This is different than how I read your title.

Lets start with some basics... For reference sake, we'll say the program I am using today is called GetToWork.exe

The program I am using GetToWork.exe offers me the ability to start it many different ways.

  • I can double click the exe which launches the program in a form
  • I can call the exe from CMD line and pass it parameters so it can do automated things without me having to do anything.
    1. I can pass the parameter --DontAskMeAnything if I want the program to do everything on its own.
    2. I can pass the parameter --AskMeForNewFile if I want the program to ask me before doing something to what it will consider to be a new file.
    3. I can pass it the parameter --DoItThisWayInstead if I want the parameter to do it a different way.

Now, I'm not the one who decided what parameters I can pass, nor what their result is going to be. In fact, the parameters were defined by the programmer who created the program. When he made the program, he decided that those were going to be the allowed parameters, and the actions related to them were going to be the actions that he decided. The programmer documented the parameters, and now I just want to use them because they make my life easier.

In order to trigger the parameter behaviors, I need to supply the said parameters to the program when I run it.

  • If I wanted to trigger --DontAskMeAnything I would write this command:

    GetToWork.exe --DontAskMeAnything

  • If I wanted to trigger --AskMeForNewFile , then I would write this command:

    GetToWork.exe --AskMeForNewFile

  • If GetToWork.exe allows multiple parameters, I would maybe write something like this:

    GetToWork.exe --AskMeForNewFile --DoItThisWayInstead

Now... back to you and your program. You are using a program called 10th-f-Downsampling.exe . We, I, don't know that program. We, I, don't know what the possible parameters are for that program. In order for you to know what you should pass, you'll have to look up the documentation of that program. Sometimes just calling the exe with a /? is enough to get the parameter list. Again, this is dependant on the programmer who made the program as /? would simply be another parameter allowed by the programmer. And the result of the /? is usually to spit out the available parameters...

Lets break down your current code

xcopy %~d0\dir-files\record\*.* "%~dp0input" /Y
  • Here, the program you are calling is xcopy
  • The first parameter you are passing to xcopy is %~d0\\dir-files\\record\\*.*
  • The second parameter you are passing is "%~dp0input"
  • The third parameter you are passing is /Y

If you were to go into command prompt and run this command xcopy /? , you'd get a chunk of text showing you how to call the program, along with a list of parameters that can be passed. Although parameters are often recognized by name... their name has little meaning because once in the program, the programmer may have coded it so that only parameter position matters, or position and name, or whatever other cocktail of business rules he obliged by. Though it's important to follow the directions if directions are provided in the documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM