Powershell Copy-To - 如果目标目录存在,则复制到现有目录
Powershell Copy-To - If Destination Directory exists, copies into Existing Directory
好的,我有一个简单的 Copy-Item
脚本可以将文件从一个目标文件夹复制到另一个目标文件夹。
发布代码。ps1
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$source,
[Parameter(Mandatory=$true)]
[string]$destination
)
Process {
Copy-Item -Path $source -Destination $destination -Recurse -Force
我 运行 releasecode.ps1
使用以下命令行:
.\releasecode.ps1 -source "C:\test\from" -destination "C:\test\to"
from
文件夹具有以下结构:
.
├── from
├── stain.txt
├── test1.txt
├── folder
| ├── test2.bmp
这正确地复制到(在第一个副本):
.
├── to
├── stain.txt
├── test1.txt
├── folder
| ├── test2.bmpthe
如果我直接 re-ran 它之后,from
文件夹将创建为 `to' 中的一个目录,而不是仅仅覆盖现有结构:
.
├── to
├── stain.txt
├── test1.txt
├── folder
| ├── test2.bmp
├── from
| ├── stain.txt
| ├── test1.txt
| └── folder
| ├── test2.bmp
如果文件和文件夹当前存在,我如何覆盖现有的 to
目录结构。
更多信息
运行 这个在 Windows 盒子上
$PSVersionTable
:
您 运行 陷入了与复制源目录相关的 Copy-Item
陷阱。
如果目标存在并且是一个文件夹,则 cmdlet 会将源复制到目标。
Copy-Item C:\src\a C:\dst\b -Recurse
C:\ C:\
├─dst ├─dst
| └─b | └─b
└─src | └─a
└─a ⇒ | ├─bar.txt
├─bar.txt | └─baz.txt
└─baz.txt └─src
└─a
├─bar.txt
└─baz.txt
如果目标不存在,cmdlet 会复制源作为目标。
Copy-Item C:\src\a C:\dst\b -Recurse
C:\ C:\
├─dst ├─dst
└─src | └─b
└─a | ├─bar.txt
├─bar.txt ⇒ | └─baz.txt
└─baz.txt └─src
└─a
├─bar.txt
└─baz.txt
在 PowerShell 中处理此问题的惯常方法是先确保目标文件夹存在,然后复制源的内容文件夹:
if (-not (Test-Path $destination)) {
New-Item -Type Directory -Path $destination | Out-Null
}
Copy-Item -Path $source\* -Destination $destination -Recurse -Force
或者您可以使用 robocopy
,它没有这个问题:
robocopy C:\src\a C:\dst\b /s
好的,我有一个简单的 Copy-Item
脚本可以将文件从一个目标文件夹复制到另一个目标文件夹。
发布代码。ps1
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$source,
[Parameter(Mandatory=$true)]
[string]$destination
)
Process {
Copy-Item -Path $source -Destination $destination -Recurse -Force
我 运行 releasecode.ps1
使用以下命令行:
.\releasecode.ps1 -source "C:\test\from" -destination "C:\test\to"
from
文件夹具有以下结构:
.
├── from
├── stain.txt
├── test1.txt
├── folder
| ├── test2.bmp
这正确地复制到(在第一个副本):
.
├── to
├── stain.txt
├── test1.txt
├── folder
| ├── test2.bmpthe
如果我直接 re-ran 它之后,from
文件夹将创建为 `to' 中的一个目录,而不是仅仅覆盖现有结构:
.
├── to
├── stain.txt
├── test1.txt
├── folder
| ├── test2.bmp
├── from
| ├── stain.txt
| ├── test1.txt
| └── folder
| ├── test2.bmp
如果文件和文件夹当前存在,我如何覆盖现有的 to
目录结构。
更多信息
运行 这个在 Windows 盒子上
$PSVersionTable
:
您 运行 陷入了与复制源目录相关的 Copy-Item
陷阱。
如果目标存在并且是一个文件夹,则 cmdlet 会将源复制到目标。
Copy-Item C:\src\a C:\dst\b -Recurse C:\ C:\ ├─dst ├─dst | └─b | └─b └─src | └─a └─a ⇒ | ├─bar.txt ├─bar.txt | └─baz.txt └─baz.txt └─src └─a ├─bar.txt └─baz.txt
如果目标不存在,cmdlet 会复制源作为目标。
Copy-Item C:\src\a C:\dst\b -Recurse C:\ C:\ ├─dst ├─dst └─src | └─b └─a | ├─bar.txt ├─bar.txt ⇒ | └─baz.txt └─baz.txt └─src └─a ├─bar.txt └─baz.txt
在 PowerShell 中处理此问题的惯常方法是先确保目标文件夹存在,然后复制源的内容文件夹:
if (-not (Test-Path $destination)) {
New-Item -Type Directory -Path $destination | Out-Null
}
Copy-Item -Path $source\* -Destination $destination -Recurse -Force
或者您可以使用 robocopy
,它没有这个问题:
robocopy C:\src\a C:\dst\b /s