简体   繁体   中英

PowerShell - copying file X times and changing their names

Can you help and tell if there is single/as short as possible command in PowerShell that would copy file a given number of times?

For example:

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

in the same folder.

There are several ways of doing this, this is a quite simple one using a foreach-object loop. Note that, this will work only if you're at the same location as your pdf file.

If you're in doubt, use the full path of the file.

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                                                                  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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