Tokens In Batch File

Posted on by

May 08, 2012 In my batch file, I need to add as a delims in my FOR command as: For /f Tokens=1 Delims=%%A in (Whatever) do echo%%A However, this command line does not. Mar 16, 2008 Can someone explain how and give an example or two of how tokens work in batch files? Batch file FOR /f tokens. This reads a file line-by-line, removing leading spaces (thanks, jeb). Sets then the line variable to the line just read and. Calls a subroutine that does something with the line. Is the start of the subroutine mentioned above. Will then split the line at /, but stopping tokenization after the first token.

Batch File Virus

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. 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. Solange Solo Star Rar. 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. Sorry if I'm dense, but I'm trying to make sense of this and I don't understand what you're asking! Format your code correctly (indent each code line at least four spaces so SO formats it as code) and 2. Provide a sample of the pertinent lines in the settings.ini file, along with 3. What you hope to have as the result from those sample lines.

For example, do you just want to end up with the number as your title seems to indicate? Or are you wanting a 'vector'? What's wrong with just%%a,%%b etc? – Mar 9 '13 at 4:33 •.

I'm unsure how to format the code properly, sorry. I'll include 1 and 2 in the original post. As for three, I hope to have it so that the script finds the number of tokens in the specific line in the Settings file and then adds users through dsadd according to the number of tokens. This would be like three dc sections if there are three tokens, four if there are four tokens etc. I want to store the various domain suffixes as variables because I have other for loops after this one so%%a etc. Would not work. – Mar 9 '13 at 4:37 •.

If I understand you right, you have a string such as domain.foo.bar. Hiren S Bootcd V10.0. 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. See 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!