0

I am running a batch script and outputting the info to a file in the temp folder, (just to learn from).

I have an input for a user to select which mapped drive they want their data transferred out of.

Here is what I have, it is super easy so I can learn then modify later.

Cls
@ECHO Off
Net use > c:\Temp\Map.txt
Net Use
Pause
:Start

ECHO Which Drive letter do you want to back up?
ECHO ? - Enter Drive letter
ECHO E - Exit
SET /P M=Make Choice then press ENTER:
IF %M%==E Exit Else goto :here
If %M%==e Exit Else goto :here

:Here
Echo %M%
Findstr "OK           %M%:" c:\Temp\Map.txt
pause

What I need is to extract the path name for the drive they select. Like if there are 5 drives, and they want drive S: backed up, output is:

OK           S:        \\10.X.X.X\Deployment\David\Davids_Scripts\bats

I would need the \\10.52.8.1\Deployment\David\Davids_Scripts\bats saved so I can put it in a transfer script.

Compo
  • 36,585
  • 5
  • 27
  • 39
doheth
  • 87
  • 9
  • Well, for unix systems I would use the "sed" command. Don't know for Microsoft. – Gyro Gearloose Oct 12 '20 at 19:11
  • 3
    I would suggest, instead of using `net use`, that you use this command in a [tag:for-loop], `%SystemRoot%\System32\wbem\WMIC.exe" Path Win32_MappedLogicalDisk Get DeviceID,ProviderPath`, and capture its output. It should list all mapped drive letters along side their respective network paths too. – Compo Oct 12 '20 at 19:46
  • (some minor improvements with the `if`: use the `/i` switch for case insensitive comparison, quote both sides to avoid syntax errors with empty inputs: `if /i "%M%" == "E" goto :eof`) – Stephan Oct 13 '20 at 08:43

1 Answers1

1

As Compo already commented, wmic is a better way than net use (mainly because you can't depend on a consistent token count *), which makes it difficult to process with a for loop. Wmic a bit slower than net use, but the benefits make it worth the time-loss.

@echo off
setlocal
set "M=Q:"
for /f "delims=" %%a in ('wmic netuse where LocalName^="%M%" get RemoteName^,Status /value ^|find "=" ') do set "%%a"
echo your drive is %RemoteName% and it's Status is %Status%.

To list your network drives, you can also use:

wmic netuse where LocalName="Q:" get  localname,remotename,status

*) net use is language dependent and the English "Unavailable" (one token) is "Nicht verfgb" (two tokens) in German.
Even within the same language you can't depend: (German example): "Verbunden" (one token) vs. "Nicht verfgb" (two tokens)

Stephan
  • 53,940
  • 10
  • 58
  • 91