Dou*_*las 9 windows-7 powershell
我有一个包含一些文件的目录 Source,我想将其复制到文件夹 Destination。目标可能存在,并且其中可能已经有文件。任何与 Source 中的文件同名的文件都应该被覆盖。
如果我在 Powershell 中运行它:
Copy-Item Source Destination -Force -Recurse
Copy-Item Source Destination -Force -Recurse
Copy-Item Source Destination -Force -Recurse
Run Code Online (Sandbox Code Playgroud)
然后第一行创建文件夹.\Destination并将其复制.\Source到其中,这是我下次要重复的内容。但是,第二行复制.\Source到新.\Destination文件夹(正在创建.\Destination\Source),然后第三行.\Destination\Source再次覆盖。
我怎样才能让它一直像第一种情况一样?也就是说,覆盖.\Destination而不是复制进去?
所以问题是
cp -r -fo foo bar
Run Code Online (Sandbox Code Playgroud)
仅当bar不存在时才有效并且
cp -r -fo foo/* bar
Run Code Online (Sandbox Code Playgroud)
仅在bar存在时才有效。所以要解决这个问题,你需要bar在做任何事情之前确保存在
md -f bar
cp -r -fo foo/* bar
Run Code Online (Sandbox Code Playgroud)
Steven Penny 的回答https://superuser.com/a/742719/126444不会删除目标目录的原始内容,只是附加到它。我需要用源内容完全替换目标文件夹并创建了 2 个函数:
function CopyToEmptyFolder($source, $target )
{
DeleteIfExistsAndCreateEmptyFolder($target )
Copy-Item $source\* $target -recurse -force
}
function DeleteIfExistsAndCreateEmptyFolder($dir )
{
if ( Test-Path $dir ) {
#http://stackoverflow.com/questions/7909167/how-to-quietly-remove-a-directory-with-content-in-powershell/9012108#9012108
Get-ChildItem -Path $dir -Force -Recurse | Remove-Item -force -recurse
Remove-Item $dir -Force
}
New-Item -ItemType Directory -Force -Path $dir
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30249 次 |
| 最近记录: |