我还有其他软件可以导出csv文件,当一切正常运行时,csv中有1291行。我试图轻松地确定哪些文件不完整。
这是我第一次进入PowerShell。我修改了stackoverflow上的代码:
$files = Get-ChildItem 'd:\*.txt'
ForEach ($file in $files) {
$lineCount = Get-Content -LiteralPath $file | Measure-Object | Select-Object -ExpandProperty Count
Write-Output "File $($file.Name) has $lineCount lines"
if ($lineCount -gt 50) {
Write-Warning "Warning $($file.Name) is too big"
}
}
并且成功地做到了这一点,没有出现错误:
$files = Get-ChildItem 'D:\!__DATA for searches\__CSVs\*.csv'
$logfile = 'D:\!__DATA for searches\__CSVs\CompletedFiles.txt'
ForEach ($file in $files) {
$lineCount = Get-Content -LiteralPath $file | Measure-Object | Select-Object -ExpandProperty Count
Write-Output "File $($file.Name) has $lineCount lines"
if ($lineCount -gt 1290) {
Write-Warning "Warning $($file.Name) is completed" | Out-File $logfile -Append
}
}
还有这个变化
$files = Get-ChildItem 'D:\!__DATA for searches\__CSVs\*.csv'
ForEach ($file in $files) {
$lineCount = Get-Content -LiteralPath $file | Measure-Object | Select-Object -ExpandProperty Count
Write-Output "File $($file.Name) has $lineCount lines"
if ($lineCount -gt 1290) {
Write-Warning "Warning $($file.Name) is completed" | Out-File 'D:\!__DATA for searches\__CSVs\CompletedFiles.txt' -append
}
}
但都不写入日志文件。该脚本在PS6控制台中运行良好,用1291行代码标识文件。
因此,我需要日志文件,另外,我还想将完成的文件移动到同一脚本中的另一个目录中,因此也非常感谢您输入实现该操作的内容/位置。
由于答案和其他研究,我完成了剧本
$files = Get-ChildItem 'D:\!__DATA for searches\__CSVs\*.csv'
$logfile = 'D:\!__DATA for searches\__CSVs\CompletedFiles.txt'
ForEach ($file in $files) {
$lineCount = Get-Content -LiteralPath $file | Measure-Object | Select-Object -ExpandProperty Count
# Write-Output "File $($file.Name) has $lineCount lines"
if ($lineCount -gt 1290) {
# Write-Warning "Warning $($file.Name) is completed" 3>&1 |Out-File $logfile -Append
Write-Output "File $($file.Name) has $lineCount lines, and moving to D:\Completed\" 3>&1 | Out-File $logfile -Append
Move-Item "D:\!__DATA for searches\__CSVs\$($file.Name)" "D:\!__DATA for searches\Completed\" -Force
}
}