Check for file existence in different folders

I have seen many examples of how to check if a file exists, but I’m need of a script example
that does the following:

  1. Check 2 file paths using wildcard for lookup. a) \\srv1\foldera\live\blt_xxxxxxxxxxx.csv b) \\srv1\foldera\arch\xxxxxxx.pdf
  2. It's needs to check live versus arch for a match on part of a file name, so in live folder everything after the blt_ up to the .csv. That the results of that lookup then make sure that result exists in the arch folder.

    example:
    Live:
    \srv1\foldera\live\blt_ps08776a.csv –> get the value ps08776a in variable
    Arch:
    \srv1\foldera\arch\ps08776a.pdf –> now lookup from variable above to see if
    ps08776a.pdf exists.
    Loop thru Live folder until no more records exist checking against Arch.

    If match not found then move the file from Live(blt_ps08776a.csv) to a 3rd folder

    called missing. –> \srv1\foldera\missing

    Thanks.

Andrew,

what have you tried so far? Please show your code.

 

$folder1 = "C:\temp\Powershell\live"
$folder2 = "C:\temp\Powershell\arch"

# Get all files under $folder1, filter out directories
$firstFolder = Get-ChildItem -Recurse $folder1 | Where-Object { -not $_.PsIsContainer }

$failedCount = 0
$i = 0
$totalCount = $firstFolder.Count
$firstFolder | ForEach-Object {
    $i = $i + 1
    Write-Progress -Activity "Searching Files" -status "Searching File  $i of     $totalCount" -percentComplete ($i / $firstFolder.Count * 100)
    # Check if the file, from $folder1, exists with the same path under $folder2
    If ( Test-Path ( $_.FullName.Replace($folder1, $folder2) ) ) {
        # Compare the contents of the two files...
        If ( Compare-Object (Get-Content $_.FullName) (Get-Content $_.FullName.Replace($folder1, $folder2) ) ) {
            # List the paths of the files containing diffs
            $fileSuffix = $_.FullName.TrimStart($folder1)
            $failedCount = $failedCount + 1
            Write-Host "$fileSuffix is on each server, but does not match"
        }
    }
    else
    {
        $fileSuffix = $_.FullName.TrimStart($folder1)
        $failedCount = $failedCount + 1
        Write-Host "$fileSuffix is only in folder 1"
    }
}

$secondFolder = Get-ChildItem -Recurse $folder2 | Where-Object { -not $_.PsIsContainer }

$i = 0
$totalCount = $secondFolder.Count
$secondFolder | ForEach-Object {
    $i = $i + 1
    Write-Progress -Activity "Searching for files only on second folder" -status "Searching File  $i of $totalCount" -percentComplete ($i / $secondFolder.Count * 100)
    # Check if the file, from $folder2, exists with the same path under $folder1
    If (!(Test-Path($_.FullName.Replace($folder2, $folder1))))
    {
        $fileSuffix = $_.FullName.TrimStart($folder2)
        $failedCount = $failedCount + 1
        Write-Host "$fileSuffix is only in folder 2"
    }
}

I have used this before to check for exact match’s of file and contents. I need the substring lookup and compare
along with the copy to missing folder. –> \srv1\foldera\missing

I am on my phone right now but if I understand correctly what you need to do, the following should work as long as the file name always follows the same pattern (_.csv):

$FullName = 'blt_ps08776a.csv'

$FullName.Substring($FullName.IndexOf('_') + 1,$FullName.IndexOf('.') - 4)

 

How do I inject that into my code, then use that to compare folder2 where I want everything
before the .pdf to match against. I really looking for a simple compare folder1 against folder2, as folder1 files
are my drivers, and if not found I need to move them to a missing directory.

Thanks for reply

 

Thanks for reply

I did not read through your complete code but I think you’re overcomplicating this a lot. Here is how I’d approach it.

$folder1 = "C:\temp\Powershell\live"
$folder2 = "C:\temp\Powershell\arch"

Get-ChildItem -Path $folder1 -File -Recurse -Filter *.csv |
    ForEach-Object {
        $CompareString = ($_.BaseName -split '_')[1]
        $EscapedFolder1 = [REGEX]::Escape($folder1)

        $EstimatedTargetPath = $_.DirectoryName -replace $EscapedFolder1,$folder2
        $EstimatedTargetFileName = Join-Path -Path $EstimatedTargetPath -ChildPath ($CompareString + '.pdf')
        if(Test-Path -Path $EstimatedTargetFileName){
            "$_.FullName  matches $EstimatedTargetFileName"
        }
        else {
            "No match found for $_.FullName"
        }
    }

Instead of the console output you can do the further steps needed for your tastk. :wink:

.

That is a much simpler approach, works great …

 

 

Many Thanks!!!