简体   繁体   English

将计算机名称和用户名传递到批处理脚本中

[英]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?如何使用带有 Windows 命令的批处理文件来执行此操作?

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我需要获取原始文件名,后跟当前系统日期时间,然后是计算机名称和登录用户

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以下是如何创建在文件名中有效的格式日期的示例: 在 Windows 批处理脚本中格式化日期和时间

%time% is the variable for time %time%是时间变量
%computername% is the variable for the computer name %computername%是计算机名称的变量
%username% is the variable for user name. %username%是用户名的变量。

You should be able to use those to get all of the data you need to name the file.您应该能够使用它们来获取命名文件所需的所有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM