Page 1 of 1

Command to find "friendly" username

Posted: 08 Mar 2013 18:07
by happiness
Hi,

I'm very new to using batch files but they do exactly what I want, easily!

I'm looking for an extension of the %USERNAME% command. I'm on a domain and our usernames are "firstname.surname" so that's what the %USERNAME% command returns.

When I look in the Start menu, I seen "Surname, Firstname" below the user's account picture. Is there a command that will return this name? If so, can it be manipulated to just return "Firstname"?

Many thanks in advance (and sorry if this sounds very simple/stupid).

Dave

Re: Command to find "friendly" username

Posted: 08 Mar 2013 18:28
by abc0502
Your %username% is returned with a dot, so you can use it as a delims in a for command to get the first part only, the second part only or first and second with any order you want.

Code: Select all

@Echo OFF
SET "Name=%userName%"
For /F "tokens=1-2 delims=." %%A In ("%Name%") Do (
        Echo First Part is : %%A
        Echo Second Part is : %%B
)
Pause
In this for command %%A represent the first part "before the dot" and %%B represent the second.
change them as you like and put them in any order, you can also assign them to a variables and use them later in your code. :)

Re: Command to find "friendly" username

Posted: 08 Mar 2013 18:58
by happiness
Awesome, worked a treat! Thanks!

You've said I can
assign them to a variable and use them later in your code
... How?? :wink:

Also, can I make the first letter of the name capitalised? I.e. dave to Dave?

Thanks again.

Re: Command to find "friendly" username

Posted: 08 Mar 2013 19:24
by abc0502
Edited : Fixed a problem in the function

hi, using command "set" to set the result to a variable, for example:

Code: Select all

SET "NewUserName=%%B.%%A"

This command must be in the for command.
after that any time you can use the "%NewUserName%" in you code when you need it.

And to capitalize the result, you will need more code, there is a function here in DosTips.com (here) that can do that you will have to add it to your code after the "exit" command

This is a modified one from the above function,

Code: Select all

:ToCamelCase <String> <Optional_Variable>
Set "ToCamelCaseString=%~1"
if not defined ToCamelCaseString EXIT /b
REM make all lower case
for %%a in ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i"
            "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r"
            "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z" "?=?"
         ) do (
    call set "ToCamelCaseString=%%ToCamelCaseString:%%~a%%"
)
call set "ToCamelCaseString= %%ToCamelCaseString%%"
REM make first character upper case
for %%a in (" a=A" " b=B" " c=C" " d=D" " e=E" " f=F" " g=G" " h=H" " i=I"
            " j=J" " k=K" " l=L" " m=M" " n=N" " o=O" " p=P" " q=Q" " r=R"
            " s=S" " t=T" " u=U" " v=V" " w=W" " x=X" " y=Y" " z=Z" " ?=?"
         ) do (
    call set "ToCamelCaseString=%%ToCamelCaseString:%%~a%%"
)
call set "ToCamelCaseString=%%ToCamelCaseString: =%%"
(
IF "%~2" NEQ "" SET "%~2=%ToCamelCaseString%"
IF "%~2" EQU "" Echo %ToCamelCaseString%
)
GOTO :EOF


and you code should be like this ( i here passed the names in the order i need to the function to skip 1 step )
you can first set the name to a variable then call the function and pass that variable to it and provide a new variable name to hold the new result.:

Code: Select all

@Echo OFF
SET "Name=%userName%"
For /F "tokens=1-2 delims=." %%A In ("%Name%") Do (
        Rem here we pass the result to the function to make all in one step
        call :ToCamelCase "%%B.%%A" "NewUserName"
)

Rem display the result
Echo %NewUserName%
Pause
Exit

Rem Functions after this line
:ToCamelCase <String> <Optional_Variable>
Set "ToCamelCaseString=%~1"
if not defined ToCamelCaseString EXIT /b
REM make all lower case
for %%a in ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i"
            "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r"
            "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z" "?=?"
         ) do (
    call set "ToCamelCaseString=%%ToCamelCaseString:%%~a%%"
)
call set "ToCamelCaseString= %%ToCamelCaseString%%"
REM make first character upper case
for %%a in (" a=A" " b=B" " c=C" " d=D" " e=E" " f=F" " g=G" " h=H" " i=I"
            " j=J" " k=K" " l=L" " m=M" " n=N" " o=O" " p=P" " q=Q" " r=R"
            " s=S" " t=T" " u=U" " v=V" " w=W" " x=X" " y=Y" " z=Z" " ?=?"
         ) do (
    call set "ToCamelCaseString=%%ToCamelCaseString:%%~a%%"
)
call set "ToCamelCaseString=%%ToCamelCaseString: =%%"
(
IF "%~2" NEQ "" SET "%~2=%ToCamelCaseString%"
IF "%~2" EQU "" Echo %ToCamelCaseString%
)
GOTO :EOF

The "NewUserName" now will hold the name with a capital size.

I explained it a bit fast if you need any explanation in any part of the code, just ask :)

Re: Command to find "friendly" username

Posted: 08 Mar 2013 19:28
by abc0502
Sorry :oops: there is a small problem in the function it won't work now i will edit my previous post, i copied it from another batch i have and forgot to edit it to work with you

fixed the problem

Re: Command to find "friendly" username

Posted: 08 Mar 2013 19:37
by happiness
Holy hell! All of that to do 1 little thing!?

Thank you so much!!

Re: Command to find "friendly" username

Posted: 08 Mar 2013 19:42
by abc0502
you are welcome, any question just ask. :)