【问题标题】:How to combine two commands to get one output using command prompt如何使用命令提示符组合两个命令以获得一个输出
【发布时间】:2021-07-27 23:43:13
【问题描述】:

我在共享网络驱动器上有一个包含大量文本文件的文件夹。我需要列出每个文件中的文件名、大小和行数/行数。我可以使用命令提示符单独获取输出,但似乎无法合并。

这非常适合列出文件名和大小:

DIR /s “files location*.txt” > Directory.txt

这适用于行数:

for %f in ("files location*.txt" ) do find /v /c "" "%f"

我尝试了以下组合,但输出为空,命令提示符窗口显示完整的文件位置和名称,但没有行数

DIR /s “files location*.txt” | for %f in (“files location*.txt”) do find /v /c "" "%f" > Directory.txt

【问题讨论】:

  • (dir /S "files location*.txt" & for %f in ("files location*.txt") do find /V /C "" "%~f") > "Directory.txt"?管道| 在这里是错误的,因为它将一个命令的输出传递到另一个命令的输入。你真的想要dir /S 而不是for /RN. B.: 我希望您的代码中使用直引号而不是印刷引号……
  • 我试过了(对于 /R “文件位置” %g IN (.txt) 输入“%g”)|也找到 /c /v "" 但什么也没发生。我也试过 set file= files location *.txt set /a cnt=0 ( For /r do ( for /r "files location *.txt" %a in ('type "%f"^|find /C /v "" ') do set /a cnt=%%a echo %file% has %cnt% lines) )>>list.txt GOTO :EOF 我收到一个错误,文件位置未被识别为内部或外部命令。文件位置.txt 包含在引号中,因为文件路径包含空格。

标签: cmd


【解决方案1】:

我想这个问题以前就已经在这里了。将这两 (2) 个文件放入同一目录中。该目录应该在 PATH 变量中。可以做很多事情来使使用参数更灵活。如果您在受支持的 Windows 系统上,则可以使用 PowerShell。如果您有 PowerShell 6 或更高版本,请将 powershell 更改为 pwsh

=== Get-FileLineCount.bat

@ECHO OFF
powershell -NoLogo -NoProfile -File "%~dp0Get-FileListCount.ps1"
EXIT /B

=== Get-FileLineCount.ps1

Get-ChildItem -File -Path 'C:\src\t' -Filter '*.txt' |
    ForEach-Object {
        [PSCustomObject]@{
            LastWriteTime = $_.LastWriteTime
            Length = $_.Length
            LineCount = (Get-Content -Path $_.FullName | Measure-Object).Count
            FileName = $_.FullName
        }
    }

这会产生以下输出。

LastWriteTime         Length LineCount FileName
-------------         ------ --------- --------
2021-04-08 08:14:59        3         1 C:\src\t\abc.txt
2021-04-08 08:16:39        8         1 C:\src\t\abc-utf-8.txt
2019-07-08 11:38:36       30         1 C:\src\t\append.txt
2019-07-08 11:38:36       36        12 C:\src\t\appendtemp.txt
2020-03-06 09:48:51      104        25 C:\src\t\Combined.txt

【讨论】:

  • 我尝试了上面的 powershell 方法,它可以工作,但我需要的是文件名、大小和行数。这只给出文件名和行数
  • @LenC,已添加长度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-27
  • 1970-01-01
相关资源
最近更新 更多