简体   繁体   English

如何在 windows 命令行上使用单个命令创建多个文件?

[英]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...它能做什么...

  • generates a number range生成一个数字范围
  • pipes each number to the New-Item cmdlet将每个数字通过管道传递给New-Item cmdlet
  • uses the -f string format operator to build the file name使用-f字符串格式运算符来构建文件名
  • creates the file创建文件

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 for New-Item , which is useful to substitute interactively.注意: niNew-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

Some tips一些技巧

  • If you want to suppress the output, you can pipe or redirect the output.如果要抑制 output,可以 pipe 或重定向 output。 Add | Out-Null添加| Out-Null | Out-Null or | Out-Null
    > $null to either example above (errors and other streams will still show as written). > $null到上述任一示例(错误和其他流仍将显示为已写入)。
  • Both commands support fully-qualified paths and relative paths.这两个命令都支持完全限定路径和相对路径。 You can provide a single string if you are only creating one item.如果您只创建一个项目,则可以提供单个字符串。
  • You can add as many array elements as you would like, just separate each element in the array with a comma , .您可以添加任意数量的数组元素,只需用逗号分隔数组中的每个元素, You can also provide an array variable instead if you need.如果需要,您还可以提供一个数组变量。

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

相关问题 我可以使用单个“复制”命令在Windows命令行上复制多个命名文件吗? - Can I copy multiple named files on the Windows command line using a single “copy” command? 如何从Windows命令行创建共享文件夹? - How can I create a shared folder from the Windows command line? 如何在 Windows 的命令行中创建一个空文件? - How can I create an empty file at the command line in Windows? 如何将文件移动到Windows命令行中文件创建日期的目录中? - How do I move files to a directory that's the create date of the files in Windows Command Line? 在单个Windows命令中移动多个文件 - Move multiple files within in a single Windows command Windows命令行:如何在没有位置的情况下列出所有文件夹中的所有文件只是文件列表 - Windows Command line: how can I list all files in all folders without the location just a list of files 如何使用for循环通过Windows 10命令行创建许多文件? - How to create many files through the windows 10 command line using a for-loop? 我可以通过命令行在 Windows 中创建雪花 ODBC DSN 吗? - Can I create a Snowflake ODBC DSN in Windows via the command line? 如何使用Windows命令行将前缀删除到文件中 - How to remove prefixes into files using the Windows command line 如何从命令行在 Windows 上运行 .class 文件? - How do I run .class files on windows from command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM