繁体   English   中英

%〜dp0不返回包含正在运行的脚本的文件夹

[英]%~dp0 not returning folder containing the running script

在批处理脚本中,与%~dp0挣扎。 问题是在脚本的某些部分, %~dp0返回了应该返回的内容,即包含正在运行的脚本的文件夹的驱动器的完整路径(在这里:'C:\\ Data \\ name \\ App \\ App \\')。 在代码的其他部分, %~dp0仅返回包含脚本的驱动器(此处为'C:\\')。

这是我的脚本(有史以来第一个批处理脚本:D):

@ECHO OFF

ECHO.
:: run the script with the command new-built.bat /all -arch x86 -config release


SETLOCAL EnableDelayedExpansion
SET options=arch version config
SET what_to_build=all lib
:: the three following lines returns what is expected
echo %%~dp0 is returning : %~dp0
echo %%~p0 is returning : %~p0
echo %%~d0 is returning : %~d0
echo The absolute path to this script is : %~dp0%0

:: read what to build, ie all or lib only
FOR %%i in (%what_to_build%) do if %1==/%%i (
SHIFT
GOTO %%i
)


::the following sections (:readoptions and :optlp) work together to collect the options entered in the command, used to launch this script. It saves the values the two arrays "options" and "what_to_build" array

:readoptions
echo Entered the read options label
rem -- initialises variables arch, version and config with empty string
FOR %%i IN (%options% badoptions) DO (SET %%i=)
:optlp
echo Entered the read options loop
SET _parm1=%1
IF NOT DEFINED _parm1 GOTO :END
IF DEFINED _parm1 FOR %%i IN (%options%) DO IF .%_parm1%==.-%%i SET %%i=%2
IF DEFINED %%i shift&shift&(SET _parm1=)
IF DEFINED _parm1 SET badoptions=%badoptions% %1&SHIFT
GOTO :optlp

:all
echo Entered the all label ...
CALL :readoptions %*
echo About to build complete app for x86 in release config
set CYGWIN_DIR="%~dp0" 
:: I want this line to return "C:\Data\name\App\App\" without quotes, but it's returning "C:\"
echo Cygwin dir (not returning what I want): %CYGWIN_DIR%
rem build lib
call %~dp0\build_scripts\build_lib_x86.bat
rem build the Visual Studio Solution
start "Build App x86 - release config" /W "%~dp0%build_scripts\build_app_x86_release.bat"


goto :end

:END
echo Program is done
endlocal

这是我得到的跟踪:

%~dp0 is returning : C:\Data\name\App\App\
%~p0 is returning : \Data\name\App\App\
%~d0 is returning : C:
The absolute path to this script is : C:\Data\name\App\App\new-build.bat
Entered the all label ...
Entered the read options label
Entered the read options loop
Entered the read options loop
Entered the read options loop
Entered the read options loop
Entered the read options loop
Entered the read options loop
Program is done
About to build complete app for x86 in release config
Cygwin dir (not returning what I want) : "C:\"
The system cannot find the path specified.
Program is done

我一定很想念某些事情。 任何帮助将不胜感激。

最好

噢-麻烦大了!

@ECHO OFF

ECHO.
:: run the script with the command new-built.bat /all -arch x86 -config release


SETLOCAL EnableDelayedExpansion
SET options=arch version config
SET what_to_build=all lib
:: the three following lines returns what is expected
echo %%~dp0 is returning : %~dp0
echo %%~p0 is returning : %~p0
echo %%~d0 is returning : %~d0
echo The absolute path to this script is : %~dp0%0

给定new-built.bat / all -arch x86 -config版本,则%1/all

:: read what to build, ie all or lib only
FOR %%i in (%what_to_build%) do if %1==/%%i (
SHIFT
GOTO %%i
)

所以,由于%1/all ,这将以%1=-arch %2=x86 %3=-config %4=release BUT %*=/all -arch x86 -config release到达:all

HAD %1/lib然后同上,但将到达:lib

但是对于%1的任何其他值,处理将直接从这里开始直到下一条语句。


...所以我们得出:all%1=-arch %2=x86 %3=-config %4=release BUT %*=/all -arch x86 -config release

 :all echo Entered the all label ... CALL :readoptions %* 

由于:readoptions用%*调用,每个原始参数都传递给:readoptions BUT而不是goto :EOF and hence the message您已将其修改为'goto END and hence the message Program done完成is displayed - and processing returns to the statement following the CALL`

 echo About to build complete app for x86 in release config set CYGWIN_DIR="%~dp0" :: I want this line to return "C:\\Data\\name\\App\\App\\" without quotes, but it's returning "C:\\" echo Cygwin dir (not returning what I want): %CYGWIN_DIR% rem build lib call %~dp0\\build_scripts\\build_lib_x86.bat rem build the Visual Studio Solution start "Build App x86 - release config" /W "%~dp0%build_scripts\\build_app_x86_release.bat" 


让我们重新开始...

我相信您实际上希望为CYGWIN_DIR分配CYGWIN目录名称的值。 我们可能可以解决这个问题,但是如果将它永久设置在您的环境中,那就更好了。 简单的方法是:

 SETX CYGWIN_DIR "wherever your cygwin dir is" SETX CYGWIN_DIR "wherever your cygwin dir is" /M 

(第一个为当前LOGON会话中的NEW CMD实例分配值,第二个为以后的登录会话分配值。IOW,执行第二个并重新启动...如果需要,可以使用SETX CYGWIN_DIR“”删除条目)

 @ECHO OFF ECHO. :: run the script with the command new-built.bat /all -arch x86 -config release SETLOCAL EnableDelayedExpansion SET options=arch version config SET SLASHOPTS=ALL LIB (SET SWITCHES=) :: %CD% is the magic pseudovariable for the current directory. :: quote it if you wish SET CYGWIN_DIR=%CD% :: The above line of course only if you want CYGWIN_DIR to be set to the current directory CALL :READOPTIONS %* :: NOTE THAT %badoptions% now contains a list of the items NOT :: involved in the OPTIONS, SWITCHES or SLASHOPTIONS :: read what to build, ie all or lib only :: ONE WAY... IF DEFINED ALL goto ALL IF DEFINED LIB goto LIB :: ANOTHER WAY to do the same thing for %%i in (%SLASHOPTS%) DO if defined %%i goto %%i :: EEK! Neither ALL not LIB was specified - what to do? echo EEK! Neither ALL not LIB was specified - exiting! goto :EOF :all echo Entered the all label ... echo About to build complete app for x86 in release config echo Cygwin dir (not returning what I want): %CYGWIN_DIR% rem build lib :: I'll assume %CYGWIN_DIR%\\build_scripts is where you're keeping this batch call %CYGWIN_DIR%\\build_scripts\\build_lib_x86.bat rem build the Visual Studio Solution start "Build App x86 - release config" /W "%CYGWIN_DIR%\\build_scripts\\build_app_x86_release.bat" goto end :LIB echo LIB processing not yet defined goto end :END echo Program is done endlocal :readoptions echo Entered the read options label rem -- initialises variables arch, version and config with empty string :readoptions FOR %%i IN (%options% %slashopts% %switches% badoptions) DO (SET %%i=) :optlp SET _parm1=%1 IF NOT DEFINED _parm1 GOTO :EOF FOR %%i IN (%switches%) DO IF /i %_parm1%==-%%i SET %%i=Y&(SET _parm1=) IF NOT DEFINED _parm1 shift&GOTO :optlp FOR %%i IN (%slashopts%) DO IF /i %_parm1%==/%%i SET %%i=Y&(SET _parm1=) IF NOT DEFINED _parm1 shift&GOTO :optlp FOR %%i IN (%options%) DO IF /i %_parm1%==-%%i ( SET %%i=%2 IF DEFINED %%i shift&shift&(SET _parm1=) ) IF DEFINED _parm1 SET badoptions=%badoptions% %1&SHIFT GOTO :optlp 

请注意,我稍微修改了:readoptions,以便您可以设置-option anoptionvalue-switch/slashoption

还要注意,您可以将这一节从:readoptions一直传输到一个独立的批处理文件,例如readoptions.bat然后将该文件放在path上的任何位置(请参见提示中的set path ),然后可以执行任何批处理

 CALL READOPTIONS %* 

读取您在变量options分别指定的options / switchs / slashoptions optionsswitchesslashopts ,并且badoptions将包含所有剩余参数...

(关于冒号的说明:call:routine parameterlist调用带有'private'参数parameterlist的INTERNAL例程调用例程parameterlist调用带有'private'参数parameterlist的EXTERNAL例程;即[batch]文件'routine from somewhere on the `

 goto :eof 

是一种用于访问文件物理结尾的内置工具。 这将终止CALL并返回到OR后面的行,或者完全退出批处理。

暂无
暂无

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

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