【问题标题】:How to merge multiple text files with 'copy' in Windows Powershell如何在 Windows Powershell 中将多个文本文件与“复制”合并
【发布时间】:2016-11-02 12:23:28
【问题描述】:

我想将多个文本文件的内容合并到一个文本文件中。

我已经尝试过 cat 在这个 answer 中解释。但这很慢。 copy 命令要快得多,但您必须将文件名放在加号分隔的字符串中,例如:

cmd /c copy file1.txt + file2.txt + file3.txt + file1.txt all.txt

几个文件没问题,但数千个文件不行。 所以我的想法是创建一个包含copy 的文件输入的变量,例如:

%list = 'file1.txt + file2.txt + file3.txt + file1.txt'

然后:

cmd /c copy %list all.txt

但这不起作用。

(我也可以在 Powershell 中使用循环创建带有文件名的字符串。)

现在我想创建一个循环,将第一个文件与第二个文件合并,将生成的文件与第三个文件合并,依此类推。

cmd /c copy file1.txt + file2.txt merge1.txt

然后:

cmd /c copy merge1.txt + file3.txt merge2.txt

...

如何在 Powershell 的循环中执行此操作?

【问题讨论】:

    标签: file powershell merge copy


    【解决方案1】:
    # Forces the creation of your content file
    New-Item -ItemType File ".\all.txt" –force
    
    # Build your file list here
    $fileList = @('file1.txt', 'file2.txt', 'file3.txt')
    
    # Assumes that all files are in the directory where you run the script
    # You might have to adapt it to provide full path (e.g. $_.FullName)
    $fileList | %{ Get-Content $_ -read 1000 } | Add-Content .\all.txt
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多