2017-04-13 210 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

如果我的源文件沒有'\ *',我無法讓我的腳本工作。 –

相關問題