windows 将计算机名称和用户名传递到批处理脚本中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7082484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing a computer name and username into a batch script
提问by l--''''''---------''''''''''''
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
我需要获取原始文件名,然后是当前系统日期时间,然后是计算机名和登录用户
回答by kapex
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 批处理脚本中的格式日期和时间
回答by MDMarra
%time%
is the variable for time%computername%
is the variable for the computer name%username%
is the variable for user name.
%time%
是时间%computername%
变量 是计算机名称%username%
变量 是用户名变量。
You should be able to use those to get all of the data you need to name the file.
您应该能够使用这些来获取命名文件所需的所有数据。