开发者

CMD line to tell if a file/path is local or remote?

From an NT s开发者_运维知识库hell 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+


Below is what I came up with. The target file/path is passed as parameter %1.

set REMOTE=0
set TEST=%~f1
if "%TEST:~0,2%"=="\\" (
    echo *** target is a remote UNC path
    set REMOTE=1
) else (
    for /f "skip=6 tokens=2" %%d in ('net use') do (
        if /i "%TEST:~0,2%"=="%%d" (
            echo *** target is a remote mapped drive
            set REMOTE=1
        )
    )
)
if %REMOTE%==0 echo *** target is a local file/directory


Your best bet is to use something like this:

fsutil fsinfo drivetype X:

However, the respective code may be language dependent due to the output from fsutil. If that's not an issue you will be best off tokenizing and using the output from fsutil.


Basic recognition script in Ruby, doesn't support mapped drivers.

where_placed.rb:

path = ARGV[0]
if path.start_with? "\\"
  puts "remote"
else
  puts "local"
end

> ruby where_placed.rb "C:\file.dat > "local"

> ruby where_placed.rb "\\REMOTEMACHINE\file.dat > "remote"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜