Passing computer name and username to batch script

I copy a file from one folder to another folder. I would like to name the target file as follows:

filename-currentdatetime-computername-username.txt 

How to do this using a batch file with Windows commands?

I need to get the original file name, followed by the current system time, and then the computer and the user who is registered

+6
source share
2 answers

If you do not need the exact date format, this command will copy the file and will include the name of the date, time, user and machine in the name of the target file.

 copy myFile.txt myFile-%date%_%time%-%computername%-%username%.txt 

The date and time will be in the default date format for your OS. Remember that some date formats may contain characters that are not allowed in file names.

To make the command portable, you need to specify the format yourself. Here are ways to create format dates that are valid in file names: Date and time format in a Windows script package

+10
source

%time% - time variable. %computername% is a variable for the computer name
%username% is a variable for the username.

You can use them to get all the necessary data to name the file.

+7
source

Source: https://habr.com/ru/post/895174/


All Articles