안녕하세요, 제가 만든 스크립트에서 문제가 생겼어요. 웹에서 찾아봐도 해결책이 없네요.
폴더에 매일(적어도 이론상으로는) 특정 파일들이 들어오고 약 3주 치가 보관돼요. 필요한 파일 내용은 csv 비슷하고 이름에는 늘 두 문자열이 들어가요. “SDD_492813_030_031_IBZXCD”와 “.csv”요(실제 확장자는 .csv가 아니라 .sav예요).
스크립트가 매일 전날 파일을 골라 가져와 처리하는데, 사흘 전에는 파일이 없어서 실패했어요. 파일이 있는지 확인한 뒤 없으면 오류를 표시하고 싶어요.
온라인에서는 [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