简体   繁体   English

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

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

I'm working with installshield and have a group of batch files that I want to run as part of the install process.我正在使用 installshield 并且有一组批处理文件,我想在安装过程中运行这些文件。 Instead of executing each batch file from installshield I want to create one batch file that executes all of the batch files.我想创建一个执行所有批处理文件的批处理文件,而不是从 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?如何在不使用调用批处理文件路径的情况下从另一个调用一个批处理文件?

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.将找到主批处理文件,因为您提供了正确的相对路径,但所有其他相对路径将从当前工作目录的角度来看,而不是从包含主批处理文件的目录的角度来看。

%~dp0 is your friend, it yields the drive letter and path to the batch file containing that character sequence. %~dp0是你的朋友,它产生包含该字符序列的批处理文件的驱动器号和路径。 Use it as a basis for relative paths and your batch files will work no matter who calls them from where.使用它作为相对路径的基础,无论谁从哪里调用它们,您的批处理文件都将起作用。

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?请参阅%~dp0 是什么意思,它是如何工作的? for more explanations on %~dp0有关%~dp0更多解释

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.如果这不起作用,请尝试在您的路径中使用 C:// 前缀。 That might/might not work.那可能/可能不起作用。 Good Luck!祝你好运!

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. M 面临同样的问题,我找到了这样的解决方案 - 右键单击​​您的任务而不是属性。 In properties click on General tab and then click on 'User Group or User' and select appropriate user.在属性中单击常规选项卡,然后单击“用户组或用户”并选择适当的用户。

Or create a another bat file to call your bat file and schedule that file.或者创建另一个 bat 文件来调用您的 bat 文件并安排该文件。 you can create the bat file like this -你可以像这样创建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 扩展名保存这个文件。

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

if your bat file is correct, try cmd command as below and hit enter(tried in windows 10):如果您的 bat 文件正确,请尝试如下 cmd 命令并按 Enter(在 Windows 10 中尝试):

"\\h2.bat" “\\h2.bat”

eg: "C:\\Users..\\bin\\h2.bat"例如:“C:\\Users..\\bin\\h2.bat”

I tried :我试过了 :

pushd h2\\bin\\推送 h2\\bin\\

call h2.bat调用 h2.bat

=> It 's okay. => 没关系。

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

相关问题 从另一个批处理文件在路径中使用空格调用Win Batch文件 - Calling Win Batch file with Whitespace in Path from another Batch file 从Windows批处理文件中读取文件并调用另一个.bat - Reading a file and calling another .bat from within a Windows batch file 批处理文件将文件夹从文本文件复制到另一个目录 - Batch File to copy folders from a text file to another directory VBS文件从批处理文件中的其他目录运行时冻结 - VBS file Freezes when run from a different directory in a Batch file 调用另一个批处理文件的调用批处理文件失败 - calling batch file that calls yet another batch file fails 从另一个批处理文件调用该批处理文件时,批处理文件中的复制命令未得到执行,但是当我双击时,该命令正在执行 - copy command in batch file is not getting executed when calling the batch file from another batch file, but is getting executed when i double click 批处理文件可将文件从一个目录复制到另一个目录 - Batch file to copy files from one directory to another Windows Batch,用于调用另一个批处理时的循环和文件属性 - Windows Batch, for loop and file attributes while calling another batch 在python的另一个目录中运行批处理文件 - Running a batch file in another directory in python 批处理文件导致Minecraft使用其他目录? - Batch file causing Minecraft to use a different directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM