从 NT shell 脚本,我需要能够判断目标路径是否在本地驱动器上(如 C:\
或 D:\
)或在远程/映射驱动器上(\\UNC\path
或映射驱动器盘符,如 Z:
)...有什么建议吗?
最佳答案
@echo off
goto main
:isremote
setlocal
set _isremote=0
if "%~d1"=="\\" (set _isremote=1) else (
(>nul 2>&1 net use "%~d1") && set _isremote=1
)
endlocal&set isremote=%_isremote%&goto :EOF
:test
call :isremote "%~1"
echo %isremote% == %~1
@goto :EOF
:main
call :test c:
call :test c:\
call :test c:\windows
call :test \\foo
call :test \\foo\
call :test \\foo\bar
call :test \\foo\bar\baz
call :test z:
call :test z:\
call :test z:\temp
在我的系统中,z: 是一个映射驱动器,我得到:
0 == c:
0 == c:\
0 == c:\windows
1 == \\foo
1 == \\foo\
1 == \\foo\bar
1 == \\foo\bar\baz
1 == z:
1 == z:\
1 == z:\temp
注意:可能会在 NT6+ 上的 UNC 符号链接(symbolic link)上失败
关于batch-file - CMD 行来判断文件/路径是本地的还是远程的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6496713/