Page 1 of 1

Need some help with explanation

Posted: 28 Jun 2010 05:50
by Zeratchi
Hello.
I'm a new user, and need some simple assistance with some .BAT explanation.

I am suppose to do a script for work, where I will add users/students with the following command (Norwegian)
10;Førrisdahl;Nathaniel;bjørkelangen skole

And in English

class(10);firstname;lastname;school/location

However, in the end of these commands, you've got these:

class(10);firstname;lastname;school/location %%i %%j

Can anyone here tell me the meaning of the %%i %%j
I somewhat knows how it works, but I don't understand the meaning.
So if someone could explain it with some detail (the whole bit if possible) I would be grateful.

Re: Need some help with explanation

Posted: 28 Jun 2010 10:56
by aGerman
%%i and %%j are dynamic variables of a FOR loop. You should post this loop for a better explanation.

Regards
aGerman

Re: Need some help with explanation

Posted: 28 Jun 2010 15:10
by miskox
Speaking of explanations...

Can somebody please explain these two things:

Code: Select all

set "line=..."


What is the purpose of using a " (Quotation_mark) before the variable name?

and

Code: Select all

set "line=echo.%%..."


What is the purpose of the echo. command (besides echoing an empty line when by itself or ???? )?

Thanks,
Saso

Re: Need some help with explanation

Posted: 28 Jun 2010 16:19
by aGerman
Well, I mostly enclose variable name and value in double quotes. This has a lot of advantages.
copy&paste:

Code: Select all

@echo off &setlocal

set "a=x" & set b=y & echo test:
echo %a%!
echo %b%!
pause

set "a=^>"
set b=^>
echo %a%
pause
echo %b%
pause


The first half shows you the handling of unintended spaces.
The second half shows the handling of special characters. (last ECHO generates a syntax error)


Regarding the dot behind the ECHO command:

Code: Select all

@echo off &setlocal
set "var1=123"
set "var2="
:: var2 is not defined in this case

echo ~~~~~~~~~~~
echo %var1%
echo %var2%
echo ~~~~~~~~~~~
echo.%var1%
echo.%var2%
echo ~~~~~~~~~~~

pause

You see the difference :wink:
So this is an easy way to make sure not to get an unintended ECHO-message.

Regards
aGerman

Re: Need some help with explanation

Posted: 29 Jun 2010 00:56
by Zeratchi
Hello, and thanks for answering :)

This is the full thing I've made, which works in a decent matter, but I cannot see the logic with it all.

Code: Select all

@echo off
REM for /F "eol=; tokens=1,2,3,4,5,* delims=;" %%i in (Bjskole.txt) do echo  "Kull-> %%i Etternavn -> %%j Fornavn -> %%k Brukernavn-> %%l Skole-> %%m"
for /F "eol=; tokens=1,2,3,4,5,* delims=;" %%i in (%1) do  echo dsadd user "cn=%%k %%j,OU=%%i,OU=%%m,OU=Elev,OU=Skole,DC=ahk,DC=no" -fn %%k -ln %%j -desc "Elev %%m skole %%i" -samid %%l -display "%%k %%j" -profile \\adskole\profiler$\%%l -pwd changeme -mustchpwd yes -upn %%l@ahk -loscr SkElev.cmd"
for /F "eol=; tokens=1,2,3,4,5,* delims=;" %%i in (%1) do  echo md \\adskole\Elever$\%%l


Just change words like "Etternavn, fornavn, brukernavn" to "Lastname, firstname, username"
So, I know what it does, but I cannot see the logic.
Like the 1,2,3,4,5,* Why is that needed, where is it linked to.
Or why start with %%j why not %%a.
Why there is already a premade Variable, would be a lot easier to like call the whole thing $0 or something, for example:

name;telephone;email;homearea = $0

Instead of the strangly %%j %%i %%k %%l which i'm using now.
I'm a bit used to linux, so this doesn't make any logical sense :P

Re: Need some help with explanation

Posted: 29 Jun 2010 06:55
by aGerman
Well, you read in a file that is given as parameter (%1). In this file the values are separated by semicolon.
Into the FOR loops you use the semicolon as delimiter to split each line into parts (tokens). It is defined, that the first 5 tokens are returned and the whole rest of the line (*) is in the sixth token. You start with the 1st token in %%i, the 2nd token in %%j, ... , 5th token in %%m and rest of the line in %%n.
BTW if you would write

Code: Select all

for /F "eol=; tokens=1,2,3,4,5,* delims=;" %%a in (%1) do ...

the 1st token would be saved in %%a and the 6th in %%f. I'm also not sure if eol=; is needed, because afaik this should be the default.

I hope you'll understand it. Sorry for my bad English, my native language is German ...


Regards
aGerman