I have seen How to run batch file from network share without "UNC path are not supported" message? - and the pushd %~dp0
to mount the network path as a drive, and popd
to unmount the drive, seems to work for me.
I'd just like to make the batch script capable of detecting whether the location the script is started from is a local, or a network/UNC path. How could I do that? I'd guess, one could check if the first two characters in the path are backslashes \\
, but I have no idea how to do it in batch.
So, as somewhat of a pseudocode, I'd need something like:
call :IsUncPath %~dp0 is_unc_path
if %is_unc_path% == 1 (
echo "Mounting UNC path as local drive"
REM @pushd %~dp0
)
echo "Script does whatever here..."
if %is_unc_path% == 1 (
echo "Unmounting local drive for UNC path"
REM @popd
)
pause
:: "force execution to quit at the end of the "main" logic" http://steve-jansen.github.io/guides/windows-batch-scripting/part-7-functions.html
EXIT /B %ERRORLEVEL%
:IsUncPath
echo The value of parameter 1 is %~1
:: NB: there must not be spaces around the assignment!
set "%~2=1"
EXIT /B 0