$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 \确实存在,如果不存在,是否可以使用单个命令创建“c:\ out \ 1 XLS \ 2010_04_22 \”。Powershell 2复制项目,它创建一个文件夹如果不存在
$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 \确实存在,如果不存在,是否可以使用单个命令创建“c:\ out \ 1 XLS \ 2010_04_22 \”。Powershell 2复制项目,它创建一个文件夹如果不存在
是的,请添加-Force
参数。
copy-item $from $to -Recurse -Force
在PowerShell 2.0中,它仍然不可能得到复制,项目cmdlet创建目标文件夹,你需要这样的代码:如果您在使用-Recurse
$destinationFolder = "C:\My Stuff\Subdir"
if (!(Test-Path -path $destinationFolder)) {New-Item $destinationFolder -Type Directory}
Copy-Item "\\server1\Upgrade.exe" -Destination $destinationFolder
Copy-Item将创建目标源结构的所有子文件夹,但它不会创建实际的目标文件夹,即使使用-Force也是如此。
感谢您的观察。我有一些不一致的复制行为;有时它复制了一个子文件夹,有时它没有。确保目标文件夹存在后,这种不一致性消失了。 – 2016-11-29 07:31:51
当目标文件夹不存在时,我也会遇到错误的行为 – vicancy 2017-04-26 01:39:38
在PowerShell 3及以上版本中,我使用Copy-Item和New-Item。
copy-item -Path $file -Destination (new-item -type directory -force ("C:\Folder\sub\sub\" + $newSub)) -force -ea 0
我还没有在版本2
这对于本地复制非常有用,谢谢!不是当使用'-ToSession'命令时:'( – Hicsy 2017-12-08 03:28:49
我已经在这里两次绊倒了,这一次是一个独特的情况,即使我用沟里我copy-item
想后我使用的解决方案试了一下。
只有完整路径的文件列表并且在大多数情况下文件没有扩展名。 -Recurse -Force
选项不适用于我,所以我放弃copy-item
函数,并使用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
}
我最喜欢使用.Net [IO.DirectoryInfo]类,它负责处理一些逻辑。我实际上使用它来解决很多类似的脚本挑战。它有一个.Create()方法,用于创建不存在的目录,如果它们存在,则不会出错。
由于这仍然是一个两步骤的问题,我使用foreach别名来保持简单。对于单个文件:
[IO.DirectoryInfo]$to |% {$_.create(); cp $from $_}
至于你的多文件/目录相匹配,我会通过xcopy使用RoboCopy。从“*”从删除,只是使用:
RoboCopy.exe $from $to *
您仍可以添加/ R(递归),/ E(递归包括空),并有50个其他有用的交换机。
编辑:回想一下,这是简洁的,但不是很可读,如果你不经常使用的代码。通常我把它一分为二,就像这样:
([IO.DirectoryInfo]$to).Create()
cp $from $to
此外,DirectoryInfo的是的FileInfo的Parent属性的类型,因此,如果您$到是一个文件,你可以使用它们一起:
([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
}
}
欢迎来到堆栈溢出!感谢您的代码片段,它可能会提供一些即时帮助。一个正确的解释[会大大提高](// meta.stackexchange.com/q/114762)通过展示*为什么*这是一个很好的解决方案,它将使它对未来的读者提供类似的,但不完全相同的问题更有用,请为你的答案添加解释,表明适用的是什么限制和假设。 – 2017-06-02 08:33:13
PowerShell是那么容易当一个人知道它:) – stej 2010-04-23 08:41:56
,对于整个目录的工作,但如果我只是复制单个文件(这样我就可以有一个ST的ATU)?当c:\ test2不存在时,如何将c:\ test1 \ file.txt复制到c:\ test2 \ file.txt中?这是我真正需要知道如何去做的。谢谢, Gary – 2010-12-16 16:46:30
我遇到了同样的问题,并通过调用New-Item first来解决: 'New-Item -Force $ to' 'Copy-Item -Force $ from $ to' – rjschnorenberg 2012-04-26 19:52:44