Detecting missing file names in a folder

Every day we receive a zip file from several clients. The file name consists of the following:

data_clientname_timestamp.zip

If “data” is always the same text, “client name” can be anything, and “timestamp” is the date the file was created.

Files are always in the same directory. Client names are always known in advance, so I know which files should be received.

The script should do the following:

  • List all received files (created) today
  • If the file from one or several clients is missing, write "the file from the client .. is missing" to the file
  • I would like to list clients in a variable so that they can be easily changed.

What I still have:

$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 a 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
+4
2

- (, CSV clients.csv), , , . :

$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
+2

, , :

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

zip :

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

result.txt:

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

Where-Object , :

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

Source: https://habr.com/ru/post/1674559/


All Articles