How do I concatenate two text files in PowerShell?

我只是一个虾纸丫 提交于 2019-11-27 06:26:16

You can simply use cat example1.txt, example2.txt | sc examples.txt. You can surely concatenate more than two files with this style, too. Plus, if the files are named similarly, you can use:

cat example*.txt | sc allexamples.txt

The cat is an alias for Get-Content, and sc is an alias for Set-Content.

Note 1: Be careful with the latter method - if you try to output to examples.txt (or similar that matches the pattern), PowerShell will get into an infinite loop! (I just tested this).

Note 2: Outputting to a file with > does not preserve character encoding! This is why using Set-Content (sc) is recommended.

user2074686

Do not use >; it messes up the character encoding. Use:

Get-Content files.* | Set-Content newfile.file
manojlds

In cmd, you can do this:

copy one.txt+two.txt+three.txt four.txt

In PowerShell this would be:

cmd /c copy one.txt+two.txt+three.txt four.txt

While the PowerShell way would be to use gc, the above will be pretty fast, especially for large files. And it can be used on on non-ASCII files too using the /B switch.

mjsr

You could use the Add-Content cmdlet. Maybe it is a little faster than the other solutions, because I don't retrieve the content of the first file.

gc .\file2.txt| Add-Content -Path .\file1.txt

To concat files in command prompt it would be

type file1.txt file2.txt file3.txt > files.txt

Powershell converts the type command to Get-Content, which means you will get an error when using the type command in powershell because the Get-Content command requires a comma separating the files. The same command in powershell would be

Get-Content file1.txt,file2.txt,file3.txt | Set-Content files.txt

I used:

Get-Content c:\FileToAppend_*.log | Out-File -FilePath C:\DestinationFile.log 
-Encoding ASCII -Append

This appended fine. I added the ASCII encoding to remove the nul characters Notepad++ was showing without the explicit encoding.

If you need to order the files by specific parameter (e.g. date time):

gci *.log | sort LastWriteTime | % {$(Get-Content $_)} | Set-Content result.log
vlad-ardelean

You can do something like:

get-content input_file1 > output_file
get-content input_file2 >> output_file

Where > is an alias for "out-file", and >> is an alias for "out-file -append".

Since most of the other replies often get the formatting wrong (due to the piping), the safest thing to do is as follows:

add-content $YourMasterFile -value (get-content $SomeAdditionalFile)

I know you wanted to avoid reading the content of $SomeAdditionalFile into a variable, but in order to save for example your newline formatting i do not think there is proper way to do it without.

A workaround would be to loop through your $SomeAdditionalFile line by line and piping that into your $YourMasterFile. However this is overly resource intensive.

I think the "powershell way" could be :

set-content destination.log -value (get-content c:\FileToAppend_*.log )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!