Hi, I have a bit of a situation with a script I wrote. I tried looking online, but didn’t find any help.
I have a folder in which everyday (in theory) a certain list of files arrives, the folder stores the files of the last 2 weeks. The file I need to filter is structured like a csv and every file name contains two particular strings. “SDD_380712_020_021_IBACDC” and “.csv” (the file itself is a .sav, so .csv is not the extension).
I made a code that everyday selects the file from the day before and imports it for processing, two days ago the file wasn’t present so my script had an error. I was looking for a way to check if the file exists and display an error otherwise.
I found online suggestion aboutusing test-path or [System.IO.File]::Exists($path), but the problem is besides those 2 common strings my files have a different and unpredictable name everyday, so I cannot know it’s exact path.
Here is my code so far
$VenditeGrezze = "C:\Files"
$FileCDC = (Get-ChildItem -Path $VenditeGrezze -Include "*SDD_380712_020_021_IBACDC*" "*.csv*" -Recurse | ? {
$_.LastWriteTime -gt (Get-Date).AddDays(-1) #the file is from yesterday
})
$Data = Import-Csv -Path $FileCDC -Delimiter '|' -ErrorAction Stop # Import the file
My idea is something like if $FileCDC is null
{
Sends error message
}
Else
{
$Data = Import-CSV …
}
Ty in advance
Alice