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