简体   繁体   English

从其他目录调用时,此批处理脚本返回不正确的值

[英]This batch script returns incorrect value when invoked from a different directory

This batch script returns incorrect value when invoked from a different directory. 从另一个目录中调用时,此批处理脚本返回不正确的值。 I am hoping someone can help me fix this so that it always determines the correct APP_HOME directory. 我希望有人可以帮助我解决此问题,以便它始终确定正确的APP_HOME目录。

The script is located in a position like: 脚本位于类似的位置:

C:\Temp\MyApplication\bin\runner.bat

And, I want to execute it from: 而且,我想从以下位置执行它:

C:\Temp\OutsideDir\runApp.bat

When I try this, I get 'OutsideDir' but I want it to get 'bin' : 当我尝试这个时,我得到'OutsideDir'但我希望它得到'bin':

C:\Temp\OutsideDir>C:\Temp\MyApplication\bin\runner.bat
Current directory is: C:\Temp\MyApplication\bin
This folder name: OutsideDir
Function arg must match actual folder name.
This script may not be running from the expected folder.
There was an error.

And here is the script: 这是脚本:

@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION

TITLE Script %~n0%~x0 running from %~dp0

CALL :getparentfolder bin

ECHO My app home is: %APP_HOME%

GOTO :END

:: function to get parent folder name or APP_HOME of scripts currrent folder
:: function requires current folders name as an arg or it will fail to run
:getparentfolder dirName
SET "BIN_HOME=%~dp0"
IF "%BIN_HOME:~-1%"=="\" SET "BIN_HOME=%BIN_HOME:~0,-1%"
ECHO Current directory is: %BIN_HOME%
FOR /F "tokens=1,* delims=^\" %%I IN ("%BIN_HOME%") DO (
  SET THISFOLDER=%%~nxI
)
ECHO This folder name: %THISFOLDER%
IF "%~1"=="%THISFOLDER%" (
  SET APP_HOME=!BIN_HOME:\%THISFOLDER%=!
  ECHO APP_HOME: %APP_HOME%
) ELSE (
  ECHO Function arg must match actual expected folder name.
  ECHO This script may not be running from the expected folder.
  GOTO :ERROR
)
EXIT /B 0

:ERROR
ECHO There was an error.
PING.exe -n 10 -w 1 127.0.0.1>nul
:END
PING.exe -n 10 -w 1 127.0.0.1>nul
ECHO The script %~n0%~x0 is finished.

The scripts own home directory is %~dp0. 脚本自己的主目录为%〜dp0。

Your code is getting the name of the current working directory, hence it's failing because it doesn't match the argument you supply (bin). 您的代码正在获取当前工作目录的名称,因此失败,因为它与您提供的参数(bin)不匹配。

SET "BIN_HOME=%~dp0" 设置“ BIN_HOME =%〜dp0”

Sets BIN_HOME to the path of the directory containing the script (in your case c:\\temp\\myapplication\\bin) 将BIN_HOME设置为包含脚本的目录的路径(在您的情况下为c:\\ temp \\ myapplication \\ bin)

FOR /F "tokens=1,* delims=^\" %%I IN ("%BIN_HOME%") DO (
   SET THISFOLDER=%%~nxI
)

Extracts tokens from the BIN_HOME variable using \\ as a delimiter. 使用\\作为定界符从BIN_HOME变量中提取标记。 You have specified you want a maximum of two tokens (tokens=1,*), so these will be allocated to the variables %%I and %%J as follows (assuming %BIN_HOME% is c:\\temp\\myapplication\\bin: 您指定了最多两个令牌(令牌= 1,*),因此这些令牌将分配给变量%% I和%% J,如下所示(假设%BIN_HOME%为c:\\ temp \\ myapplication \\ bin:

%%I = C:   (everything up to the first backslash)
%%J = temp\myapplication\bin (the remainder of the string

Then attempts to get the file name part of th value %%I (%%~nxI). 然后尝试获取值%% I(%%〜nxI)的文件名部分。 This gets the name of the current working directory (the path "C:" refers to the current working directory). 这将获取当前工作目录的名称(路径“ C:”是指当前工作目录)。

I'm not sure exactly what you're trying to achieve, why can't you simply do something like: 我不确定您要达到的目标,为什么不能简单地执行以下操作:

SET APP_HOME=%~dp0

UPDATE UPDATE

I can't do that because %~dp0 refers to the current directory and not the parent of the current directory 我无法执行此操作,因为%〜dp0指向当前目录,而不是当前目录的父目录

If you want the parent of the script's directory, you can do it with something like: 如果您想要脚本目录的父目录,则可以使用以下方法:

SET MYDIR=%~dp0
echo My directory is %MYDIR%

CALL :GETPARENT PARENT "%MYDIR:~0,-1"

echo Parent is %PARENT%

GOTO :EOF

:GETPARENT
SET %1=%~dp2
GOTO :EOF

try this: 尝试这个:

@echo off&setlocal
SET "BIN_HOME=%~dp0"
IF "%BIN_HOME:~-1%"=="\" SET "BIN_HOME=%BIN_HOME:~0,-1%"
ECHO Current directory is: %BIN_HOME%
FOR %%I IN ("%BIN_HOME%") DO (
  SET THISFOLDER=%%~nxI
)
ECHO This folder name: %THISFOLDER%

output is: 输出为:

C:\TEMP\OutsideDir>C:\Temp\MyApplication\bin\runner.bat
Current directory is: C:\TEMP\MyApplication\bin
This folder name: bin
FOR /F "tokens=1,* delims=\" %%I IN ("%BIN_HOME%") DO (
  SET THISFOLDER=%%~nxJ
)
SET thisfolder

FOR /F "delims=" %%I IN ("%BIN_HOME%") DO (
  IF /i "%%~nxI"=="%~1" (SET APP_HOME=%%~dpI) ELSE (SET APP_HOME=)
)
IF NOT DEFINED app_home GOTO error
SET app_home=%app_home:~0,-1%
SET app

The caret is not required. 不需要插入号。 Note %%~nx J 注意%%〜nx J

Personally, I'd employ the second substitute method... 就个人而言,我会采用第二种替代方法...

and setting your title is easier done with Script %~nx0 ... 使用Script %~nx0 ...可以更轻松地设置标题

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

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