Yes, that would be why it failed. Sorry about that!
A couple of changes that should hopefully get it working:
1) Comment or remove the "PUSHD "%~dp0" line directly above the ":LOOP" line.
2) Remove the new code that you added to the top (below the "@ echo off" and above the "set LC_ALL=en_US") and paste in the following updated code. This should work regardless of whether or not the drive is mapped. If the drive is mapped it will determine the UNC path and temporarily map a new drive (first letter available) to the level of the batch script in order to avoid the spaces in the path.
Hopefully this works better!
Cheers,
Andrew
:Get the current folder
set CURRENTDIR=%~dp0
:Have TEMPMAP default to current folder for when the drive is already mapped
:set TEMPMAP=%CURRENTDIR%
SET FIRSTTWO=%CURRENTDIR:~0,2%
SET THEREST=%CURRENTDIR:~2%
echo First two characters are "%FIRSTTWO%" and the rest of the path is "%THEREST%"
IF %FIRSTTWO%==\\ GOTO mapdrive
echo Drive is already mapped, now to find the UNC path
:Find the UNC path based on currently mapped drive letter
for /f "tokens=3" %%b in ('net use ^| find "%FIRSTTWO%"') do set CURRENTDIR=%%b%THEREST%
:mapdrive
echo UNC path is "%CURRENTDIR%"
:Remove the trailing slash (causes "net use" to fail)
IF %CURRENTDIR:~-1%==\ SET CURRENTDIR=%CURRENTDIR:~0,-1%
echo Will now map drive to UNC path of batch script folder (deepest level to avoid any spaces)
:Map folder (not peristent) to first available letter. Normally pushd would be used but it maps to the first
:folder in the path and subsequent folders also have spaces so it won't resolve the problem
net use * "%CURRENTDIR%" /persistent:no
:Find which drive letter the new/temporary mapping is under
for /f "tokens=2" %%a in ('net use ^| find "%CURRENTDIR%"') do set TEMPMAP=%%a
echo %TEMPMAP% has been mapped to "%CURRENTDIR%"