1

从其他目录调用此批处理脚本时返回不正确的值。我希望有人可以帮助我解决这个问题,以便它始终确定正确的 APP_HOME 目录。

该脚本位于如下位置:

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

而且,我想从以下位置执行它:

C:\Temp\OutsideDir\runApp.bat

当我尝试这个时,我得到 '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.

这是脚本:

@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.
4

3 回答 3

1

脚本自己的主目录是 %~dp0。

您的代码正在获取当前工作目录的名称,因此它失败了,因为它与您提供的参数 (bin) 不匹配。

SET "BIN_HOME=%~dp0"

将 BIN_HOME 设置为包含脚本的目录的路径(在您的情况下为 c:\temp\myapplication\bin)

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

使用 \ 作为分隔符从 BIN_HOME 变量中提取标记。您已指定最多需要两个令牌 (tokens=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

然后尝试获取 th 值 %%I (%%~nxI) 的文件名部分。这将获取当前工作目录的名称(路径“C:”指的是当前工作目录)。

我不确定您到底要达到什么目标,为什么您不能简单地执行以下操作:

SET APP_HOME=%~dp0

更新

我不能这样做,因为 %~dp0 指的是当前目录而不是当前目录的父目录

如果你想要脚本目录的父目录,你可以这样做:

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
于 2013-05-01T17:24:51.133 回答
1

尝试这个:

@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%

输出是:

C:\TEMP\OutsideDir>C:\Temp\MyApplication\bin\runner.bat
Current directory is: C:\TEMP\MyApplication\bin
This folder name: bin
于 2013-05-01T17:27:07.883 回答
0
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

插入符号不是必需的。注意 %%~nxJ

就个人而言,我会采用第二种替代方法......

并且设置您的标题更容易完成Script %~nx0 ...

于 2013-05-01T17:29:10.677 回答