2017-04-13 167 views
1

我有一个脚本,它将具有特定扩展名的文件从源目录移动到目标目录。复制项目不包括PowerShell中源目录的子文件夹文件

我的源目录的样子:

FILESFOLDER

  • File1.td

  • File2.td

    SUBFOLDER

    • 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来自一个配置文件,只有当我声明它为0123时才有效

该脚本可以工作,但不会将文件从子文件夹复制到目标。目的地只有File1和File2。

我的Copy-Item命令有什么问题?

+0

是否要将子文件夹中的文件与子文件夹本身一起复制?或者你只想要这些文件? –

+0

我想要在目的地中具有完全相同的结构,所以子文件夹和文件 –

回答

1

因此,这将比较目录源和目标,然后复制内容。忽略我的质朴变量,我已经低于修正他们在编辑

#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 

     } 
    } 
+1

不起作用,你刚加了'-Force'并改变了开关的位置?我仍然只有2个文件在目标文件夹中 –

+1

对不起,这很愚蠢。我已经更新了它。 –

+0

您的更新回答无效。它说'Copy-Item:不能将参数绑定到参数'Path',因为它是空的。'' –

1

您应该使用Get-ChildItem cmdlet和-recurse参数根据您的过滤条件检索所有文件。然后,您可以将结果传递给Copy-Item cmdlet,只需指定一个目标。

+0

这也让我有源目录中的完全相同的结构吗? –

+0

你必须自己照顾自己。你可以考虑使用Robocopy。 –

+0

不太可能,这是可能的, 您想在复制之前进行比较还是仅复制所有内容而不管目的地是否存在? –

1

-Recurse开关在你的情况没有影响,因为它仅适用于由$SourceDirPath的组合,它采用了相匹配的项目跟踪\*以匹配项目,源目录和-Filter,在您的情况下,您的源目录中仅有*.td文件位于直接

省略尾随\*到目标目录本身(例如,C:\FOLDER而非C:\FOLDER\*),解决了这个问题原则,但是,due to an annoying quirk只按预期工作,如果目标目录不存在尚未。 如果确实存在,则将项目放置在目标目录的子目录中,该目录的名称为目录。

如果没有什么在现有目标目录复制之前保存(和目录的任何特殊属性本身需要维护,您可以解决此问题,通过删除事先预先存在的目标目录。

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内容或属性(访问控制列表,...),需要更多的工作。

+0

它没有工作可悲,它也删除我的整个目标文件夹? –

+0

@KahnKah:正如所述(让我知道我是否可以在答案中做得更清楚):**解决方法_requires_目标文件夹首先_removed _ **(我知道这很烦人 - 'Copy-Item'在许多方法)。除此之外,它应该工作。所以:你是说这是行不通的,还是你说你必须保留现有目的地目录中的内容? – mklement0

1

ŧ ry this:

$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 
    } 
} 
+0

如果我的源文件没有'\ *',我无法让我的脚本工作。 –

相关问题