繁体   English   中英

在PowerShell中复制多个文件夹时,如何更改文件夹的名称?

[英]How to change name of the folders when copying multiple folders in PowerShell?

这是我现在的代码:

[int]$NumberOfProfiles = Read-Host "Enter the number of Portable Firefox profiles You need"
$WhereToWrite = Read-Host "Enter the full path where You'd like to install the profiles" 
$Source = Get-Location 
$FolderName = "JonDoFoxPortable"
$SourceDirectory = "$Source\$Foldername"
$Copy = Copy-Item $SourceDirectory $WhereToWrite -Recurse -Container
while ($NumberOfProfiles -ge 0) {$Copy; $NumberOfProfiles--}

正如您现在所看到的,它只是覆盖了文件夹,但是我需要它复制在$NumberOfProfiles声明的一定数量的文件夹(例如$NumberOfProfiles = 10 ),它使JonDoFoxPortable1,JonDoFoxPortable2 ... JonDoFoxPortable10 ...

这样的事情应该起作用:

while ($NumberOfProfiles -ge 0) {
  $DestinationDirectory = Join-Path $WhereToWrite "$Foldername$NumberOfProfiles"
  Copy-Item $SourceDirectory $DestinationDirectory -Recurse -Container
  $NumberOfProfiles--
}

或者,也许更简单,像这样:

0..$NumberOfProfiles | % {
  $DestinationDirectory = Join-Path $WhereToWrite "$Foldername$_"
  Copy-Item $SourceDirectory $DestinationDirectory -Recurse -Container
}

这应该为您做:

更换:

while ($NumberOfProfiles -ge 0) {$Copy; $NumberOfProfiles--}

与:

(0..$NumberOfProfiles) | % {
    Copy-Item $SourceDirectory "$WhereToWrite$_" -Recurse -Container
}

这将从0到$NumberOfProfiles循环,并复制到名为$WhereToWrite的位置,并将迭代编号附加到$_

暂无
暂无

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

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