User Name With Registry

I was messing around with REGEDIT and managed to
find the DOS command-line options. I’ve always
wanted to get the user name of the person logged
in, so I thought I’d see what I could come up
with. There may be easier ways to do it, but I
couldn’t think of any. Here’s the REGEDIT options:

REGEDIT [/L:system] [/R:user] filename1
REGEDIT [/L:system] [/R:user] /C filename2
REGEDIT [/L:system] [/R:user] /E filename3 [regpath1]
REGEDIT [/L:system] [/R:user] /D regpath2
/L:system       Specifies the location of the SYSTEM.DAT file.
/R:user         Specifies the location of the USER.DAT file.
filename1       Specifies the file(s) to import into the registry.
/C filename2    Specifies the file to create the registry from.
/E filename3    Specifies the file to export the registry to.
regpath1        Specifies the starting registry key to export from.
(Defaults to exporting the entire registry).
/D regpath2     Specifies the registry key to delete.
/S              Specifies “silent” operation when importing
(No “Succesfully Imported” window will appear)

Here’s a batch file that gets the name of the
person logged in to Windows. Note that the name
of this batch file CAN NOT be called “Current User.bat”:

——————————————-
@echo off
start /w regedit /e reg.txt HKEY_LOCAL_MACHINE\System\CurrentControlSet\control
type reg.txt | find “Current User” > “Current#User.bat”
echo set CurrentUser=%%1>”Current User.bat”
call “Current#User.bat”
del “Current?User.bat” > nul
del reg.txt > nul
echo %CurrentUser%
exit
——————————————-

Here’s a batch file that gets the name of the
person logged in to the network. Note that the
name of this batch file CAN NOT be called “username.bat”:

——————————————-
@echo off
start /w regedit /e reg.txt HKEY_LOCAL_MACHINE\Network\Logon
type reg.txt | find “username” > “us#rname.bat”
echo set NetUser=%%1>”username.bat”
call “us#rname.bat”
del “us?rname.bat” > nul
del reg.txt > nul
echo %NetUser%
exit
——————————————-

Iain Hamilton from Scotland noticed that the
returned data comes complete with quotes
around it. The way I used the data, quotes
weren’t a problem, but I’ve got to admit a
general solution should provide the data
without quotes. Iain removed the quotes by
creating a file named after the returned data.
Pretty clever! Here’s Iain’s suggestion (with
his permission):

*******************************************
*******************************************
*******************************************

For example for a user Bert, the batch program
give you an output of currentuser=”Bert”, which
is all very nice but you can’t map shares to a
name in quotes.  The following code below is my
method of converting
currentuser=”Bert”
into
currentuser=Bert
——————————————-
md temp1
cd temp1
:: creates a temp1 directory for the batch file to work in.  Essentially
:: just a directory that doesn’t contain any files.
:: HERES THE IMPORTANT BIT:-
echo hello>%username%
:: echos hello into a file called “Bert”
:: DOS can’t call the file “bert”, so it drops the quotes on each side….
:: …giving you a directory with one file in it  (called bert)
dir /B >c:\workingdirectory\user.txt
:: outputs the data to a file called user.txt in the working directory
cd..
copy fragment.txt+user.txt runme.bat
:: merges your pre-prepared fragment containing the text:
:: set currentuser=
:: with the user.txt into runme.bat
:: runme.bat now contains the string: set currentuser=bert
call runme.bat
:: executes the batch file
deltree temp1
:: just a quick tidy up.
——————————————-
*****************************