2011-06-27 39 views

回答

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 

在我的系統,其中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 

注:可能會在UNC符號鏈接失敗在NT6 +

+0

哦,聰明地使用'net use%〜d1'加上'&&' - 比我通過'net use'結果循環更好! – ewall

+0

有趣的解決方案。 +1 – 0xC0000022L

-2

Ruby中的基本識別腳本不支持映射的驅動程序。

where_placed.rb:

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

> ruby where_placed.rb "C:\file.dat> 「本地」

> ruby where_placed.rb "\\REMOTEMACHINE\file.dat> 「遠程」

+0

有關映射驅動器,它們是什麼遠程但不要不要從\\開始。以及爲什麼紅寶石,如果檢查\\是足夠的,你可以在一個批處理文件中做到這一點... – Anders

+0

我luv紅寶石,但唉它不夠便攜,這個腳本,它必須是「純」的CMD外殼。 – ewall

+2

以'\\?\'開頭且不*遠程的本地路徑怎麼樣? – 0xC0000022L

1

下面是我想出了。目標文件/路徑作爲參數%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 
+1

'\\?\ C:'以兩個反斜槓開始......並且很可能是本地路徑。事實上,你可以在Win32 API中打破260個字符的路徑長度。 – 0xC0000022L

+1

@STATUS_ACCESS_DENIED:雖然這是真的,但記錄爲「僅將%I擴展爲驅動器號」的%〜dI的cmd.exe內置擴展器對於「\\?\ C:」也會失敗,因爲它將返回「\\」 – Anders

0

最好的辦法就是用這樣的:

fsutil fsinfo drivetype X: 

然而,相應的代碼可能會依賴於語言的,由於從FSUTIL輸出。如果這不是問題,則最好使用fsutil的標記和使用輸出。

+0

fsutil需要XP上的管理員權限... – Anders

+0

@Anders:公平點。所以 ...? :) – 0xC0000022L

相關問題