[英]Powershell Copy-Item not keeping folder structure
I have the below script to copy data from a local folder to a remote folder created with the current date.我有以下脚本将数据从本地文件夹复制到使用当前日期创建的远程文件夹。 However the files are copying but the folder structure is not.
但是,文件正在复制,但文件夹结构不是。
$Date = (Get-Date).ToString("MMddyyyy"),$_.Extension
$Source = "E:\Folder1\\*"
$Dest = "\\Server\Share\Folder2"
$Username = "Username"
$Password = ConvertTo-SecureString "Password" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential($Username, $Password)
Remove-PSDrive -Name T
Start-Sleep -s 1
New-PSDrive -Name T -PSProvider FileSystem -Root $Dest -Credential $mycreds -Persist
if (!(Test-Path "T:\$Date"))
{
md -Path "T:\$Date"
}
Get-ChildItem -Path $Source -Recurse | % { Copy-Item -Path $_ -Destination "T:\$Date" -Container -Force -Verbose }
Could anyone advise where I am going wrong here?谁能告诉我这里哪里出错了?
Thank you.谢谢你。
Nice script, I think we can get this sorted in no time! 不错的脚本,我想我们可以立即得到排序!
The reason this is failing is in this step right here: 失败的原因就在此步骤中:
Get-ChildItem -Path $Source -Recurse
The -Recurse
switch is causing you pain. -Recurse
开关使您感到痛苦。 To illustrate why this is, I created a simple folder structure. 为了说明原因,我创建了一个简单的文件夹结构。
When you run Get-ChildItem -Path $Source -Recurse
alone, you'll get a recursive listing of all files in the $Source
path, like so: 单独运行
Get-ChildItem -Path $Source -Recurse
时,将在$Source
路径中获得所有文件的递归列表,如下所示:
PS C:\temp\stack> Get-ChildItem -Recurse
Directory: C:\temp\stack
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/4/2017 10:50 AM Source
Directory: C:\temp\stack\Source
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/4/2017 10:57 AM 1
d----- 8/4/2017 10:57 AM 2
d----- 8/4/2017 10:57 AM 3
d----- 8/4/2017 10:57 AM 4
Directory: C:\temp\stack\Source\1
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 8/4/2017 10:57 AM 20 Archive.rar
-a---- 8/4/2017 10:56 AM 0 File01.bmp
Well, in the next step of your script, you pipe that output over to Copy-Item
for every single file. 好吧,在脚本的下一步中,您将通过管道
Copy-Item
每个文件的输出传递到Copy-Item
。
You're basically expressly telling PowerShell 'take this folder and all of it's subfolders and everything, and dump them all in this one folder, ignoring the folder structure' 您基本上是在明确地告诉PowerShell“将这个文件夹及其所有子文件夹以及所有内容都放到一个文件夹中,而忽略文件夹结构”
What you really want to do is simply move the -Recurse
parameter over to Copy-Item
, and you're done :) 您真正想要做的只是将
-Recurse
参数移至Copy-Item
,就可以完成了:)
Get-ChildItem -Path $Source |
Copy-Item -Destination "T:\$Date" -Container -Recurse -Force -Verbose
Hope that helps, have a nice 🎃day! 希望对您有帮助,祝您度过愉快的一天!
This question is a few years old, but in case someone arrives here like I did...这个问题已经有几年了,但万一有人像我一样到达这里......
In another post, a user suggested using robocopy .在另一篇文章中,用户建议使用 robocopy 。 It worked great for me:
它对我很有用:
robocopy "source/folder" "target/folder" "*.file_extension" /s
This command copies from the source folder the files that match the given filter, replicating the folder structure in the destination folder.此命令从源文件夹复制与给定过滤器匹配的文件,复制目标文件夹中的文件夹结构。
In this case, it would be used like:在这种情况下,它将像这样使用:
robocopy "E:\Folder1\" "\\Server\Share\Folder2" "*" /s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.