首页 > 解决方案 > PowerShell - 复制文件 X 次并更改其名称

问题描述

你能帮忙告诉PowerShell中是否有单个/尽可能短的命令可以复制文件给定的次数吗?

例如:

file.pdf to file1.pdf, file2.pdf, file3.pdf 

在同一个文件夹中。

标签: powershell

解决方案


有几种方法可以做到这一点,这是一种使用foreach-object循环的非常简单的方法。请注意,这仅在您与pdf文件位于同一位置时才有效。

如果您有疑问,请使用文件的完整路径。

PS /~> $null > 'test.pdf'

PS /~> ls

Mode                LastWriteTime         Length Name                                                                       
----                -------------         ------ ----                                                                       
-a----         4/8/2021   5:11 PM              2 test.pdf                                                                   

PS /~> 0..10|%{Copy-Item .\test.pdf -Destination ".\test$_.pdf"}

PS /~> ls

Mode                LastWriteTime         Length Name                                                                       
----                -------------         ------ ----                                                                       
-a----         4/8/2021   5:11 PM              2 test.pdf                                                                   
-a----         4/8/2021   5:11 PM              2 test0.pdf                                                                  
-a----         4/8/2021   5:11 PM              2 test1.pdf                                                                  
-a----         4/8/2021   5:11 PM              2 test10.pdf                                                                 
-a----         4/8/2021   5:11 PM              2 test2.pdf                                                                  
-a----         4/8/2021   5:11 PM              2 test3.pdf                                                                  
-a----         4/8/2021   5:11 PM              2 test4.pdf                                                                  
-a----         4/8/2021   5:11 PM              2 test5.pdf                                                                  
-a----         4/8/2021   5:11 PM              2 test6.pdf                                                                  
-a----         4/8/2021   5:11 PM              2 test7.pdf                                                                  
-a----         4/8/2021   5:11 PM              2 test8.pdf                                                                  
-a----         4/8/2021   5:11 PM              2 test9.pdf                                                                  


推荐阅读