Combine multiple text files and produce a new single text file using PowerShell?

32,598

Solution 1

Here is a solution that will generate a combined file with a blank line separating the contents of each file. If you'd prefer you can include any character you want within the "" in the foreach-object loop.

Get-ChildItem d:\scripts -include *.txt -rec | ForEach-Object {gc $_; ""} | out-file d:\scripts\test.txt

And FTR there is no need to worry about putting the file you are creating in the same directory that you are scanning. It is being created after the Get-ChildItem cmdlet has run in the pipeline and so will not create problems (although its contents will be included in subsequent runs of your script if you do not remove it).

Solution 2

$directory = "C:\tmp"

$resultFile = $env:USERPROFILE + "\Desktop\result.txt"

Get-ChildItem -Path $directory -Include *.txt -Recurse | Get-Content | Out-File -FilePath $resultFile -NoClobber

Don't put the result in the same directory as the text files or you will get bad results :)

Solution 3

Try this:

 $yourdir = "c:\temp\"
 Get-ChildItem $yourdir -File -Filter *.txt | gc | out-file -FilePath ([Environment]::GetFolderPath("Desktop") + "\totalresult.txt")

If you want add file name for header you can do it:

$yourdir="c:\temp\"
$destfile= ([Environment]::GetFolderPath("Desktop") + "\totalresult.txt")
Get-ChildItem $yourdir -File -Filter *.txt | %{"________________________" |out-file  $destfile -Append; $_.Name  | Out-File  $destfile -Append; gc $_.FullName | Out-File  $destfile -Append}
Share:
32,598

Related videos on Youtube

user7026863
Author by

user7026863

Updated on January 18, 2020

Comments

  • user7026863
    user7026863 over 4 years

    I have multiple text files in a directory. How can I merge all the files into one and produce a new single text file saved on the desktop?

    • Mike Garuccio
      Mike Garuccio over 7 years
      There are a few different ways to do that with powershell but it would be helpful if you can let us know what you've already tried and what problems you have been encountering, then we can help you work through those and explain how to deal with similar problems in the future, rather than just writing code for you.
    • user7026863
      user7026863 over 7 years
      dir C:\folder* -include *.txt -rec | gc | out-file C:\result.txt ..
    • user7026863
      user7026863 over 7 years
      @MikeGaruccio all the text files are in dfs path inside a directory .. i wish to combine all those and save the result in my desktop
    • user7026863
      user7026863 over 7 years
      @MikeGaruccio also seprator between each new text file will be good
    • Mike Garuccio
      Mike Garuccio over 7 years
      The code you pasted in looks correct, and ran successfully on my machine to combine text files. Personally I'd replace the alias dir with the actual cmdlet get-childitem but that's stylistic. Are you getting any specific error messages when you try and run it it? once we work out what's going wrong with the base script we can look into adding a separator.
    • user7026863
      user7026863 over 7 years
      $merge = " " $pathmerge = "$([Environment]::GetFolderPath("Desktop"))\merge.txt";
    • user7026863
      user7026863 over 7 years
      dir C:\merge* -include *.txt -rec | gc | Out-File |fl > $pathmerge; notepad $pathmerge
    • user7026863
      user7026863 over 7 years
      @MikeGaruccio i am trying to get output saved in my desktop contents are not getting merged
    • Mike Garuccio
      Mike Garuccio over 7 years
      That's strange, I just ran your code again and it successfully combined text files and outputted them for me. What output are you getting on your desktop? are you getting error messages or is the command showing a successful completion in the powershell console? for reference the exact code I am running is dir d:\scripts* -include *.txt -rec | select -First 10| gc | out-file d:\scripts\test.txt but the differences are only in the actual paths I am supplying and that I am doing a select to limit the number of files processed
    • user7026863
      user7026863 over 7 years
      @MikeGaruccio pls try to run this $merge = " " $pathmerge = "$([Environment]::GetFolderPath("Desktop"))\merge.txt"; dir C:\merge* -include *.txt -rec | gc | Out-File |fl > $pathmerge; notepad $pathmerge contents are not getting copied text is produced with no contents
    • Mike Garuccio
      Mike Garuccio over 7 years
      Ok now the problems are beginning to make more sense, Not sure what the need for $merge = " " is as that variable is not used later in the script, but ignoring that your only problem is with the stuff you've included after Out-File, there is no need to do a format-list and you don't want to be using the old school cmd > piping to output to a file. Just give out file a target and your done (so exactly what you posted in your first comment) dir C:\folder* -include *.txt -rec | gc | out-file C:\result.txt
    • Mike Garuccio
      Mike Garuccio over 7 years
      or if you'd like to use the $pathmerge variable dir d:\scripts* -include *.txt -rec | gc | Out-File $pathmerge
    • user7026863
      user7026863 over 7 years
      @MikeGaruccio thanks how about using a separator for every new text file merged
  • user7026863
    user7026863 over 7 years
    how about separator for each new text file merged
  • user7026863
    user7026863 over 7 years
    Thanks can this be included sub directory or only the master directory any way to include all from master and sub directory
  • Mike Garuccio
    Mike Garuccio over 7 years
    That code uses the -recurse switch so it will gather text files from the directory and all sub directories underneath it, if you drop the -recurse it will only gather text files from the first level of the directory.
  • user7026863
    user7026863 over 7 years
    Big help Mike.. Thanks one last question.. Instead of Get-ChildItem even if we directly specify directory it wont affect anything ..
  • Mike Garuccio
    Mike Garuccio over 7 years
    dir and get-childitem do the exact same thing, dir is just an alias for get-childitem to make things a bit more comfortable for people coming from the DOS world (ls does the same thing as well) but scripts are easier for other PS devs to read if you use the standard syntax as opposed to the convenience alias's.
  • user7026863
    user7026863 over 7 years
    suppose my text file names are a.txt b.txt c.txt d.txt while merging all in e.txt can i get a dynamic separator that is text file name itself .. ex : a.text- content b.text -content c.text -content. For every separator im lokking to have the name of textfile itself hope u understood my query
  • user7026863
    user7026863 over 7 years
    Thanks big help :)
  • Esperento57
    Esperento57 over 7 years
    Can you clic on up Arrow please ;)
  • user7026863
    user7026863 over 7 years
    Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly di.... done
  • Luis Gouveia
    Luis Gouveia over 4 years
    This command output was an infinite loop and an output file getting larger and larger. The only thing I have changed was the txt to sql.
  • Edward Chew
    Edward Chew about 3 years
    @LuisGouveia Make sure you don't put the output file within the same directory. It will keep reading it causing infinite loop
  • Alex Gordon
    Alex Gordon over 2 years
    how do we ensure that this happens in a specific order? for example i want files merged but by alphabetical order of the file name
  • formicini
    formicini about 2 years
    @AlexGordon add | Sort-Object after -rec. Default for file is by name ascending already so you don't have to pass any param for Sort-Object.