通过批处理脚本启动 Windows 可执行文件,exe 不在程序文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2405666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
starting a windows executable via batch script, exe not in Program Files
提问by Anthony
This is probably batch scripting 101, but I can't find any clear explanation/documentation on why this is happening or if my workaround is actually the solution. So basically any terminology or links to good sources is really appreciated.
这可能是批处理脚本 101,但我找不到任何关于为什么会发生这种情况的明确解释/文档,或者我的解决方法是否实际上是解决方案。所以基本上任何术语或良好来源的链接都非常受欢迎。
So I have a program I want to execute via batch script (along with several other programs). It's the only one where the exe
is not in a Program Files
folder. I can get it to start like this:
所以我有一个我想通过批处理脚本执行的程序(以及其他几个程序)。这是唯一一个exe
不在Program Files
文件夹中的文件夹。我可以让它像这样开始:
C:\WeirdProgram\WeirdProgramModule\weirdmodule.exe
But I get an error along the lines of:
但我收到以下错误:
Run-time Error '3024':
Could not find file
C:\Users\MyUserName\Desktop\ModuleSettings.mdb
So it seems that the program is looking for its settings files from the same location that the batch script starts up. Given that I finally got everything to work by doing the following:
因此,该程序似乎正在从批处理脚本启动的同一位置查找其设置文件。鉴于我最终通过执行以下操作使一切正常工作:
cd C:\WeirdProgram\WeirdProgramModule\
weirdmodule.exe
That works fine, and it's not the end of the world to have to go this route (just one extra line), but I've convinced myself that I'm doing something wrong based on lack of basic understanding.
这很好用,而且必须走这条路线并不是世界末日(只是一条额外的路线),但我已经说服自己,由于缺乏基本的理解,我做错了什么。
Anybody know or can point me to why it works this way?
任何人都知道或可以指出为什么它会以这种方式工作?
Oh, and doing the following:
哦,并执行以下操作:
start "C:\WeirdProgram\WeirdProgramModule\weirdmodule.exe"
doesn't do anything at all.
根本不做任何事情。
Thanks,
谢谢,
回答by Phil Rykoff
you are doing it perfectly :-)
你做得很完美:-)
the executable is probably looking for this file in the "current working directory", which is being set, when you "cd" to it before.
当您之前“cd”到它时,可执行文件可能正在“当前工作目录”中查找该文件,该目录正在设置中。
you can set your working directory manually by creating a shortcut to your batch file; right click; properties.
您可以通过创建批处理文件的快捷方式来手动设置工作目录;右键点击; 特性。
edit:
编辑:
you can also set your current working directory using the start
command:
您还可以使用以下start
命令设置当前工作目录:
start "Title" /D "C:\WeirdProgram\WeirdProgramModule\" "weirdmodule.exe"
edit:
编辑:
If you like to pass params, just add them to the executable filename as you would in a regular shortcut:
如果您想传递参数,只需像在常规快捷方式中一样将它们添加到可执行文件名中:
start "Title" /D "C:\WeirdProgram\WeirdProgramModule\" "weirdmodule.exe" "param1 param2"
or
或者
start "Title" /D "C:\WeirdProgram\WeirdProgramModule\" "weirdmodule.exe param1 param2"
For reference, the syntax is described here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx?mfr=true.
作为参考,此处描述了语法:http: //www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx?mfr=true。
回答by Michael Burr
What's happening is that weirdmodule.exe
is looking in the "current directory" for the .mdb
file. You might be able to tell it where to find the .mdb
file through a command line parameter or some other configuration method (registry or .ini file maybe). How you'd specify the location is entirely up to the weirdmodule.exe
program, though.
发生的事情是weirdmodule.exe
在“当前目录”中查找.mdb
文件。您也许可以.mdb
通过命令行参数或其他一些配置方法(可能是注册表或 .ini 文件)告诉它在哪里可以找到文件。但是,您如何指定位置完全取决于weirdmodule.exe
程序。
Other than that, your current workaround is probably what you're stuck with.
除此之外,您当前的解决方法可能是您遇到的问题。
As far as your problem with using start.exe
... the start.exe
program has the very, very odd behavior (bizarre behavior in my opinion) of treating the first parameter as the 'title' to put in the window if (and only if) the first parameter is in quotes. So you have a couple of options:
至于你使用的问题start.exe
......该start.exe
程序具有非常非常奇怪的行为(在我看来是奇怪的行为)将第一个参数视为“标题”,如果(且仅当)第一个参数是引号。所以你有几个选择:
Don't use quotes to specify the program. This works for you because you don't need quotes (there aren't any spaces or other special characters in the path that would require quoting it):
start C:\WeirdProgram\WeirdProgramModule\weirdmodule.exe
Give an empty (or some other string) title as the first parameter. This is something you'd have to do if your path required quotes:
start "" "C:\WeirdProgram\WeirdProgramModule\weirdmodule.exe"
不要使用引号来指定程序。这对您有用,因为您不需要引号(路径中没有任何空格或其他特殊字符需要引用它):
start C:\WeirdProgram\WeirdProgramModule\weirdmodule.exe
给出一个空(或其他字符串)标题作为第一个参数。如果您的路径需要引号,则这是您必须执行的操作:
start "" "C:\WeirdProgram\WeirdProgramModule\weirdmodule.exe"