windows 从不同目录中的另一个批处理文件调用批处理文件 - 找不到资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16718442/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:32:12  来源:igfitidea点击:

Calling a batch file from another batch file in different directory - resources not found

windowsbatch-file

提问by jonsinfinity

I'm working with installshield and have a group of batch files that I want to run as part of the install process. Instead of executing each batch file from installshield I want to create one batch file that executes all of the batch files.

我正在使用 installshield 并且有一组批处理文件,我想在安装过程中运行这些文件。我想创建一个执行所有批处理文件的批处理文件,而不是从 installshield 执行每个批处理文件。

The issue I have is that the calling batch file sits two directories up from the others. When the batch file tries to call the others they fail to run because they can not find the resources that they need. It seems that when they are executed from the batch file two directories up they are for some reason using the relative path of the calling batch file. Is my assumption correct?

我遇到的问题是调用批处理文件位于两个目录之上。当批处理文件尝试调用其他文件时,它们无法运行,因为它们找不到所需的资源。似乎当它们从批处理文件向上两个目录执行时,它们出于某种原因使用调用批处理文件的相对路径。我的假设正确吗?

One of the batch files that I am calling is a batch file to star an h2 database the call looks like this:

我正在调用的批处理文件之一是用于启动 h2 数据库的批处理文件,调用如下所示:

call h2\bin\h2.bat

If I go to the /h2/bin directory in a command prompt the h2.bat runs fine but once I run it from the calling batch file this is the error that I get.

如果我在命令提示符下转到 /h2/bin 目录,h2.bat 运行良好,但是一旦我从调用批处理文件中运行它,这就是我得到的错误。

Error: Could not find or load main class org.h2.tools.Console

错误:无法找到或加载主类 org.h2.tools.Console

How do I call one batch file from another without using the calling batch files path?

如何在不使用调用批处理文件路径的情况下从另一个调用一个批处理文件?

回答by Matth?us Brandl

Explanation

解释

It seems that when they are executed from the batch file two directories up they are for some reason using the relative path of the calling batch file. Is my assumption correct?

似乎当它们从批处理文件向上两个目录执行时,它们出于某种原因使用调用批处理文件的相对路径。我的假设正确吗?

Yes your assumption is correct. Calling a batch file will not change the current working directory. The main batch file will be found because you are providing the correct relative path, but all the other relative paths will be seen from the perspective of your current working directory, not from the directory that contains the main batch file.

是的,你的假设是正确的。调用批处理文件不会更改当前工作目录。将找到主批处理文件,因为您提供了正确的相对路径,但所有其他相对路径将从当前工作目录的角度来看,而不是从包含主批处理文件的目录的角度来看。

%~dp0is your friend, it yields the drive letter and path to the batch file containing that character sequence. Use it as a basis for relative paths and your batch files will work no matter who calls them from where.

%~dp0是您的朋友,它会生成包含该字符序列的批处理文件的驱动器号和路径。使用它作为相对路径的基础,无论谁从哪里调用它们,您的批处理文件都将起作用。

Example:

例子:

Fictitious h2.bat that won't work:

无效的虚构 h2.bat:

@echo off
h2.exe start

Working h2.bat:

工作 h2.bat:

@echo off
"%~dp0\h2.exe" start

See What does %~dp0 mean, and how does it work?for more explanations on %~dp0

请参阅%~dp0 是什么意思,它是如何工作的?有关更多解释%~dp0

回答by hornzach

Try setting the directory:

尝试设置目录:

cd ht\bin\
call h2.bat
cd %HOMEPATH%
REM  just reset to where ever you were before.

If that doesn't work, try using the C:// prefix in your path. That might/might not work. Good Luck!

如果这不起作用,请尝试在您的路径中使用 C:// 前缀。那可能/可能不起作用。祝你好运!

回答by Brijesh Rana

It might be because you don't have permission. M facing the same problem and i found the solution like this - Right click on your task than properties. In properties click on General tab and then click on 'User Group or User' and select appropriate user.

可能是因为你没有权限。M 面临同样的问题,我找到了这样的解决方案 - 右键单击​​您的任务而不是属性。在属性中单击常规选项卡,然后单击“用户组或用户”并选择适当的用户。

Or create a another bat file to call your bat file and schedule that file. you can create the bat file like this -

或者创建另一个 bat 文件来调用您的 bat 文件并安排该文件。你可以像这样创建bat文件 -

open Notepad and give your original bat file path and then call bat file with name like -

打开记事本并提供原始 bat 文件路径,然后使用名称调用 bat 文件,例如 -

D:

乙:

cd "E:/ABC/FirstJob/main/"

cd "E:/ABC/FirstJob/main/"

call main_run.bat

调用 main_run.bat

Now save this file with .bat extension.

现在用 .bat 扩展名保存这个文件。

回答by Muhammad Imran Tariq

Suppose current .bat file is running in C drive and you want to run .bat file placed in D: directory then in first .bat write.

假设当前 .bat 文件在 C 驱动器中运行,并且您要运行放置在 D: 目录中的 .bat 文件,然后在第一个 .bat 写入。

D:
cd "D:/folder/folder2/"
call batFile.bat

回答by TQ Thái

I tried :

我试过 :

pushd h2\bin\

推送 h2\bin\

call h2.bat

调用 h2.bat

=> It 's okay.

=> 没关系。