我看到了 如何在网络共享中运行批处理文件而不出现“UNC path are not supported”消息? - 使用 pushd %~dp0
将网络路径挂载为驱动器,使用 popd
卸载驱动器,对我有效。
我想让批处理脚本能够检测启动脚本的位置是本地路径还是网络/UNC路径。我该如何实现?我猜,可以检查路径中前两个字符是否为反斜杠 \\
,但我不知道如何在批处理中实现。
因此,作为一种伪代码,我需要类似于:
call :IsUncPath %~dp0 is_unc_path
if %is_unc_path% == 1 (
echo "Mounting UNC path as local drive"
REM @pushd %~dp0
)
echo "Script does whatever here..."
if %is_unc_path% == 1 (
echo "Unmounting local drive for UNC path"
REM @popd
)
pause
:: "force execution to quit at the end of the "main" logic" http://steve-jansen.github.io/guides/windows-batch-scripting/part-7-functions.html
EXIT /B %ERRORLEVEL%
:IsUncPath
echo The value of parameter 1 is %~1
:: NB: there must not be spaces around the assignment!
set "%~2=1"
EXIT /B 0
pushd
/popd
命令来处理所有路径。 - aschipfl