【问题标题】:Powershell script to run multiple input text files用于运行多个输入文本文件的 Powershell 脚本
【发布时间】:2021-03-23 21:59:37
【问题描述】:

我正在尝试通过 PowerShell 脚本从输入文本文件中删除空格。

我有成百上千个必须删除空格的文本文件。我编写了删除空格的代码。它适用于一个文本文件,但我想执行多个文件。

gc C:\test\3.txt | where {$_ -ne ""} > C:\test\out.txt

在上面的示例中,我有更多的输入文件,例如 4.txt 、 5.txt 、 6.txt 等。我想一次性执行所有文件并删除空格并写入 4out.txt、5out.txt , 6out.txt 等到另一个文件夹。

【问题讨论】:

  • 请停止在您的问题中使用“lakh”。我们不都是印度人,也不懂印度话

标签: powershell text spaces


【解决方案1】:

这应该只是循环遍历您的文件并运行您已有的同一行的情况。

如果您想安全起见,请添加一些 try-catch。

$InputFiles = Get-ChildItem 'C:\test\'
$OutputPath = 'C:\test\out\'

ForEach($File in $InputFiles) {
    Get-Content $File |
        Where-Object {$_ -ne ''} |
            Out-File (Join-Path -Path $OutputPath -ChildPath $File.Name)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    相关资源
    最近更新 更多