Здравствуйте, возникла трудность с моим скриптом. Поиск в интернете решения не дал.
В папку ежедневно (по крайней мере в теории) приходит определённый набор файлов, и хранятся они примерно 3 недели. Нужный файл по содержимому похож на csv, а в имени всегда присутствуют две строки. “SDD_492813_030_031_IBZXCD” и “.csv” (настоящее расширение у него .sav, а не .csv).
Скрипт каждый день выбирает вчерашний файл и импортирует его для обработки, но три дня назад файла не оказалось, и скрипт завершился с ошибкой. Хочу проверять наличие файла и выдавать ошибку, если его нет.
В интернете советуют [System.IO.File]::Exists($path) или test-path, однако кроме 2 общих строк имя каждый день меняется непредсказуемо, так что полный путь заранее неизвестен.
Вот текущий код
$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
Представляю себе проверку вроде if $DailyInput is null
{
Показать ошибку
}
Else
{
$Records = Import-CSV …
}
Заранее спасибо
Tavin
Переведено с английского·Показать оригинал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