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

时间:2014-06-18 10:17:43

标签: powershell copy

这是我现在的代码:

[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。

2 个答案:

答案 0 :(得分:1)

这样的事情应该有效:

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
}

答案 1 :(得分:1)

这应该适合你:

取代:

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

使用:

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

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