Page 1 of 1

How to use a FOR loop to match items of 2 "array"

Posted: 25 Mar 2008 04:41
by budhax
Hello,
is it possible to do this mapping:

Code: Select all

IF NOT EXIST "S:\" NET USE S: \\192.168.100.11\SHARED
IF NOT EXIST "J:\" NET USE J: \\192.168.100.11\SHARED\08.JOBS
IF NOT EXIST "P:\" NET USE P: \\192.168.100.11\SHARED\00.PERSO\ALP


using a FOR loop and 2 variables:

Code: Select all

SET drivers=S J P
SET remoteFolders=SHARED SHARED\08.JOBS SHARED\00.PERSO\ALP


Thanks

Posted: 26 Mar 2008 22:25
by DosItHelp
budhax,

Not that we like goto:loop stuff here at DosTips but the following may work for you:

Code: Select all

@ECHO OFF
SET drivers=S J P
SET remoteFolders=SHARED SHARED\08.JOBS SHARED\00.PERSO\ALP

:loop
if not defined drivers GOTO:DONE
for /f "tokens=1,*" %%A in ("%drivers%") do set "dr=%%A"&set "drivers=%%B"
for /f "tokens=1,*" %%A in ("%remoteFolders%") do set "rf=%%A"&set "remoteFolders=%%B"
IF NOT EXIST "%dr%:\" echo NET USE %dr%: \\192.168.100.11\%rf%
goto:loop

:DONE

Output:
NET USE S: \\192.168.100.11\SHARED
NET USE J: \\192.168.100.11\SHARED\08.JOBS
NET USE P: \\192.168.100.11\SHARED\00.PERSO\ALP

DOS IT HELP? ;)

Posted: 26 Mar 2008 23:07
by DosItHelp
budhax,
Here the solution without using a goto:loop

Code: Select all

@ECHO OFF
SET drivers=S J P
SET remoteFolders=SHARED SHARED\08.JOBS SHARED\00.PERSO\ALP

for /f %%D in ('"echo.%drivers: =&echo.%"') do (
    for /f "tokens=1,*" %%F in ('"echo.%%remoteFolders%%"') do (
        set "remoteFolders=%%~G"
        IF NOT EXIST "%%D" echo.NET USE %%D: \\192.168.100.11\%%F
))

Output:
NET USE S: \\192.168.100.11\SHARED
NET USE J: \\192.168.100.11\SHARED\08.JOBS
NET USE P: \\192.168.100.11\SHARED\00.PERSO\ALP

:wink:

Posted: 29 Mar 2008 20:41
by budhax
Thanks a lot DosItHelp.
Another solution: http://www.ss64.org/viewtopic.php?id=340