1

I have several text files that I would like to combine together into one. Size of folder containg all the separate text files is nearly 8Gb. I tried the following in powershell :

cat example*.txt | sc allexamples.txt

but as soon as the size of combined file reaches close to 800 Mb the laptop is hanging and Im not able to proceed further. Any other way in which I can do this?

elyashiv
  • 3,623
  • 2
  • 29
  • 52
aswathy
  • 821
  • 3
  • 15
  • 27

3 Answers3

5

It can be done in PowerShell as well, you just need to break it up a bit or use a stream writer which is even less work so you're not hanging onto things in memory.

$file = [system.io.file]::OpenWrite("$($pwd.Path)\allexamples.txt")
$writer = New-Object System.IO.StreamWriter($file)

cat example*.txt | ForEach-Object { $writer.WriteLine($_) }

$writer.Close()
$file.Close()
Chris Dent
  • 3,910
  • 15
  • 15
0

The reason why it hangs is due to the lack of memory. what your command essentially does is piping the output to the buffer and after that redirected to another file. what you should do is write a simple script in python to merge the file for you. Python has functions/classes that supports such operation.

Mox
  • 2,355
  • 2
  • 26
  • 42
0

Get-Content -readcount can help a lot here to find the righ Balance between Memory consumption and speed. I'd try something like

Foreach ($File in (Get-Childitem <Folder> -Filter example*.txt)) {
    Get-Content $File -Readcount 1000 | Out-File allexamples.txt -Append
}

What the best value for readcount is depends on the filesize, Memory available and processing power.

whatever
  • 871
  • 4
  • 11