[英]How can I create multiple files using a single command on windows command line?
The command for creating a new file on windows using cmd is:使用 cmd 在 windows 上创建新文件的命令是:
cd.> filename.txt
But how can I create more than one file using a single command?但是如何使用单个命令创建多个文件?
here is one way to generate a series of files with powershell.这是使用 powershell 生成一系列文件的一种方法。 [ grin ] [咧嘴笑]
the code...编码...
1..9 |
ForEach-Object {
New-Item -Path $env:TEMP -Name ('FileNumber_{0}.txt' -f $_) -ItemType 'File'
}
what it does...它能做什么...
New-Item
cmdlet将每个数字通过管道传递给New-Item
cmdlet-f
string format operator to build the file name使用-f
字符串格式运算符来构建文件名truncated output...截断 output...
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2022-02-03 1:10 PM 0 FileNumber_1.txt
[*...snip...*]
-a---- 2022-02-03 1:10 PM 0 FileNumber_9.txt
if you want to get rid of the output to the screen, add $Null =
in front of the New-Item
line.如果你想摆脱 output 到屏幕,添加$Null =
在New-Item
行前面。
With PowerShell you can use New-Item
with an array of filenames.使用 PowerShell,您可以使用带有文件名数组的New-Item
。
Note:
ni
is a built-in alias forNew-Item
, which is useful to substitute interactively.注意:ni
是New-Item
的内置别名,可用于交互替换。
# New-Item
ni fileA.txt, fileB.txt, filec.txt, "file_with_a_${var}_in_it.txt"
You can also use New-Item
to create directories, but a shorter way to do this is to use mkdir
instead, which is a built-in "alias" function for essentially calling New-Item -ItemType Directory
:您也可以使用New-Item
创建目录,但更短的方法是使用mkdir
代替,它是一个内置的“别名” function 用于本质上调用New-Item -ItemType Directory
:
# New-Item -ItemType Directory
mkdir dirA, dirB, dirC
| Out-Null
添加| Out-Null
| Out-Null
or | Out-Null
或> $null
to either example above (errors and other streams will still show as written). > $null
到上述任一示例(错误和其他流仍将显示为已写入)。,
.您可以添加任意数量的数组元素,只需用逗号分隔数组中的每个元素,
。 You can also provide an array variable instead if you need.如果需要,您还可以提供一个数组变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.