Hello, I've run into a difficulty with a script of mine. Searching the web hasn't turned up a solution.
A folder receives a particular set of files daily (at least in theory), retaining about 3 weeks of them. My target file has csv-like contents, and its name always includes these two strings. “SDD_492813_030_031_IBZXCD” and “.csv” (its actual extension is .sav, rather than .csv).
My script picks the previous day's file each day and imports it to process it, but three days ago the missing file caused it to fail. I'd like to test for the file's presence and show an error if it's absent.
Online sources mentionusing [System.IO.File]::Exists($path) or test-path, yet aside from the 2 shared strings, the name varies unpredictably each day, so I can't determine it's full path beforehand.
This is my current code
$InputDirectory = "D:\Incoming"
$DailyInput = (Get-ChildItem -Path $InputDirectory -Include "*SDD_492813_030_031_IBZXCD*" "*.csv*" -Recurse | ? {
$_.LastWriteTime -gt (Get-Date).AddDays(-2) #pick files from the prior day
})
$Records = Import-Csv -Path $DailyInput -Delimiter '|' -ErrorAction Stop # read the selected file
I'm imagining a test like if $DailyInput is null
{
Display an error
}
Else
{
$Records = Import-CSV …
}
Thanks beforehand
Tavin