在批处理脚本上,与%~dp0
斗争。
问题是在我的脚本的某些部分,%~dp0
返回它应该返回的内容,即包含正在运行的脚本的文件夹的驱动器的完整路径(这里:'C:\ Data \ name \应用程序\软件\')。在代码的其他一些部分中,%~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
必定会发生一些事情,我不知道。任何帮助将受到高度赞赏。
最佳
答案 0 :(得分:1)
哦 - 大麻烦!
@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
,因此会:all
%1=-arch %2=x86 %3=-config %4=release
但%*=/all -arch x86 -config release
HAD %1
然后/lib
然后同上,但会到达:lib
但对于%1的任何其他值,处理将直接通过此处收取下一个声明......
<小时/> ......所以我们
:all
%1=-arch %2=x86 %3=-config %4=release
%*=/all -arch x86 -config release
:all
echo Entered the all label ...
CALL :readoptions %*
%*
从以下情况开始:使用goto :EOF
将每个 ORIGINAL 参数传递给:readoptions但不是and hence the message
您已修改这个'转到结束is displayed - and processing returns to the statement following the
程序完成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"
CALL`
SETX CYGWIN_DIR "wherever your cygwin dir is"
SETX CYGWIN_DIR "wherever your cygwin dir is" /M
<小时/> 让我们重新开始......
我相信您实际上希望为CYGWIN_DIR分配CYGWIN目录名称的值。可能我们可以解决这个问题,但如果它永久地设置在你的环境中,那就更好了。这样做的简单方法是:
@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
(第一个为当前LOGON会话中的NEW CMD实例分配值,第二个为将来的登录会话分配.IOW,执行第二个并重新启动...如果您愿意,可以使用SETX CYGWIN_DIR删除条目“” )
-option anoptionvalue
请注意,我稍作修改:readoptions,以便您设置-switch
或/slashoption
或readoptions.bat
另请注意,您可以将部分从:readoptions转到最后一个独立的批处理文件,例如path
,并将该文件放在set path
的任意位置(请参阅CALL READOPTIONS %*
)提示)然后任何批处理都可以执行
options
阅读您的选项/开关/ slashoptions,因为您已在批量中的变量switches
,slashopts
和badoptions
中单独指定它们,并且parameterlist
将包含任何剩余的参数...
(关于冒号的注意事项:
call:例程参数列表
使用“私有”参数调用INTERNAL例程parameterlist
调用例程参数列表
使用'private'参数调用EXTERNAL例程from somewhere on the
;即。 [批处理]文件'例程goto :eof
路径`
{{1}}
内置设施是否转到物理文件结尾。这将终止一个CALL并返回到OR完全退出批处理后的行。 )