Skip navigation

JSI Tip 7780. How can I test if a script was executed from a network path?

Using batch parsing techniques, I have scripted RemoteScript.bat to determine if your script was executed from a remote network path.

The syntax for using RemoteScript.bat is:

call RemoteScript FullyQualifiedScriptName YesNo

Where FullyQualifiedScriptName should literally be %~F0, and YesNo is a call directed environment variable that will contain a Y if the your script was executed from a remote network path, or an N if it was executed from a local path.

NOTE: A UNC path that points to the local computer is considered local, as are net use and subst drives that point to the local computer.

RemoteScript.bat contains:

@echo off
if \{%2\}==\{\} @echo Syntax call RemoteScript ScriptPath YesNo&exit /b 1
if not exist %~F1 @echo Syntax call RemoteScript ScriptPath YesNo - ScriptPath does NOT exist.&exit /b 2
setlocal
set Rmt=N
set Drv=%~D1
if "%Drv%" NEQ "\\" goto callq
set Rmt=Y
:: Test if UNC path is local
for /f "Tokens=*" %%r in ('@echo %~F1^|Findstr /i /l /c:"\\%computername%"') do set Rmt=N
goto Finish
:callq
call :quiet>nul 2>&1
:Finish
endlocal&set %2=%Rmt%
exit /b 0
:quiet
for /f "Tokens=*" %%r in ('net use %Drv%^|Findstr /I /L /C:"Remote name"^|Findstr /i /l /v /c:"\\%computername%"') do set Rmt=Y&goto quietend
for /f "Tokens=*" %%r in ('subst ^|Findstr /i /l /b /c:"%Drv%"^|Findstr /i /l /c:UNC\^|Findstr /i /l /v /c:"\\%computername%"') do set Rmt=Y
:quietend



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish