I'm trying to get my current username by registry using command line. But my username has two words between a space. Ex.: "Cat Dog". Well, if i use a space as delimiter, it only prints 'Cat', if i put tokens=3
it prints also the same because there's a space between these two words.
I'm doing it to use in many computers, so i can't determine the exactly username of each one. How can i delimiter from the first word of the token number 3
until the end not mattering how much spaces it has ?
@echo off
for /f "tokens=3" %%a in ('reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\SessionData\1 /v LoggedOnUser') do (
set "user=%%a")
set "user=%user:.\=%"
echo %user%
pause
CodePudding user response:
Is there any reason you couldn't use the whoami command? For example:
for /f "tokens=*" %%g in ('whoami') do set "user=%%g"
CodePudding user response:
Windows has a system variable %username% in the environment
echo %username%