1

I'm running a command line script on multiple PC's and i'm trying to save username as a file name so i can see who's information i'm viewing later on.

In the command line script i run Whoami and i'd like to save it as "user"."file type". I'm trying to do this in a command line script because I always do it manually in command line and am trying to automate this process so I can do it faster.

If you know how to do it in a better way do share.

Carlos Cavero
  • 3,011
  • 5
  • 21
  • 41
Filip Radil
  • 11
  • 1
  • 3
  • 1
    Perhaps these threads are interesting for you: [Assign Command output to Variable in Batch file](https://stackoverflow.com/q/16203629), [Set the value of a variable with the result of a command in a Windows batch file](https://stackoverflow.com/q/889518), [How to set commands output as a variable in a batch file](https://stackoverflow.com/q/6359820). – aschipfl Mar 18 '19 at 12:39
  • ty, now i have username in a variable, how do i put it as a file name? – Filip Radil Mar 18 '19 at 13:14
  • like `%variable%.txt`. There is also the system variable `%username%`, which may do for you. – Stephan Mar 18 '19 at 14:54
  • 1
    `DIR >"%USERNAME%.txt"` or `ECHO>"%USERNAME%.txt" this information` There are many options with redirection of the output. https://ss64.com/nt/syntax-redirection.html – lit Mar 18 '19 at 15:12
  • >%USERNAME%.txt worked in the end. Ty – Filip Radil Mar 19 '19 at 11:36

2 Answers2

0

whoami > test.txt

tells it to go to a file and "test.txt" will be generated wherever your CMD CurDir is.

shadow2020
  • 1,315
  • 1
  • 8
  • 30
0

You may use Windows Environment variables %USERNAME% and possibly %USERDOMAIN% if the domain is needed.

%USERNAME% does not return the domain by itself.

Full list of standard environment variables: How-to: Windows Environment Variables

Use these in the command as needed. For example:

dir > %USERNAME%.txt

If you need the domain in there:

dir > %USERDOMAIN%_%USERNAME%.txt

(using _ to separate domain and username instead of \ since filename cannot contain \)

Remember to use >> instead of > if you don't want the file to be overwritten each time the command is run.

You may want to direct errors and standard output as needed: Redirecting error messages from Command Prompt: STDERR/STDOUT

slayernoah
  • 4,382
  • 11
  • 42
  • 73