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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
budhax
Posts: 63
Joined: 09 Oct 2006 12:25

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

#1 Post by budhax » 25 Mar 2008 04:41

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

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 26 Mar 2008 22:25

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? ;)

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#3 Post by DosItHelp » 26 Mar 2008 23:07

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:

budhax
Posts: 63
Joined: 09 Oct 2006 12:25

#4 Post by budhax » 29 Mar 2008 20:41

Thanks a lot DosItHelp.
Another solution: http://www.ss64.org/viewtopic.php?id=340

Post Reply