【问题标题】:Count characters for each line计算每行的字符数
【发布时间】:2016-01-26 19:13:43
【问题描述】:

我是 WinPowerShell 的新手。拜托,你能不能给我一些代码或信息,如何编写一个程序来处理文件夹中的所有 *.txt 文件: 1.计算文件中每一行的字符数 2. 如果行的长度超过 1024 个字符,则在该文件夹中创建一个子文件夹并将文件移动到那里(我将如何知道哪个文件每行有超过 1024 个字符)

我尝试过VB和VBA(这对我来说比较熟悉),但我想学习一些新的很酷的东西!

非常感谢!

编辑:我发现了一段代码的开始

$fileDirectory = "E:\files";
foreach($file in Get-ChildItem $fileDirectory)
    {
       # Processing code goes here
    }

$fileDirectory = "E:\files";
foreach($line in Get-ChildItem $fileDirectory)
{
 if($line.length -gt 1023){# mkdir and mv to subfolder!}
}

【问题讨论】:

  • 到目前为止你得到了什么?如果您的目的是学习,那么仅仅向其他人寻求解决方案会适得其反。
  • 为什么你认为会适得其反?使用 stackoverflow,我在 VBA 方面学到了很多东西。请找到我的编辑帖子。我对 OOP 非常了解。我只需要一个代码来查看,让我说,“一种新的思维方式”。请支持我。谢谢
  • 您要求一些事情,但看起来您没有表现出任何努力,这是提出问题的先决条件? ...从那里开始,编辑看起来很棒。你的powerShell版本是什么
  • 这是一个可教的时刻。 foreach ($file in gci *.txt) { gc $file | %{ if ($_.length -gt 1024) { move $file subdir; break } } } 向您展示了 foreach 的两种不同表示法。你能找到第二个吗?
  • @Matt 我觉得是3.0版,我用的是WIN7内置PS(cmd->PowerShell)

标签: powershell


【解决方案1】:

如果你愿意学习,何不从这里开始。

您可以在 PS 中使用 Get-Content 命令来获取文件的一些信息。 http://blogs.technet.com/b/heyscriptingguy/archive/2013/07/06/powertip-counting-characters-with-powershell.aspxGetting character count for each row in text doc

【讨论】:

  • 另外,this TechNet blog entry 是我学习理解 if 语句的方法,它使用了在其他语言中不常见的比较运算符。
【解决方案2】:

在您的第二次编辑中,我确实看到了一些努力,所以我想帮助您。

$path = "D:\temp"
$lengthToNotExceed = 1024
$longFiles = Get-ChildItem -path  -File | 
    Where-Object {(Get-Content($_.Fullname) | Measure-Object -Maximum Length | Select-Object -ExpandProperty Maximum) -ge $lengthToNotExceed} 

$longFiles | ForEach-Object{
    $target = "$($_.Directory)\$lengthToNotExceed\"
    If(!(Test-Path $target)){New-Item $target -ItemType Directory -Force | Out-Null}

    Move-Item $_.FullName -Destination $target
}

您可以将其设为单线,但它会变得不必要地复杂。在Get-Content 返回的数组上使用度量对象。该数组或多或少是一个字符串数组。在 PowerShell 字符串中有一个 length 属性用于查询。

这将返回文件中的最大长度行。我们使用Where-Object 仅过滤具有我们想要的长度的那些结果。

然后对于每个文件,我们尝试将其移动到与匹配文件位于同一位置的子目录中。如果不存在子文件夹,我们会创建它。


注意事项:

  1. -File 开关至少需要 3.0。取而代之的是,您可以更新 Where-Object 以获得另一个子句:$_.PSIsContainer
  2. 这对于包含大量行的文件会表现不佳。

【讨论】:

  • 使用命令get-host,我发现我使用的是2.0版本。我会在win8上试试这个,在我的另一个comp上。谢谢!
【解决方案3】:

这是我上面的评论以 .ps1 脚本形式缩进和换行。

$long = @()

foreach ($file in gci *.txt) {
    $f=0
    gc $file | %{
        if ($_.length -ge 1024) {
            if (-not($f)) {
                $f=1
                $long += $file
            }
        }
    }
}

$long | %{
    $dest = @($_.DirectoryName, '\test') -join ''
    [void](ni -type dir $dest -force)
    mv $_ -dest (@($dest, '\', $_.Name) -join '') -force
}

我还提到了那里的标签和中断。除了$f=0if (-not($f)),您可以使用break 跳出内部循环,如下所示:

$long = @()

foreach ($file in gci *.txt) {
    :inner foreach ($line in gc $file) {
        if ($line.length -ge 1024) {
            $long += $file
            break inner
        }
    }
}

$long | %{
    $dest = @($_.DirectoryName, '\test') -join ''
    [void](ni -type dir $dest -force)
    mv $_ -dest (@($dest, '\', $_.Name) -join '') -force
}

你有没有注意到foreach 的两种不同的调用方式?有详细的foreach 命令,然后是command | %{},其中迭代项由$_ 表示。

【讨论】:

  • 成功了!只是一个小问题,如果我有几个子目录,例如:files/cchbc files/samsung files/etc,我需要更改哪一行代码,从所有 subs txt 中提取并将其放入 sub-subFolder Test ?谢谢!
  • @Stefan0309 在第二个示例中,将foreach ($file in gci *.txt) 更改为foreach ($file in gci -filter *.txt -recurse)。另外,将foreach ($line in gc $file) 更改为foreach ($line in gc $file.FullName)。我认为其余的应该没问题。
  • 我有错误的文件 hae >1024chars mv : Cannot find path 'D:\files\aleks\Sales and service business assistant.txt' because it does not exist. At line:4 char:5 + mv $_ -dest (@($dest, '\', $_.Name) -join '') -force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (D:\files\aleks\...s assistant.txt:String) [Move-Item], ItemNotFoundExce ption + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand
  • 啊。也许尝试做mv $_.FullName etc. 对不起,我应该明白的。
  • mv $_.FullName (@($dest, '\', $_.Name) -join '') -force 它有效!谢谢!
猜你喜欢
  • 2018-10-03
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 2015-09-30
  • 1970-01-01
  • 2014-04-17
  • 1970-01-01
相关资源
最近更新 更多