Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
复制项目,但不包括PowerShell中源目录的子文件夹文件_Powershell_Copy Item_File Move - Fatal编程技术网

复制项目,但不包括PowerShell中源目录的子文件夹文件

复制项目,但不包括PowerShell中源目录的子文件夹文件,powershell,copy-item,file-move,Powershell,Copy Item,File Move,我有一个脚本,可以将具有特定扩展名的文件从源目录移动到目标目录 我的源目录如下所示: if(!(Test-Path $SourceDirPath) -or !(Test-Path $DestinationDirPath)) { Write-Host "The source- or destination path is incorrect, please check " break }else { Write-Host "Success" Copy-Item -p

我有一个脚本,可以将具有特定扩展名的文件从源目录移动到目标目录

我的源目录如下所示:

if(!(Test-Path $SourceDirPath) -or !(Test-Path $DestinationDirPath))
{
    Write-Host "The source- or destination path is incorrect, please check "
    break
}else
{
    Write-Host "Success"
    Copy-Item -path $SourceDirPath -Filter "*$extension" -Destination $DestinationDirPath -Recurse
}
文件文件夹

  • File1.td

  • File2.td

    子文件夹

    • File3.td

    • File4.td

我的脚本很短,看起来像:

if(!(Test-Path $SourceDirPath) -or !(Test-Path $DestinationDirPath))
{
    Write-Host "The source- or destination path is incorrect, please check "
    break
}else
{
    Write-Host "Success"
    Copy-Item -path $SourceDirPath -Filter "*$extension" -Destination $DestinationDirPath -Recurse
}
我必须提到
$SourceDirPath
来自一个配置文件,只有当我声明它为
C:\FILESFOLDER\*

脚本可以工作,但不会将文件从子文件夹复制到目标文件夹。目标只有文件1和文件2


“我的复制项目”命令有什么问题?

您应该使用带有
-recurse
参数的
Get-ChildItem
cmdlet根据筛选条件检索所有文件。然后,您可以将结果通过管道传输到
复制项
cmdlet,只需指定目标。

这样将比较目录源和目标,然后复制内容。忽略我的时髦变量,我在下面的编辑中修改了它们

 #Release folder
    $Source =  ""
    #Local folder
    $Destination = ""

    #Find all the objects in the release
    get-childitem $Source -Recurse | foreach {

    $SrcFile = $_.FullName
    $SrcHash = Get-FileHash -Path $SrcFile -Algorithm MD5 # Obtain hash

    $DestFile = $_.Fullname -replace [RegEx]::escape($Source),$Destination #Escape the hash
    Write-Host "comparing $SrcFile to $DestFile" -ForegroundColor Yellow

    if (Test-Path $DestFile) 
    {
    #Check the hash sum of the file copied
    $DestHash = Get-FileHash -Path $DestFile -Algorithm MD5

    #compare them up
    if ($SrcHash.hash -ne $DestHash.hash) {
        Write-Warning "$SrcFile and $DestFile Files don't match!"

        Write-Warning "Copying $SrcFile to $DestFile " 

        Copy-Item $SrcFile -Destination $DestFile -Force
    } 
    else {
        Write-Host "$SrcFile and $DestFile Files Match" -ForegroundColor 
Green

    }
        } 
    else {
        Write-host "$SrcFile is missing! Copying in" -ForegroundColor Red
        Copy-Item $SrcFile -Destination $DestFile -Force

         }
    }  

在您的情况下,
-Recurse
开关无效
,因为它仅应用于由
$SourceDirPath
组合匹配的项,该组合使用尾随的
\*
匹配源目录中的项和
-Filter
,在您的情况下,只有直接位于源目录中的
*.td
文件

省略尾随的
\*
以目录本身为目标(例如,
C:\FOLDER
而不是
C:\FOLDER\*
),原则上解决了这个问题,但是,仅在目标目录还不存在时才起作用。 如果确实存在,则将这些项放置在目标目录的子目录中,以源目录命名

如果在复制之前现有目标目录中没有要保留的内容(并且目录本身没有需要保留的特殊属性),您可以通过提前删除先前存在的目标目录来解决此问题

if(!(Test-Path $SourceDirPath) -or !(Test-Path $DestinationDirPath))
{
    Write-Host "The source or destination path is incorrect, please check "
    break
}
else
{
    Write-Host "Success"

    # Delete the preexisting destination directory,
    # which is required for the Copy-Item command to work as intended.
    # BE SURE THAT IT IS OK TO DO THIS.
    Remove-Item $DestinationDirPath -Recurse

    # Note: $SourceDirPath must NOT end in \*  
    Copy-Item -Recurse -LiteralPath $SourceDirPath -Filter "*$extension" -Destination $DestinationDirPath
}
如果确实需要保留现有的
$DestinationDirPath
内容或属性(ACL,…),则需要做更多的工作。

尝试以下操作:

$source =  "C:\Folder1"

$destination = "C:\Folder2"

$allfiles = Get-ChildItem -Path $source -Recurse -Filter "*$extension"
foreach ($file in $allfiles){
    if (test-path ($file.FullName.replace($source,$destination))) {
    Write-Output "Seccess"
    Copy-Item -path $file.FullName -Destination ($file.FullName.replace($source,$destination))
    }
    else {
    Write-Output "The source- or destination path is incorrect, please check "
    break
    }
}

你想用子文件夹本身复制子文件夹中的文件吗?还是你只想要这些文件?我想在目标中有完全相同的结构,这样子文件夹和文件在源目录和目标目录中也能有完全相同的结构吗?你必须自己解决这个问题。你知道吗可能会考虑使用RoopScript来做这个。不可能,这是可能的,你想在复制之前复制或者只是复制所有的东西而不管目的地的存在吗?我告诉过你,你必须自己去关心这个结构。还有,RoopRoad附带的是Windows,它不是一个外部工具。哦,好的,感谢不起作用,你只是添加了
-Force
并更改了开关的位置?我在目标文件夹中仍然只有2个文件。您更新的答案不起作用。它说“复制项:无法将参数绑定到参数”路径,因为它为空。“这是因为我在复制和粘贴过程中遗漏了一个变量。我必须在一分钟后运行,但我在上面添加了一个脚本。”注意,它对对象进行二进制比较,然后重复使用副本。这将执行您想要的操作,但也将执行二进制比较,并向Ps报告颜色编码的引用。这是一种执行您想要的操作的非常冗长的方法,但会执行!如果它们不同,它会执行。在您的情况下,这将是您更新的唯一原因(如果你明白我的意思的话)-MD5算法在这方面很好,因为它检查实际的文件散列以及内容。这是一个全面的检查,而不是一些“哦,是同一个日期”然后继续。我刚刚意识到,如果文件之间存在差异,确实如此。再次感谢这段伟大的代码。如果我的源代码没有
\*
,我的脚本就无法运行。遗憾的是,它没有运行,它还删除了我的整个目标文件夹?@KahnKah:如上所述(请告诉我,我是否可以在回答中更清楚地说明这一点):解决方法要求首先删除目标文件夹(我知道这很烦人-
复制项在许多方面都已损坏)。但是,除此之外,它应该可以工作。那么:你是说它不工作,还是说必须保留现有目标目录中的内容?