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:
Check 2 file paths using wildcard for lookup.
a) \\srv1\foldera\live\blt_xxxxxxxxxxx.csv
b) \\srv1\foldera\arch\xxxxxxx.pdf
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
$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):
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.