GHamilton
GHamilton

Reputation: 35

Determine Number of Tokens - BATCH

I'm currently working on a mass user creation script through PowerShell and Batch. At the moment the script is 95 lines and is the largest script I've ever written in Batch.

I want the script to be as automated as possible and plan to give it to clients that need help creating a mass number of users. To do this, I have a 21 line settings file and one of the variables that I need is the full domain name (This is needed for dsadd)

The problem with this is that users may have any number of variables for this - anywhere from two in testlabs to four in places like schools. I am so far able to separate the tokens however I need them all stored as variables like %%a, %%b and not to store everything as %%a. The number of tokens will be dynamic so I need some sort of solution to this. Something like this (Yes I know this is not the correct syntax):

if number of tokens=4 (
dsadd "cn=%%a,ou=%%b,dc=%DSuffix1%,dc=%DSuffix2%,dc=%DSuffix3%,dc=%Dsuffix4% )

In that line %%a and %%b are variables in another for loop later in the code that reads from a user list excel file. I would need something like that for anything from two tokens to four tokens. I don't mind if the solution to this is not purely BATCH however that is my preferred option.

Thanks.

EDIT: Here is the for loop I have at the moment, this is nested in another larger for loop that adds the users:

for /f "tokens=1,2,3,4 delims=." %%a in (Settings.ini) do (
set L=22
if !L!=22 set DSuffix1=%%a&& set DSuffix2=%%b&& set DSuffix3=%%c&& set DSuffix4=%%d
)

The settings.ini file contains various settings such as the Exchange server and path to the user's home directory. The line I need to interpret is line 22 which looks like this:

DOMAINNAME.SUFFIX(.SUFFIX.SUFFIX.SUFFIX) A real life example would be: testlab.local or testlab.ghamilton.local

For a testlab the setting should only be the domainname and suffix although for others such as schools or institutions the number of domain suffixes can go up to four. I want to interpret this.

EDIT: Managed to indent code correctly, sorry.

Upvotes: 3

Views: 3097

Answers (2)

Nate Hekman
Nate Hekman

Reputation: 6657

If I understand you right, you have a string such as domain.foo.bar.baz and you want to change that to dc=domain,dc=foo,dc=bar,dc=baz.

How about this?

set domain=domain.foo.bar.baz
echo dc=%domain:.=,dc=%

That should echo this:

dc=domain,dc=foo,dc=bar,dc=baz

That %domain:.=,dc=% line is expanding the %domain% environment variable, but replacing all . with ,dc=. See http://ss64.com/nt/syntax-replace.html for more details.

To do string replacement in an environment variable, you do need the value to be in an environment variable first. Here's a script that will read line 22 from settings.ini, combined with the above technique:

setlocal enabledelayedexpansion
set L=0
for /f %%a in (settings.ini) do (
    set /a L=!L! + 1
    if !L! equ 22 (
        set domain=%%a
        echo dc=!domain:.=,dc=!
    )
)

L is being used to count what line we're on. When we reach line 22, we set an environment variable to the value of the entire line (%%a), then do the string substitution as above (but with ! rather than % to take advantage of delayed expansion).

Of course instead of an echo, you would do something like

dsadd "cn=%cn%,ou=%ou%,dc=!domain:.=,dc=!"

Upvotes: 3

BDM
BDM

Reputation: 3900

Here's my little snippet to sort of demonstrate how to get the separate tokens (the delims are space and tab, but you can change them).

@echo off & setlocal enabledelayedexpansion
for /f "tokens=1,2,3,4" %%a in (test.txt) do (
    set token1=%%a
    set token2=%%b
    set token3=%%c
    set token4=%%d
    set full=%%a%%b%%c%%d
)
set token
set full
pause>nul

test.txt contains:

first second third

The output was:

token1=first
token2=second
token3=fourth
full=firstsecondthird

So, you could judge how many tokens there is by something like

for /l %%i in (4,-1,1) do if not defined token%%i set amount=%%i

Or something along the same lines.

Hope that helps.

Upvotes: 0

Related Questions