0

I've got similar information stored in two variables, both containing Windows share names.

set _N= net use N: \\WS1\DISK2; net use O: \\WS1\DISK3; net use P: \\WS2\DISK3; net use Q: \\WS3\DISK2; net use U: \\WS-NAS1\NAS1; net use V: \\WS5\DISK4;

set _DRV2map= \\WS1\DISK3 \\WS2\DISK3 \\WS-NAS1\NAS1

_N always contains all the values stored in _DRV2map.

How can I compare the two variables and retrieve information by getting a handle onto the corresponding expressions in _N.

With this example data the desired result should be as follows:

Starting with _DRV2map the result for the term \\WS1\DISK3 should be net use O: \\WS1\DISK3 retrieved from _N. Likewise for \\WS2\DISK3 the return is net use P: \\WS2\DISK3 and for \\WS-NAS1\NAS1 the return is net use U: \\WS-NAS1\NAS1

The important information seeked is the drive letter (in _N) for a given share name in both variables.

I am able to compare two variables and find the differences between them as long as both variables got their content structured equally. But in this case the content structure differs and I have no clue how to get started eventually leading to a meaningful result. That is the reason I did not provide any code. I don't even know whether this is possible using batch processing (I would not be surprised if it weren't).

Advice, help and suggestions are much appreciated. Greetings

snahl
  • 497
  • 2
  • 10
  • 25

4 Answers4

0

The request is clear: identify the letters associated with the share names from the 2nd envvar, but the output (what should be displayed to the user) is not. Therefore here's a small batch file that will iterate through network paths from %_DRV2map% and for each:

  • will print it
  • will iterate through the commands from %_N%, and if it finds the network path it will display the associated mapped network drive:

Note: I's not very flexible regarding the envvars content (for example if the semicolon ; doesn't follow the share name in the 1st var, it won't work)

@echo off

echo.
echo _N: %_N%
echo _DRV2map: %_DRV2map%

call :loopshares "%_DRV2map%"
goto :eof

:loopshares
    for /f "tokens=1,*" %%f in (%1) do (
        echo.
        echo Share name: %%f
        call :loopcmds "%_N%" "%%f"
        call :loopshares "%%g"
    )
    goto :eof


:loopcmds
    for /f "tokens=1,2,3,4,*" %%f in (%1) do (
        if "%~2;" EQU "%%i" (
            echo Mapped Network Drive: %%h
            goto :eof
        )
        call :loopcmds "%%j" "%~2"
    )
    goto :eof

It might seem a little bit cryptic, here's a link containing very good explanations and examples; on my opinion it could be called The batch FOR command Bible.

CristiFati
  • 38,250
  • 9
  • 50
  • 87
0

A simple way :

@echo off&cls
setlocal enabledelayedexpansion

set _N= net use N: \\WS1\DISK2; net use O: \\WS1\DISK3; net use P: \\WS2\DISK3; net use Q: \\WS3\DISK2; net use U: \\WS-NAS1\NAS1; net use V: \\WS5\DISK4;
set _DRV2map= \\WS1\DISK3 \\WS2\DISK3 \\WS-NAS1\NAS1

set "$c=1"
for %%a in (%_DRV2map%) do (
         Echo Treating ==^> [%%a]
         for %%x in (%_N%) do (
            if !$c!==3 (
               set "$Drive=%%x"
               set "$c=1"
            )
            if "%%a"=="%%x" echo Command for [%%a] ==^> net use !$Drive! %%a&echo ****
         set /a $c+=1
         )
)

Output :

Treating ==> [\\WS1\DISK3]
Command for [\\WS1\DISK3] ==> net use O: \\WS1\DISK3
****
Treating ==> [\\WS2\DISK3]
Command for [\\WS2\DISK3] ==> net use P: \\WS2\DISK3
****
Treating ==> [\\WS-NAS1\NAS1]
Command for [\\WS-NAS1\NAS1] ==> net use U: \\WS-NAS1\NAS1
****
SachaDee
  • 9,245
  • 3
  • 23
  • 33
0

You don't give an example of what you want your output to be.

This might work for you:

@ECHO OFF
SETLOCAL
set _N= net use N: \\WS1\DISK2; net use O: \\WS1\DISK3; net use P: \\WS2\DISK3; net use Q: \\WS3\DISK2; net use U: \\WS-NAS1\NAS1; net use V: \\WS5\DISK4;
set _DRV2map= \\WS1\DISK3 \\WS2\DISK3 \\WS-NAS1\NAS1

FOR %%d IN (%_drv2map%) DO (
 FOR /f "tokens=3,4" %%m IN ('echo %_N:;=^&echo(%') DO IF "%%n"=="%%d" ECHO %%m %%d
)

GOTO :EOF
Magoo
  • 77,302
  • 8
  • 62
  • 84
0

The simplest way to correlate information from two sources is via an array in which the elements have in the subscript the value from one source, and in the contents the value from the second source. This method is both simple and fast, because the array is created just once and, after that, the access to each element is immediate no matter the number of elements. The Batch file below create an array with the common data in the subscript, and the rest of information from _N in the value. After that, just show the elements of the subscripts given by _DRVmap.

@echo off
setlocal EnableDelayedExpansion

set _N= net use N: \\WS1\DISK2; net use O: \\WS1\DISK3; net use P: \\WS2\DISK3; net use Q: \\WS3\DISK2; net use U: \\WS-NAS1\NAS1; net use V: \\WS5\DISK4;

set _DRV2map= \\WS1\DISK3 \\WS2\DISK3 \\WS-NAS1\NAS1

rem Load the data from _N into an array
for /F "delims=" %%a in (^"!_N:^;^=^
% Do NOT remove this line %
!^") do for /F "tokens=4" %%b in ("%%a") do (
   set "name[%%b]=%%a"
)

rem Get the information in _N from _DRV2map
echo   _DRV2map        _N
echo/
for %%a in (%_DRV2map%) do echo %%a      !name[%%a]!

Output:

  _DRV2map        _N

\\WS1\DISK3       net use O: \\WS1\DISK3
\\WS2\DISK3       net use P: \\WS2\DISK3
\\WS-NAS1\NAS1       net use U: \\WS-NAS1\NAS1

For further information on array management in Batch files, see this post.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108