1

I am updating a windows logon batch script. I wanted to simplify the volume of lines needed to map all the necessary drives, but be able to make defining the script for the user group easy. Presently I have all the drive paths defined as variables, as well as the user groups and what drive letters they get.

The problem is that I have need of a nested variable in a FOR statement. The nested variable needs to complete the name of the variable it is nested in. For example:

I have user/group variables: set john=i,j,l,m,o,p,q,r,s,u,y,z (6 users/groups like this are defined)

drive mapping variables: set mapdrv_i=i: \\sqlserver\ARCHIVE>NUL (necessary drives in the group i-z are defined)

My FOR statement is for %%C in (%john%) do if defined mapdrv_%%C net use %%mapdrv_%%C%%>NUL && if Errorlevel 0 ( ECHO. %%mapdrv_%%C%% - Mapped. ) else ( ECHO. Error mapping: %%mapdrv_%%C%% )

I do have setlocal enabledelayedexpansion set early on in the script.

This is intended to allow me to define what drives are mapped simply by changing the user/group defined in the in ( ) portion of the FOR statement. The problem is that %%mapdrv_%%C%% resolves %%C properly but does not resolve further, it litterally places %mapdrv_N% into the command so all net use commands produced by the FOR statement obviously fail.

How do I correct the nested %%mapdrv_%%C%% to allow variable to be properly resolved and the command to work?

1 Answer 1

0

Since you are already using delayedexpansion, change %%mapdrv_%%C%% to !mapdrv_%%C! and it should work.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you - made the change and while I had to contend with finding a typo that was also causing me a headache, this worked perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.