2016-11-02 12 views
0

複数のテキストファイルの内容を1つのテキストファイルにマージしたい。複数のテキストファイルをWindows Powershellの 'copy'とマージする方法

私はこのanswerで説明したcatを試しました。しかし、それは非常に遅いです。それはいくつかのファイルのためではなく、数千のファイルのためokです

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

:のような文字列を分離 copyコマンドは、はるかに高速ですが、プラス記号でファイル名を配置する必要があります。 だから、私の考えは次のようにcopyのファイルの入力を含む変数を作成することでした。

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

、その後:

cmd /c copy %list all.txt 

しかし、これは動作しませんが。

(私はループでPowerShell内にもファイル名の文字列を作成することができます。)

今、私は2番目のファイルで最初のファイルをマージしループと3番目のファイルと結果のファイルを作りたいとそうです。その後、

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

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

...

どのように私はPowerShellでループ内でこれを行うことができますか?

答えて

0
# 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 
関連する問題