bRins
bRins

Reputation: 93

Detect missing filenames in folder

Every day we receive a zipfile from a number of clients. The filename consists of the following:

data_clientname_timestamp.zip

Where "data" is always the same text, "clientname" could be anything and "timestamp" is the file creation date.

The files are always in the same directory. The clientnames are always known in advance, so I know what files should be received.

The script should do the following:

What I have so far:

$folder='C:\data'
Get-ChildItem $folder -recurse -include @("*.zip") | 
    Where-Object {($_.CreationTime -gt (Get-Date).Date )} | select name | out-file $folder\result.txt

But how to check the file for missing files?

Edit: Testdata:

$Timestamp = (Get-Date).tostring(“yyyyMMddhhmmss”)
New-Item c:\Data -type Directory
New-Item c:\Data\Data_client1_$Timestamp.zip -type file
New-Item c:\Data\Data_client2_$Timestamp.zip -type file
New-Item c:\Data\Data_client3_$Timestamp.zip -type file
New-Item c:\Data\Data_client5_$Timestamp.zip -type file
New-Item c:\Data\Data_client6_$Timestamp.zip -type file
New-Item c:\Data\Data_client7_$Timestamp.zip -type file
exit

Script:

$folder='C:\Data'
$clients = @("client1", "client2", "client3", "client4", "client5", "client6", "client7")
$files = Get-ChildItem $folder -recurse -include @("*.zip") | 
    Where-Object {($_.CreationTime -gt (Get-Date).Date )}

$files | Select-Object Name | Out-File $folder\result.txt
$files | Where-Object { ($_.Name -replace '.+?_([^_]+).*', '$1') -notin $clients} | Out-File $folder\result2.txt

Upvotes: 4

Views: 1951

Answers (2)

Martin Brandl
Martin Brandl

Reputation: 58931

Start with defining a list of clients you would expect like:

$clients = @("client1", "client2")

Then retrieve all zip files and save it to a variable:

$files = Get-ChildItem $folder -recurse -include @("*.zip") | 
    Where-Object {($_.CreationTime -gt (Get-Date).Date )}

Export the existing files to your result.txt:

$files | Select-Object Name | Out-File $folder\result.txt

Now you can determine each missing client using the Where-Object cmdlet with a regex that grabs the clientname:

$fileClients = $files | ForEach-Object { ($_.Name -replace '.+?_([^_]+).*', '$1') } 
Compare-Object $clients $fileClients | select -ExpandProperty InputObject | Out-File $folder\result2.txt

Upvotes: 3

Mark Wragg
Mark Wragg

Reputation: 23355

You need to have a list of your clients somewhere (such as in a CSV file named clients.csv) then you could loop through that list to check if a file is found for each client. For example:

$folder='C:\data'
$Clients = Import-CSV Clients.csv

$Files = Get-ChildItem $folder -recurse -include @("*.zip") | Where-Object {($_.CreationTime -gt (Get-Date).Date )} | select name 

$Clients | ForEach-Object { 
    $Client = $_
    $ClientCheck = $Files | Where-Object {$_ -like $Client}
    If (-not $ClientCheck) { 
        Write-Warning "$Client is missing!"
    }Else{
        Write-Output $ClientCheck
    }
} | out-file $folder\result.txt

Upvotes: 2

Related Questions