batch-file - CMD 行来判断文件/路径是本地的还是远程的?

标签 batch-file cmd

从 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/

相关文章:

Python:在 32 位 python 上使用 subprocess.Popen() 运行批处理文件时出错

batch-file - 当用户按下按钮时停止循环

batch-file - robocopy 在批处理文件中的标准输出和错误输出

batch-file - 有运行时间限制吗?

c++ - 在 Windows 8 上使用 MinGW 在 cmd 中编译 C++

windows - 为什么 findstr 不能正确处理大小写(在某些情况下)?

batch-file - 为什么FOR/F为其余 token 中的重复数字设置空值?

Windows 批处理循环遍历子文件夹并运行命令

CMD命令以复制具有特定扩展名的文件

node.js - 无法识别 Cordova... 安装后立即