Command to find "friendly" username

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
happiness
Posts: 6
Joined: 08 Mar 2013 18:02
Location: Dinnington, Newcastle upon Tyne, UK
Contact:

Command to find "friendly" username

#1 Post by happiness » 08 Mar 2013 18:07

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

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Command to find "friendly" username

#2 Post by abc0502 » 08 Mar 2013 18:28

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. :)

happiness
Posts: 6
Joined: 08 Mar 2013 18:02
Location: Dinnington, Newcastle upon Tyne, UK
Contact:

Re: Command to find "friendly" username

#3 Post by happiness » 08 Mar 2013 18:58

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.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Command to find "friendly" username

#4 Post by abc0502 » 08 Mar 2013 19:24

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 :)
Last edited by abc0502 on 08 Mar 2013 19:30, edited 1 time in total.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Command to find "friendly" username

#5 Post by abc0502 » 08 Mar 2013 19:28

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

happiness
Posts: 6
Joined: 08 Mar 2013 18:02
Location: Dinnington, Newcastle upon Tyne, UK
Contact:

Re: Command to find "friendly" username

#6 Post by happiness » 08 Mar 2013 19:37

Holy hell! All of that to do 1 little thing!?

Thank you so much!!

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Command to find "friendly" username

#7 Post by abc0502 » 08 Mar 2013 19:42

you are welcome, any question just ask. :)

Post Reply