Link to home
Create AccountLog in
Avatar of travisjbennett
travisjbennettFlag for United States of America

asked on

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

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?
ASKER CERTIFIED SOLUTION
Avatar of rbgCODE
rbgCODE
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Steve Knight
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

Avatar of Bill Prew
Bill Prew

I like it Steve!

~bp
Avatar of travisjbennett

ASKER

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.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
That's in a batch... use single % instead of %% for command line.
Yes thank you the single percent would work and you can also use string manipulation commands like trim left and trim right.
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

Good stuff.  Glad to help

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