3

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?

ewall
  • 27,179
  • 15
  • 70
  • 84

4 Answers4

1

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
ewall
  • 27,179
  • 15
  • 70
  • 84
  • 1
    `\\?\C:` begins with two backslashes ... and can well be a local path. It's in fact how you can break the 260 character barrier for path lengths in the Win32 API. – 0xC0000022L Jun 27 '11 at 18:56
  • 1
    @STATUS_ACCESS_DENIED: While this is true, the cmd.exe built-in expander for %~dI that is documented as "expands %I to a drive letter only" also fails for "\\?\C:" since it will return "\\" – Anders Jun 27 '11 at 19:08
1
@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+

Anders
  • 97,548
  • 12
  • 110
  • 164
  • Ooh, smart use of the `net use %~d1` plus the `&&` -- much better than my looping through the `net use` results! – ewall Jun 27 '11 at 19:37
0

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.

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
-2

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"

Gosha A
  • 4,520
  • 1
  • 25
  • 33
  • What about mapped drives, they are remote but don't start with \\. And why ruby, if checking for \\ was enough you could do it in a batch file... – Anders Jun 27 '11 at 18:28
  • I luv Ruby, but alas it's not portable enough for this script, which must be "pure" CMD shell. – ewall Jun 27 '11 at 18:33
  • 2
    What about local paths that start with `\\?\` and are *not* remote? – 0xC0000022L Jun 27 '11 at 18:57
  • I said it's very basic script :) I don't know how can I do this in pure CMD script, because I'm not interested in windows and its shell. – Gosha A Jun 28 '11 at 06:42