#1 25 Mar 2008 11:40

budhax
Member
Registered: 04 Dec 2007
Posts: 49

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

Hello,
is it possible to do this mapping:

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:

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

Thanks

Offline

#2 25 Mar 2008 13:10

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,105

Re: How to use a FOR loop to match items of 2 "array" variables?

Something like this should do what you ask, though it's not the only method I can think of.
* * UNTESTED * *

@echo off
setlocal enabledelayedexpansion

SET drives=S J P
SET remoteFolders=SHARED SHARED\08.JOBS SHARED\00.PERSO\ALP
SET server=192.160.100.11

set dcount=0
set fcount=0

for %%d in (%drives%) do (
    set /a dcount+=1
    set fcount=0
    for %%f in (%remoteFolders%) do (
        set /a fcount+=1
        if !dcount! EQU !fcount! if not exist "%%~d:\" net use %%~d: "\\%server%\%%~f" 
    )
)

pause

cmd | *sh | Ruby | Chef

Offline

#3 25 Mar 2008 18:19

budhax
Member
Registered: 04 Dec 2007
Posts: 49

Re: How to use a FOR loop to match items of 2 "array" variables?

Thank you bluesxman,
your code works fine.

QUESTION
What does  "~"  mean in "%%~d  or  %%~f   ?
Your code works fine with or without "~" in this line:

if !dcount! EQU !fcount! ECHO.if not exist "%%~d:\" net use %%~d: "\\%server%\%%~f"

Thanks in advance.


Based on your idea, here is a script converting a phrase in UPPERCASE.

@ECHO OFF &SETLOCAL
@SETLOCAL ENABLEDELAYEDEXPANSION
SET MIN=a b c d e f g h i j k l m n o p q r s t u v w x y z
SET CAP=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

SET Phrase=Phoebe Silvarumdue. Botens Diana

ECHO.%Phrase%

SET Mi=0
FOR %%m in (%MIN%) DO (
    SET /a Mi+=1
    SET Ci=0
    FOR %%c in (%CAP%) DO (
        SET /a Ci+=1
        IF "!Mi!"=="!Ci!" (SET Phrase=!Phrase:%%m=%%c!)
    )
)

ECHO.%Phrase%

Last edited by budhax (25 Mar 2008 18:31)

Offline

#4 26 Mar 2008 00:50

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,105

Re: How to use a FOR loop to match items of 2 "array" variables?

I meant to point out that the method I used wasn't particularly efficient, but for 3 items it would suffice, since only 9 iterations are required.  If you start working with longer lists and/or nesting the FOR commands more deeply it's going to lag quite badly.

The "~" is used to remove surrounding double quotes on "%1" and "%%a" type strings.  It wasn't necessary in the example you gave, as the source data had no double quotes, I used it out of force of habit.

As for your case-changer, you can do it much more simply than that, by taking advantage of the fact that string replacements aren't case sensitive.  For example:

@echo off
setlocal enabledelayedexpansion
set phrase=Phoebe Silvarumdue. Botens Diana
set alpha=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
for %%a in (%alpha%) do set phrase=!phrase:%%a=%%a!

set phrase
pause

cmd | *sh | Ruby | Chef

Offline

#5 26 Mar 2008 01:53

budhax
Member
Registered: 04 Dec 2007
Posts: 49

Re: How to use a FOR loop to match items of 2 "array" variables?

Thank you so much bluesxman,
you are the man, :clap:
Another script added in my DOS library ;-))

Offline

Board footer

Powered by FluxBB