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
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.
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.
Likewise, any custom GUI element can be added to your Windows batch files by calling required methods from the . NET framework.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With