简体   繁体   English

powershell获取项目| 复制项目不只保留一个文件夹的文件夹结构

[英]powershell get-item | copy-item not keeping folder structure for just one folder

I am trying to move all folders/subfolders/subfiles from one folder on a server to another using a powershell script, while keeping the same file structure throughout. 我试图使用Powershell脚本将所有文件夹/子文件夹/子文件从服务器上的一个文件夹移动到另一个文件夹,同时始终保持相同的文件结构。 To do this I am using Get-childItem | 为此,我正在使用Get-childItem | copy-item. 复制项目。 All folders, subfolders, and subfiles are moving properly EXCEPT for the first folder in the directory. 除目录中的第一个文件夹外,所有文件夹,子文件夹和子文件都可以正常移动。 It instead outputs all of these subfiles/subfolders from this folder into the destination. 而是将所有这些子文件/子文件夹从此文件夹输出到目标位置。 It does however keep their structure, just does not include their parent directory. 但是,它不会保留其结构,只是不包括其父目录。 My code is as follows: 我的代码如下:

Get-ChildItem $sourceFilePath -Force | Copy-Item -Destination ("*file path*" -f $destinationServer, $destinationClient) -Recurse -Force 
  • filepath is paraphrased to improve readability 改写文件路径以提高可读性
  • $sourceFilePath is the source folder that I am trying to copy $ sourceFilePath是我要复制的源文件夹
  • $destination server / $destinationClient are variables used in the paraphrased "file path" $ destination服务器/ $ destinationClient是在释义的“文件路径”中使用的变量

I cannot figure out why this code works for all other folders, subfolders, and files EXCEPT for this one single folder and its items. 我无法弄清楚为什么此代码除了适用于该单个文件夹及其项目以外,还适用于所有其他文件夹,子文件夹和文件。 Any help would be greatly appreciated, and if there's any other information that would help please let me know. 我们将不胜感激,如果有任何其他信息可以帮助您,请告诉我。


Answer thanks to @Christian Müller: 感谢@ChristianMüller:

New-Item $destinationFilePath -name "MissingFolder" -type directory -Force | Out-Null

Get-ChildItem $sourceFilePath -Force | Copy-Item -Destination ("*filepath*" -f $destinationServer, $destinationClient) -Recurse -Force

This is a really strange bug and also astonished myself! 这是一个非常奇怪的错误,也令我惊讶! :) :)

It seems when the destination base directory is not existing, than it is only created but not with content. 似乎目标基本目录不存在,而是仅创建目录而不包含内容。

So with this construction, you can even debug this behaviour in ISE: 因此,通过这种构造,您甚至可以在ISE中调试此行为:

$sourceFilePath = "c:\temp\Test1"
$destPath = "c:\temp\Test2"
$a=Get-ChildItem $sourceFilePath -Force

rm -Force $destPath
foreach($x in $a) {
    $x | Copy-Item -Destination "$destPath" -Recurse -Force 
}

dir $destPath

Creating the target directory first, resolving the issue, with New-item: 首先使用新项目创建目标目录,以解决该问题:

$sourceFilePath = "c:\temp\Test1"
$destPath = "c:\temp\Test2"
$a=Get-ChildItem $sourceFilePath -Force

rm -Force $destPath

New-Item -ItemType Directory -Force -Path $destPath
foreach($x in $a) {
    $x | Copy-Item -Destination "$destPath" -Recurse -Force 
}

dir $destPath

But for my example it would work, to not using "Get-ChildItem" at all but 但以我的示例为例,完全不使用“ Get-ChildItem”即可

Copy-Item c:\temp\test1 -Destination c:\temp\test2 -Recurse -Force

Would this also work for you? 这对您也有用吗?

Copy-Item $sourceFilePath -Destination ("*file path*" -f $destinationServer, $destinationClient) -Recurse -Force 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM