告诉CMD行,如果一个文件/路径是本地还是远程? [英] CMD line to tell if a file/path is local or remote?
本文介绍了告诉CMD行,如果一个文件/路径是本地还是远程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从NT shell脚本,我需要能够告诉如果目标路径是一个本地驱动器上(如 C:\\
或 D:\\
)或远程/映射驱动器上(无论是 \\\\ UNC \\路径
或类似以Z映射驱动器号:
)...任何建议
解决方案
关闭@echo
转到主:isremote
SETLOCAL
设置_isremote = 0
如果%〜D1==\\\\(设置_isremote = 1)其他(
(大于NUL 2 - ;&放大器; 1 NET USE%〜D1)及和放大器;设置_isremote = 1
)
ENDLOCAL&放大器;设置isremote =%_ isremote%放大器;转到:EOF:测试
拨打:isremote%〜1
回声%isremote%==%〜1
@goto:EOF:主要
拨打:测试C:
拨打:测试C:\\
拨打:测试C:\\ WINDOWS
拨打:测试\\\\富
拨打:测试\\\\富\\
拨打:测试\\\\富\\酒吧
拨打:测试\\\\富\\酒吧\\巴兹
拨打:试验Z:
拨打:试验Z:\\
拨打:试验Z:\\ TEMP
在我的系统,其中z:是映射驱动器,我得到:
0 == C:
0 == C:\\
0 == C:\\ WINDOWS
1 == \\\\富
1 == \\\\富\\
1 == \\\\富\\酒吧
1 == \\\\富\\酒吧\\巴兹
1 == Z:
1 == Z:\\
1 == Z:\\ TEMP
注:可能会在UNC符号链接失败的NT6 +
From an NT shell script, I need to be able to tell if the target path is on a local drive (like C:\
or D:\
) or on a remote/mapped drive (either \\UNC\path
or mapped drive letter like Z:
)... Any suggestions?
解决方案
@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
On my system where z: is a mapped drive I get:
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
Note: Will probably fail on UNC symlinks on NT6+
这篇关于告诉CMD行,如果一个文件/路径是本地还是远程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文