user name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
macoga
Posts: 20
Joined: 03 Oct 2021 09:35

user name

#1 Post by macoga » 03 Oct 2021 09:39

Hello community, I need a command to know the user name of each computer I check, please!!!!

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: user name

#2 Post by aGerman » 03 Oct 2021 12:56

A computer doesn't have a user name. A user might be logged in on a computer. How do you "check" a computer? Remotely (if so, how)? Sitting in front of it?

Steffen

macoga
Posts: 20
Joined: 03 Oct 2021 09:35

Re: user name

#3 Post by macoga » 03 Oct 2021 13:47

Hello agerman, maybe I didn´t explain well.

to make a script that list how many files I have in all the folders in my desktop, I need a first line:

"drive=C:\Users\Anonimo\desktop"

Anonimo is the users name is this case, but if I check another computer I need to change the user name, that is why I thought of a script to do it automatically.

thanks

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: user name

#4 Post by aGerman » 03 Oct 2021 13:57

What about the predefined %userprofile% variable? So, "%userprofile%\desktop" expands to the desktop path of the currently logged-in user. There are a couple more predefined variables (like %username%, to answer your initial question). Run SET in a command prompt to see all of them.

Steffen

macoga
Posts: 20
Joined: 03 Oct 2021 09:35

Re: user name

#5 Post by macoga » 03 Oct 2021 14:33

Sorry Steffen, so the first line of the scrip to ask for the username or userprofile will be?

set C:\users\%username%\desktop an so on?
Last edited by macoga on 03 Oct 2021 14:52, edited 1 time in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: user name

#6 Post by aGerman » 03 Oct 2021 14:43

Why do you want to ask for the user name? I've been talking about pre-defined variables. They already have the right values.
Again:
- open a cmd window
- type SET
- hit the Enter key
- see what variables are predefined

Steffen

macoga
Posts: 20
Joined: 03 Oct 2021 09:35

Re: user name

#7 Post by macoga » 03 Oct 2021 15:05

say if I have a program or a script in a pendrive that takes me to the desktop of the computer I am checking, then at the desktop I want to do copy, move or list, right?

if "%drive%"=="1" (set "drive=C:\Users\Anonimo\desktop"

Then when I want to do another computer there will be a diferent owner or user, so the program should ask first who is the user and change the line above.

that clear the doubt?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: user name

#8 Post by aGerman » 03 Oct 2021 15:10

Code: Select all

if "%drive%"=="1" (set "drive=%userprofile%\desktop")
%userprofile% expands to C:\Users\Anonimo if you're logged in on your PC, and to C:\Users\Steffen if I'm logged in on my PC. It's really that simple.

Steffen

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: user name

#9 Post by AR Coding » 03 Oct 2021 15:15

%userprofile%\Desktop will automaticly expand to the user thats logged ins desktop folder

Edit: @agerman: i didnt see your post when i posted mine

macoga
Posts: 20
Joined: 03 Oct 2021 09:35

Re: user name

#10 Post by macoga » 04 Oct 2021 09:18

Thanks for the help, there is always a first time of doing this kind of script, that I think they are very usefull if we know how to do them. Now that you indicated the username, my inicial idea was to somehow collect the gigas used in all the folders in each partition in a quick way, not selecting the folders and look at the properties, then change to a diferent partition to do the same, you understand? then the information should be listed in a txt form.

Here is a beginning, you will see my ignorance

Code:
@echo off
color 0A
CLS
:MENU
ECHO.
ECHO Desktop C: - 1
ECHO D: - 2
ECHO E: - 3
ECHO F: - 4
ECHO G: - 5
ECHO H: - 6
ECHO I: - 7
ECHO SALIR - 8
ECHO.
set drive=
set /p drive= HOW MANY GIGABYTES ARE IN EACH PARTITION?
ECHO.
if "%drive%"=="1" (set "drive=%userprofile%\desktop")
)else if "%drive%"=="2" (set "drive=D:"
)else if "%drive%"=="3" (set "drive=E:"
)else if "%drive%"=="4" (set "drive=F:"
)else if "%drive%"=="5" (set "drive=G:"
)else if "%drive%"=="6" (set "drive=H:"
)else if "%drive%"=="7" (set "drive=I:"
)else if "%drive%"=="8" (goto :eof
) else goto :menu


md %drive%\GIGA
if not exist %drive%\GIGA md %drive%\GIGA
for %%x in %drive% do LIST GIGA.TXT >nul 2>nul /-y %drive%\*.%%x %drive%\GIGA

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: user name

#11 Post by atfon » 04 Oct 2021 10:30

As a quick tip, this will get you a list of volume drive letters on their size in bytes on a given system:

Code: Select all

wmic logicaldisk get name, size
You can find other posts on this forum about converting from bytes to gigabytes.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: user name

#12 Post by aGerman » 04 Oct 2021 10:41

Working with sizes >=2 GB is much more complicated than you may think.
Drives:
viewtopic.php?p=60676#p60676

Folders:
(to calculate the returned bytes into GB you have to use the same :B2Gib subroutine like in the linked post)

Code: Select all

@echo off &setlocal
set "folder=%userprofile%\Desktop"
for /f "tokens=1,2 delims=: " %%a in ('robocopy "%folder%" "%folder%" /L /S /NJH /BYTES /FP /NC /NDL /NFL /TS /XJ /R:0 /W:0') do if /i "%%a"=="Bytes" set "size=%%b"
echo %size% bytes
pause
Steffen

macoga
Posts: 20
Joined: 03 Oct 2021 09:35

Re: user name

#13 Post by macoga » 10 Oct 2021 20:35

Hello there to Steffen, AR Coding and Atfon:
You know the line that you guys suggested

if "%drive%"=="1" (set "drive=C:\Users\%userprofile%\desktop")

Worked for my computer, And I am running windows10, but I got to check another computer with windows 7, and the program didn´t do a thing, investigating what was the user, it showed me in Spanish:

C:\Usuarios\usuario\escritorio
which mean: C:\users\user\desktop

So I changed the line to: C:\Users\user\desktop" but did a folder in C:\users\user\desktop NOT on the desktop and did not list the gigas used by the folders on the desktop.

Would there be a command that will take the user name doesn´t matter where it is, and do the calculation and put it on a list?

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: user name

#14 Post by ShadowThief » 10 Oct 2021 20:59

macoga wrote:
10 Oct 2021 20:35
Hello there to Steffen, AR Coding and Atfon:
You know the line that you guys suggested

if "%drive%"=="1" (set "drive=C:\Users\%userprofile%\desktop")

Worked for my computer, And I am running windows10, but I got to check another computer with windows 7, and the program didn´t do a thing, investigating what was the user, it showed me in Spanish:

C:\Usuarios\usuario\escritorio
which mean: C:\users\user\desktop

So I changed the line to: C:\Users\user\desktop" but did a folder in C:\users\user\desktop NOT on the desktop and did not list the gigas used by the folders on the desktop.

Would there be a command that will take the user name doesn´t matter where it is, and do the calculation and put it on a list?
It's just %USERPROFILE%, not C:\Users\%USERPROFILE%. The %USERPROFILE% variable already includes the C:\Users bit.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: user name

#15 Post by Squashman » 10 Oct 2021 22:31

How many times do we need to tell you to open up a command prompt and type SET. This will help you understand what all those predefined system variables are.

Post Reply