我想将多个文本文件的内容合并到一个文本文件中。
我已经尝试过这个answer中解释的cat
。但它的速度非常慢。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中的循环中完成此操作?
发布于 2016-11-02 21:22:44
# 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
https://stackoverflow.com/questions/40379878
复制相似问题