简体   繁体   English

使用命令行参数从bat文件运行exe

[英]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. bat安装目录的子目录中有一个bat文件,它将复制文件,然后运行一个程序,然后运行另一个程序。 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 问题是我需要将命令行参数添加到下采样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. 问题是为了做到这一点(根据我目前对bat文件的了解,这是有限的)是创建快捷方式并以通常的Windows快捷方式输入命令行参数。 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 .... 好吧,到目前为止,我已经设法创建了一个bat文件,该文件使用此脚本在同一目录中创建了一个快捷方式。

@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 . cmd行参数以%0%1%2的形式提供给您。 With %~d0 in your current code, you are pulling the letter drive from argument %0 . 在当前代码中使用%~d0 ,您将从参数%0中拉出letter drive

This MS page can probably help you . 该MS页面可能会为您提供帮助

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. 正如您在链接中看到的那样,这是从WindowsXP文档中提取的,它可以解释为什么链接充其量是片刻的。

https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true 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. 并且您希望从bat文件启动程序。 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 让我们从一些基础知识开始...为了参考起见,我们将说我今天使用的程序称为GetToWork.exe

The program I am using GetToWork.exe offers me the ability to start it many different ways. 我正在使用GetToWork.exe的程序使我能够以许多不同的方式启动它。

  • I can double click the exe which launches the program in a form 我可以双击以某种形式启动程序的exe
  • I can call the exe from CMD line and pass it parameters so it can do automated things without me having to do anything. 我可以从CMD行调用exe并传递参数,这样它就可以执行自动化操作,而无需执行任何操作。
    1. I can pass the parameter --DontAskMeAnything if I want the program to do everything on its own. 如果我希望程序自己执行所有操作,则可以传递参数--DontAskMeAnything
    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. 如果我希望程序在对将被视为新文件的内容进行处理之前询问我,则可以传递参数--AskMeForNewFile
    3. I can pass it the parameter --DoItThisWayInstead if I want the parameter to do it a different way. 如果希望参数以其他方式执行此操作,则可以传递参数--DoItThisWayInstead

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: 如果我想触发--DontAskMeAnything我会编写以下命令:

    GetToWork.exe --DontAskMeAnything GetToWork.exe --DontAskMeAnything

  • If I wanted to trigger --AskMeForNewFile , then I would write this command: 如果我想触发--AskMeForNewFile ,那么我将编写以下命令:

    GetToWork.exe --AskMeForNewFile GetToWork.exe --AskMeForNewFile

  • If GetToWork.exe allows multiple parameters, I would maybe write something like this: 如果GetToWork.exe允许多个参数,则我可能会编写如下内容:

    GetToWork.exe --AskMeForNewFile --DoItThisWayInstead GetToWork.exe --AskMeForNewFile --DoItThisWayInstead

Now... back to you and your program. 现在...回到和您的程序。 You are using a program called 10th-f-Downsampling.exe . 您正在使用一个名为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 /? 有时只用/?调用exe /? 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 在这里,您正在调用的程序是xcopy
  • The first parameter you are passing to xcopy is %~d0\\dir-files\\record\\*.* 您传递给xcopy的第一个参数是%~d0\\dir-files\\record\\*.*
  • The second parameter you are passing is "%~dp0input" 您传递的第二个参数是"%~dp0input"
  • The third parameter you are passing is /Y 您传递的第三个参数是/Y

If you were to go into command prompt and run this command xcopy /? 如果要进入命令提示符并运行此命令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. 尽管在文档中提供了指导,但务必遵循指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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