1

In C:Temp I have 5 csv files, how I copy all to one file.txt? Thanks Josef

Josef
  • 11
  • 2

2 Answers2

3

If you know the names, you can get the content like this:

Get-Content c:\item1.csv, c:\item2.csv
# or
Get-Content c:\*.csv

The Get-Content command will read all the specified files and return it as array of strings. Note that you can specify encoding as well.

Get-Content -Encoding Utf8 c:\*.csv

(for more info see Get-Help Get-Content -online or help gc -online)

As for storing the content to the file, pipe the otput from Get-Content to Set-Content. You can specify encoding as well. I'm saying that because redirection (>) by default outputs the content as Unicode, which is not always wanted.

Long story short. Use:

Get-Content *.csv | Set-Content file.txt

You can of course use build-in cmdlets for working with CSV. First list the files and pipe to Import-Csv. This will produce an PsObject for each csv row. This is piped to Export-CSv to store it in a file:

Get-Item c:\csv1.csv, c:\csv2.csv | 
  Import-Csv | 
  Export-Csv c:\res.txt
stej
  • 28,745
  • 11
  • 71
  • 104
2
cat *.csv > yourtxtfilename.txt

If you're just going to use the command prompt, replace cat with type.

The wildcard expansion order is undefined, so if the order is important you can specify it explicitly with:

cat file1.csv, file2.csv, file3.csv, file4.csv, file5.csv > file1-5.csv

cat is an alias for Get-Content

David Newcomb
  • 10,639
  • 3
  • 49
  • 62
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151