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

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

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

Post by MigrationUser »

25 Mar 2008 11:40
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

----------------------------

#2 25 Mar 2008 13:10
bluesxman


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

Code: Select all

@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

----------------------------

#3 25 Mar 2008 18:19
budhax


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.

Code: Select all

@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)

----------------------------

#4 26 Mar 2008 00:50
bluesxman


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:

Code: Select all

@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

----------------------------

#5 26 Mar 2008 01:53
budhax


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