2014-01-21 3 views
0

.CMD 파일 내부에서 스크립트를 실행할 때 % ~ dp0은 현재/작업 디렉터리를 가져옵니다. UNC 경로를 인식합니다. 그러나 3 개의 플래시 드라이브, 5 개의 다른 휴대용 드라이브 및 3 개의 다른 파일 서버에 여러 개의 스크립트가 있습니다. 나는 그들 모두에 달릴 1 개의 원본을 쓸 수있을 것입니다 싶습니다.UNC 원본을 드라이브로 매핑

스크립트를 사용하여 "% ~ dp0"을보고 UNC 원본에 있는지 여부를 확인하고 원본이 UNC이면 드라이브를 매핑하고 그렇지 않으면 그냥 실행합니까?

답변

0

당신은 드라이브가 매핑되어 있는지 확인하려면 다음을 사용할 수 있습니다 -

net use %~d0 2>nul >nul || echo drive not mapped & goto :run 

2>nul >nul는 NUL에 stdout 및 stderr를 모두 리디렉션합니다.

|| 다음의 명령은 net use 명령으로 오류 수준을 설정 한 경우에만 실행됩니다. 스크립트가 원격으로 실행되는 경우

if "%~d0"=="\\" for /f %%D in (R S T U V W X Y Z) do if not exist %%D:\ (
    net use %%D: %~dp0 >nul 
    set path=%%D: 
    goto :run 
) 

:run 

%~d0\\ 동일하지만 경로가있다 - 드라이브가 원격 서버에 있지만 매핑되지 있는지 확인하려면

, 다음 드라이브를 매핑 아직 매핑되지 않았습니다.

(이미 매핑되어있는 경우는 없으며 문자를 추가/제거 할 수 있음) 이 이미 매핑되어 있는지 확인하여 루프합니다.입니다. 일치하지 않으면 해당 드라이브 문자로 경로를 매핑하고 스크립트를 계속 진행하십시오.

0
@echo off 
    setlocal enableextensions 

    (call :ensureMappedRun %*)|| exit /b 

    rem This line is reached when :ensureMappedRun finishes with errorlevel=0 
    rem meaning the batch file is not being executed from a UNC path 

    echo running "%~f0" %* 
    pause 

    endlocal 
    exit /b 0 

:ensureMappedRun 
    rem test if current context is "drive:...." 
    echo %~f0 | findstr /r /i /b /c:"[a-z]:" >nul 2>nul 
    if not errorlevel 1 exit /b 0 

    rem Running from UNC path. Map to drive letter 
    pushd "\\%~p0" 
    if errorlevel 1 (
     echo ERROR : UNC path not accesible or no drive letters available 
     pause 
     exit 
    ) 

    rem Restart the current batch file from the new drive\path 
    call "%cd%\%~nx0" %* 

    rem Clean the drive assignment 
    popd 

    rem And return, signaling the batch has been run redirected 
    exit /b 1