我有一个 .csv,希望在将其发送到 RPA 工具以自动化 ETL 流程之前对其进行检查。此脚本的目的是验证每个 .csv 中的两个字符串,以逗号分隔,并且第三个数据值是整数。第一行是列标题,应被忽略。该工作表看起来像这样(请注意,第二个字符串内部有逗号)。
每行必须包含两个文本字符串,末尾有一个数字 - 如果每行缺少任何数据,我们预计会失败
这就是我们认为“已批准”的输入文件
ProductName,Description,Value
"Product Number One","This is a description for product number 1, released 5/3/2024",3.4563
"A different Product Number","This is a description for product number 2, released 3/2/2025",5.3200
"This is another product","Another unique description, released 6/3/2022",5.0000
这是一张应该失败并返回退出代码 2 的工作表
ProductName,Description,Value
"Product Number One","This is a description for product number 1, released 5/3/2024",
"A different Product Number",,5.3200
""Another unique description, released 6/3/2022",5.0000
我尝试使用这个脚本,但我无法让它与一个数据集一起工作,更不用说 3 了。无论如何,它似乎都会返回下面的条件 0,除非我将其更改为匹配,但这是行不通的
$File = 'c:\test\FileNameHere.csv'
$Contents = 'ProductName'
If (Get-Content $File | foreach {$_ -like $Contents})
{
echo "File check successful"
EXIT 0
}
else
{
echo "Error with source file - manual review is required to proceed"
EXIT 2
}
有什么想法吗?
我猜你正在寻找这样的东西:
function validatething {
param($path)
foreach ($row in Get-Content $path | Select-Object -Skip 1) {
if ($row -notmatch '^(?:".+",){2}[0-9]+(?:\.[0-9]+?)$') {
return $false
}
}
return $true
}
$File = 'c:\test\FileNameHere.csv'
if (validatething $File) {
'File check successful'
exit 0
}
else {
'Error with source file - manual review is required to proceed'
exit 2
}
您可以在此处查看有关正则表达式模式的详细信息:https://regex101.com/r/SkX8FY/2。