We help IT Professionals succeed at work.

WinXP Batch: How to get the UNC Server's name of a file in a mapped network drive?

Medium Priority
1,134 Views
Last Modified: 2012-05-12
I have a variety of mapped network drives on my computer. They reconnect automagically.

I want to have a batch file that can, given a file's path as the %1 input parameter to the batch script...
e.g. "Z:\Folder\Demo.pdf"

... find out what the UNC path of the Z:\ drive is (like "\\Servername\Shared\".

Example:
Input: Z:\Folder\Demo.pdf
Batch File Takes "Z:\" and Translates to "\\Servername\Shared"

Can batch do this?
Comment
Watch Question

Commented:
To get the UNC names from a mapped network drive you would want to use a vbscript or batch file like you said, for vbscript you would check in the registry HKEY_CURRENT_USER\Network\? where ? is the drive letter and to do it through batch commands


set _drive=
set _uncshare=
set _drive=%~d0
if "%_drive%"=="\\" (
for /f "tokens=1,2 delims=\" %%a in ("%~dp0") do set _uncshare=\\%%a\%%b
) ELSE (
for /f "tokens=2,3" %%a in ('net use') do if /i "%%a"=="%_drive%" set _uncshare=%%b
)
:: Below handles case where we are not running off a mapped drive - 
:: just return drive
if NOT DEFINED _uncshare set _uncshare=%_drive%
echo %_uncshare%

Open in new window

Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
For batch you can do something like this:
@echo off
if "%~1"=="" (
  echo Usage: getunc Z:\somedir\somefile.txt
  echo Output: \\server\share\somedir\somefile.txt
  exit /b
)

set file=%~1
set drive=%file:~0,2%

set unc=
for /f "tokens=2* delims= " %%U in ('net use %drive% ^| find /i "Remote name"') do set unc=%%V
echo %unc%\%file:~3%

Open in new window

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
I like it Steve!

~bp

Author

Commented:
I've decided to use rbgCODEDate's solution because it more easily allows me to handle cases where the file provided as input is on a local drive. (e.g. C:\). I changed the %~d0 to %~d1 to allow taking a file (other than itself) as input for easier testing.

However, all that I really need as output is the server name, not the full share path.

If, for example, %_uncshare% = "\\Servername\Users\MyHome"
how do I trim off everything after and includeing the third "\" in the string, then strip off the first two "\" ?
I can't find any Batch tutorials on how to search a string for the position of a character.
Steve KnightIT Consultancy
CERTIFIED EXPERT
Commented:
Fair enough.  Could have added more checking but assumed you were only passing a mapped drive since that was what you asked for!

You can use a for command most easily:

for /f "delims=\ tokens=1" %%a in ('echo %_uncshare%') do set server=%%a

Where %_uncshare% is your path

Steve
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
That's in a batch... use single % instead of %% for command line.

Commented:
Yes thank you the single percent would work and you can also use string manipulation commands like trim left and trim right.

Author

Commented:
I ended up with something like this (this is just part of the product)...

Thank you both for your help!
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

IF "X%~1"=="X" GOTO SETUP

SET _file=
SET _drive=
SET _uncshare=
SET _drive=%~d1
SET server=

IF "%_drive%"=="\\" (
FOR /f "tokens=1,2 delims=\" %%a IN ("%~dp1") DO SET _uncshare=\\%%a\%%b
) ELSE (
FOR /f "tokens=2,3" %%a IN ('net use') DO IF /i "%%a"=="%_drive%" SET _uncshare=%%b
)

IF NOT DEFINED _uncshare (
SET _uncshare=%_drive% 
MSG "%username%" This file appears to be local to %computername% on the !_uncshare! drive.
) ELSE (
FOR /f "delims=\ tokens=1" %%a in ('echo !_uncshare!') DO SET server=%%a
SET _file=%~nx1
ECHO.Server: !server!
ECGI,File: !_file!
)

Open in new window

Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Good stuff.  Glad to help

Steve

Commented:
Yes I to am glad to have been some help in solving your problem.  

Explore More ContentExplore courses, solutions, and other research materials related to this topic.