Powershell -如果文本文件中有多行,则处理文件

wbrvyc0a  于 2023-01-20  发布在  Shell
关注(0)|答案(1)|浏览(166)

我是powershell的新手。我们在sFTP位置有多个文本文件,需要扫描,如果文件中有多行,则移动到加载过程,如果只有一条记录,则忽略它们,不加载它们。
我能够通过使用下面的代码获得行数,但它没有给我预期的结果。不知道如何实现这一点。任何帮助非常感谢。

$paramDest = "C:\\All\" 
Get-ChildItem $paramDest -Filter *.txt | 
ForEach-Object { $content = (Get-Content $_.FullName -Delimiter "`r`n" | Measure-Object).Count 
$content | Set-Content $_.FullName 
}
4sup72z8

4sup72z81#

这是你要的密码。
如果txt文件等于2行或超过2行,则将继续执行脚本。

$txtfile="C:\Users\test\Desktop\test.txt"
$content = Get-Content $txtfile -Force 
$rowcount=($content.Count)-1
If($rowcount -ge 2){
#Paste you command Here
}

**Update1:**请查找以下多个文件的代码:

$file_have_2_line = $null
    $file_have_2_line = [System.Collections.ArrayList]@()
    $sftp_path="C:\Users\test\Desktop"
    $total_txt_file = @(Get-ChildItem -Path $sftp_path -Filter *.txt -Force -Recurse  | %{$_.fullname})
    $total_txt_file = $total_txt_file |  ? { $_ } | sort -uniq
    
    Foreach($txt in $total_txt_file){
    $content = Get-Content $txt -Force 
    $rowcount=($content.Count)-1
    If($rowcount -ge 2){
    $file_have_2_line += $txt
    }}
Write-Host $file_have_2_line

您可以在$file_have_2_line变量中获取所有文件列表。

相关问题