Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a computer name and username into a batch script

I am copying a file from one folder into another folder. I would like to name the destination file in this specific way:

filename-currentdatetime-computername-username.txt

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

I need to get the original filename followed by the current system date time and then the computernaem and the user that is logged on

like image 830
Alex Gordon Avatar asked Aug 16 '11 17:08

Alex Gordon


People also ask

What does %% mean in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What does %1 do in batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

Can you make a GUI in batch?

Likewise, any custom GUI element can be added to your Windows batch files by calling required methods from the . NET framework.


1 Answers

If you don't care about the exact date format, this command will copy a file and include the date, time, user and machine name in the target file name.

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

The date and time will be in the default date format of your OS. Beware that some date formats can contain characters which are not allowed in file names.

To make the command portable you need to specify the format yourself. Here are exampled of how to create format dates that are valid in file names: Format date and time in a Windows batch script

like image 72
kapex Avatar answered Sep 21 '22 15:09

kapex