简体   繁体   English

如何正确地从Delphi7打开.bat文件(例如,打开RandomApp.exe)?

[英]How to open a .bat file (eg which opens RandomApp.exe) correctly from Delphi7?

I've searched a lot and I don't quite find the correct answer. 我搜索了很多东西,但找不到正确的答案。

Directory: 目录:

  • Project (This folder contains the delphi code and a folder called "RunThis" 项目(此文件夹包含delphi代码和一个名为“ RunThis”的文件夹

    • RunThis 运行这个

      • RandomApp.exe 随机应用程序
      • Run.bat 运行蝙蝠
      • Config.txt Config.txt

Run.bat code Run.bat代码

start %cd%\RandomApp.exe
pause

Delphi 7 (Executes this code on the click of a button.) Delphi 7(单击按钮即可执行此代码。)

ShellExecute (application.handle, 'open', 'cmd', PChar('cmd.exe /c RunThis\Run.bat'), nil, SW_MAXIMIZE

If I don't add the 'cmd.exe /c' part for the directory, Delphi will open a cmd without any of my code. 如果我没有为目录添加'cmd.exe / c'部分,Delphi将打开一个没有任何代码的cmd。 Similiar to if one had to run cmd.exe .However, if I add the 'cmd.exe /c', then it opens the Run.bat correctly. 类似于必须运行cmd.exe。但是,如果我添加了“ cmd.exe / c”,则它将正确打开Run.bat。

Windows the gives me this error: Windows cannot find 'C:\\Project\\RunThis\\RandomApp.exe' .Make sure you typed the name the name correctly, and then try again. Windows给了我这个错误:Windows找不到'C:\\ Project \\ RunThis \\ RandomApp.exe'。确保您正确输入名称,然后重试。

What's funny about this is that if I just double click on Run.bat, it excutes perfectly. 有趣的是,如果我只要双击Run.bat,它就会完美执行。 However, when I open it through Delphi it gives me this error. 但是,当我通过Delphi打开它时,出现了这个错误。 I thought maybe the Run.bat current address had a problem. 我以为Run.bat当前地址可能有问题。 So Instead of saying: 所以不用说:

start RandomApp.exe

I used the full current directory instead: 我改用完整的当前目录:

start %cd%\RandomApp.exe    

Just to add, when I got Delphi 7 to just open the program directly such as: 补充一下,当我使用Delphi 7直接打开程序时,例如:

ShellExecute(Handle, 'open', PChar('RunThis\RandomApp.exe'),null, null, SW_SHOWNORMAL) ;

The program gave me an error that it couldn't find "Config.txt". 该程序给我一个错误,找不到“ Config.txt”。 RandomApp.exe uses a text file that stores all its settings. RandomApp.exe使用存储其所有设置的文本文件。

Again, if I simply click on Run.bat or RandomApp.exe, they both execute perfectly. 同样,如果我只单击Run.bat或RandomApp.exe,它们都可以完美执行。 The problem is when I try to open it with code. 问题是当我尝试使用代码打开它时。 I have the feeling that it has to do with the directory but I don't know well. 我觉得它与目录有关,但我不太了解。

Thank you very much. 非常感谢你。

PS: Running Windows 10. PS:运行Windows 10。

Don't rely on relative paths, always use absolute paths. 不要依赖相对路径,请始终使用绝对路径。

RandomApp.exe is likely using a relative path to find Config.txt , but when you run it from your app, the current working directory is your project folder, not the RunThis folder. RandomApp.exe可能使用相对路径来查找Config.txt ,但是从应用程序运行时,当前的工作目录是您的项目文件夹,而不是RunThis文件夹。 That is why RandomApp.exe can't find the file. 这就是为什么RandomApp.exe找不到文件的原因。 So pass the correct folder to ShellExecute() , eg: 因此,将正确的文件夹传递给ShellExecute() ,例如:

var
  Folder: string; 
begin
  Folder := ExtractFilePath(Application.ExeName)+'RunThis\' ;
  ShellExecute(Application.Handle, nil, PChar(Folder+'Run.bat'), nil, PChar(Folder), SW_MAXIMIZE);
end;

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

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