检查文件是否存在并移动重复项

时间:2018-07-18 18:47:04

标签: powershell

我正在尝试遍历包含多个文件的文件夹,以查看共享网络文件夹/文件夹中是否有任何文件重复。如果是这样,请将其移动到名为CompareDuplicates的文件夹中。

问题1:
只有P:\clients\是共同的,而且非常大。

模式是

P:\clients\"some-clientname"\EOB\"some-year"\"some-month" 

我想更进一步一点:

P:\clients\*wildcard-or-list*\EOB\"search-the-rest-recursively"

问题2:让Get-ChildItem工作还是使用Test-Path?

这是我到目前为止所拥有的:

#Common Paths
$PathFilesToCheck = "P:\GroupofFiles" #May have a folder in it - do not want to look for it in other folders
$PathFilesToCompareTo = "P:\clients\" #Needs to recusively look
$PathDupFolder = "$PathFilesToCheck\ComparedDuplicates" #Place holder for dulpicate files found.

#Variables
$FilesToCheck = Get-ChildItem "$PathFilesToCheck\*.*"

#Make a Duplicate folder (if folder does NOT exist) to hold duplicate files that were compared to the client folders.
If ( -Not (Test-Path -Path $PathDupFolder)) {
    New-Item "$PathDupFolder" -ItemType directory -Force |Out-Null #Out-Null makes powershell wait for the folder to be created before moving files.
}

#Loop through each file name and compare to client folders
ForEach ($File in $FilesToCheck) { #Loop
    $FileExists = Get-ChildItem -Path $PathFilesToCompareTo -Include $File -Recurse #Test if file name exist
    If ($FileExists -eq $True) { #File is a duplicate
        Move-Item -Path $PathFilesToCompare -Destination $PathDupFolder  #Move the file to the duplicate folder
    }
}

谢谢

0 个答案:

没有答案