Powershell 2副本项目创build一个文件夹如果不存在

$from = "\\something\1 XLS\2010_04_22\*" $to = "c:\out\1 XLS\2010_04_22\" copy-item $from $to -Recurse 

如果c:\ out \ 1 XLS \ 2010_04_22 \确实存在,这将起作用。 如果不存在,是否可以使用单个命令创build“c:\ out \ 1 XLS \ 2010_04_22 \”。

是的,添加-Force参数。

 copy-item $from $to -Recurse -Force 

在PowerShell 2.0中,仍然无法使Copy-Item cmdlet创build目标文件夹,因此需要如下所示的代码:

 $destinationFolder = "C:\My Stuff\Subdir" if (!(Test-Path -path $destinationFolder)) {New-Item $destinationFolder -Type Directory} Copy-Item "\\server1\Upgrade.exe" -Destination $destinationFolder 

如果在复制项目中使用-Recurse,则会在目标中创build源结构的所有子文件夹,但即使使用-Force也不会创build实际的目标文件夹。

在PowerShell 3及以上版本中,我使用Copy-Item和New-Item。

 copy-item -Path $file -Destination (new-item -type directory -force ("C:\Folder\sub\sub\" + $newSub)) -force -ea 0 

我没有在第二版中试过。

我最喜欢的是使用.Net [IO.DirectoryInfo]类,它负责一些逻辑。 我实际上使用它来处理很多类似的脚本挑战。 它有一个.Create()方法创build不存在的目录,如果他们这样做没有错误。

由于这仍然是一个两步问题,我使用foreach别名来保持简单。 对于单个文件:

 [IO.DirectoryInfo]$to |% {$_.create(); cp $from $_} 

至于你的多文件/目录相匹配,我会通过xcopy使用RoboCopy。 删除你的“*”,只需使用:

 RoboCopy.exe $from $to * 

你仍然可以添加/ r(Recurse),/ e(Recurse包含空),还有50个其他有用的开关。

编辑:回想一下,这是简洁的,但不是很可读,如果你不经常使用的代码。 通常我把它分成两部分,就像这样:

 ([IO.DirectoryInfo]$to).Create() cp $from $to 

此外, DirectoryInfoFileInfo的Parent属性的types,所以如果$ to是文件,则可以一起使用它们:

 ([IO.FileInfo]$to).Parent.Create() cp $from $to 
 function Copy-File ([System.String] $sourceFile, [System.String] $destinationFile, [Switch] $overWrite) { if ($sourceFile -notlike "filesystem::*") { $sourceFile = "filesystem::$sourceFile" } if ($destinationFile -notlike "filesystem::*") { $destinationFile = "filesystem::$destinationFile" } $destinationFolder = $destinationFile.Replace($destinationFile.Split("\")[-1],"") if (!(Test-Path -path $destinationFolder)) { New-Item $destinationFolder -Type Directory } try { Copy-Item -Path $sourceFile -Destination $destinationFile -Recurse -Force Return $true } catch [System.IO.IOException] { # If overwrite enabled, then delete the item from the destination, and try again: if ($overWrite) { try { Remove-Item -Path $destinationFile -Recurse -Force Copy-Item -Path $sourceFile -Destination $destinationFile -Recurse -Force Return $true } catch { Write-Error -Message "[Copy-File] Overwrite error occurred!`n$_" -ErrorAction SilentlyContinue #$PSCmdlet.WriteError($Global:Error[0]) Return $false } } else { Write-Error -Message "[Copy-File] File already exists!" -ErrorAction SilentlyContinue #$PSCmdlet.WriteError($Global:Error[0]) Return $false } } catch { Write-Error -Message "[Copy-File] File move failed!`n$_" -ErrorAction SilentlyContinue #$PSCmdlet.WriteError($Global:Error[0]) Return $false } } 

我在这里偶然发现了两次,而这最后一次是一个独特的情况,即使我使用copy-item我想发布我使用的解决scheme。

有一个只有完整path文件的列表,在大多数情况下,文件没有扩展名。 -Recurse -Force选项对我来说不起作用,所以我放弃了copy-itemfunction,并使用xcopy恢复到下面的样子,因为我仍然希望将它保留一行。 最初我与Robocopy绑定,但它显然是寻找一个文件扩展名,因为我的很多没有扩展它认为它是一个目录。

 $filelist = @("C:\Somepath\test\location\here\file","C:\Somepath\test\location\here2\file2") $filelist | % { echo f | xcopy $_ $($_.Replace("somepath", "somepath_NEW")) } 

希望它可以帮助别人。

  $filelist | % { $file = $_ mkdir -force (Split-Path $dest) | Out-Null cp $file $dest }