topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Saturday December 14, 2024, 9:59 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: DONE: Append/Concatenate/Merge text files, but prepend filenames  (Read 12637 times)

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Any suggestions on the best way to append/concatenate/merge text files, and include filenames (and paths, even better) separating the files.

I've regularly simply used 'copy':

copy *.txt bigfile

But this, of course, does not include filenames between files.

Thoughts appreciated.

Thanks!

Nicholas Kormanik


c.gingerich

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 748
    • View Profile
    • The Blind House
    • Donate to Member
Re: DONE: Append/Concatenate/Merge text files, but prepend filenames
« Reply #1 on: October 03, 2018, 07:31 AM »
@nkormanik Yep I can whip up a quick app.  :Thmbsup:

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: DONE: Append/Concatenate/Merge text files, but prepend filenames
« Reply #2 on: October 03, 2018, 07:48 AM »
Code: PowerShell [Select]
  1. Get-ChildItem ..\*.ps1 | ForEach-Object { Add-Content -Path 'output.txt' "`r`n----------`r`n$($_.FullName)`r`n----------"; Add-Content -Path 'output.txt' -Value (Get-Content $_.Fullname) }

Output:
Code: Text [Select]
  1. ----------
  2. Z:\test\Base64Encode.ps1
  3. ----------
  4. $Content = Get-Content -Path K:\favicon.ico -Encoding Byte
  5. $Base64 = [System.Convert]::ToBase64String($Content)
  6. $Base64 | Out-File K:\encoded.txt
  7.  
  8. ----------
  9. Z:\test\concat.ps1
  10. ----------
  11. Get-ChildItem ..\*.ps1 | ForEach-Object { Add-Content -Path 'output.txt' "`r`n----------`r`n$($_.FullName)`r`n----------"; Get-Content $_ | Out-File 'output.txt' -Append -Encoding utf8 }
  12.  
  13. ----------
  14. Z:\test\Detect-Computers.ps1
  15. ----------
  16. Ping 192.168.0.255
  17. arp -a | select-string 'dynamic' | foreach  {$_.line.trim().split(" ") | select -first 1 } | foreach {(Resolve-DnsName -Name $_ -ea 0).namehost}
  18. (Test-Connection SABnzbd -Quiet -Count 1)
« Last Edit: October 03, 2018, 06:33 PM by 4wd »

c.gingerich

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 748
    • View Profile
    • The Blind House
    • Donate to Member
Re: DONE: Append/Concatenate/Merge text files, but prepend filenames
« Reply #3 on: October 03, 2018, 07:51 AM »
@nkormanik

Give this a try. Extract and run MergeTXT.exe. Drag drop plain text files on to the dialog, set an output file, and click Go. You can optionally set the file name as the separator between text in the output file.

There currently is no way to reorder the files in the list other than how you drop them on the dialog and you can also click on the "File" header in the list to sort them A-Z or Z-A. If this works well for you then I'll add a way to reorder the files [buttons to move the file up or down in the list].

https://filedn.com/l...YliWIkY/MergeTXT.zip
« Last Edit: October 04, 2018, 10:13 PM by c.gingerich »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,190
    • View Profile
    • Donate to Member
Re: DONE: Append/Concatenate/Merge text files, but prepend filenames
« Reply #4 on: October 03, 2018, 03:29 PM »
Code: PowerShell [Select]
  1. Get-ChildItem ..\*.ps1 | ForEach-Object { Add-Content -Path 'output.txt' "----------`r`n$($_.FullName)`r`n----------`r`n"; Get-Content $_ | Out-File 'output.txt' -Append -Encoding utf8 }

Output:
Code: Text [Select]
  1. ----------
  2. Z:\test\Base64Encode.ps1
  3. ----------
  4. $Content = Get-Content -Path K:\favicon.ico -Encoding Byte
  5. $Base64 = [System.Convert]::ToBase64String($Content)
  6. $Base64 | Out-File K:\encoded.txt
  7.  
  8. ----------
  9. Z:\test\concat.ps1
  10. ----------
  11. Get-ChildItem ..\*.ps1 | ForEach-Object { Add-Content -Path 'output.txt' "`r`n----------`r`n$($_.FullName)`r`n----------"; Get-Content $_ | Out-File 'output.txt' -Append -Encoding utf8 }
  12.  
  13. ----------
  14. Z:\test\Detect-Computers.ps1
  15. ----------
  16. Ping 192.168.0.255
  17. arp -a | select-string 'dynamic' | foreach  {$_.line.trim().split(" ") | select -first 1 } | foreach {(Resolve-DnsName -Name $_ -ea 0).namehost}
  18. (Test-Connection SABnzbd -Quiet -Count 1)


Google again?  You have me wanting to use powershell for more than I use it for...

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: DONE: Append/Concatenate/Merge text files, but prepend filenames
« Reply #5 on: October 03, 2018, 05:30 PM »
Only to look up the Add-Content command, otherwise was just a mashup of other small scripts I've done.

Was trying to get Add-Content to work instead of Out-File since it takes care of character encoding depending on the input files without having to specify it like Out-File.

Couldn't seem to get the pipes right though.

I like PowerShell because it reminds me a lot of ARexx on  Amiga, you can write off the cuff small bits of code that leverage the system and other programs, run them instantly, and get reasonably non-cryptic error messages back when you screw up.
Plus it's part of the system, I don't need to install IDE, compiler, etc, etc.

Edit: DUH! Just found how it's done using Add-Content  :-[
Wonderful what happens when you have a good sleep ...
Code: PowerShell [Select]
  1. Add-Content -Path 'output.txt' -Value (Get-Content $_.Fullname)

Modified my original post

Can also throw a Sort in the pipe if a particular order is wanted, eg.

Get-ChildItem ..\*.ps1 | Sort CreationTime | ...
Get-ChildItem ..\*.ps1 | Sort -Descending Length | ...

etc etc
« Last Edit: October 06, 2018, 08:02 AM by 4wd »

cranioscopical

  • Friend of the Site
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,776
    • View Profile
    • Donate to Member
Re: DONE: Append/Concatenate/Merge text files, but prepend filenames
« Reply #6 on: October 04, 2018, 07:53 AM »
I like PowerShell because it reminds me a lot of ARexx on  Amiga
:Thmbsup:

(http://www.rexxla.org/events/2015/presentations/brexx-GergerEva-20141225.pdf)


wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,190
    • View Profile
    • Donate to Member
Re: DONE: Append/Concatenate/Merge text files, but prepend filenames
« Reply #7 on: October 04, 2018, 10:04 AM »
I like PowerShell because it reminds me a lot of ARexx on  Amiga, you can write off the cuff small bits of code that leverage the system and other programs, run them instantly, and get reasonably non-cryptic error messages back when you screw up.

I was going further back to the source in my memories, i.e. REXXw on OS/2

Plus it's part of the system, I don't need to install IDE, compiler, etc, etc.

Yeah, there is that too.  I use csc quite a bit for that reason too for simple things- no external dependencies.  The right tool for the job  :Thmbsup:

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Re: DONE: Append/Concatenate/Merge text files, but prepend filenames
« Reply #8 on: October 04, 2018, 06:33 PM »
4wd, wraith808, cranioscopical...  you guys are great.

Thanks for your help.

Skwire, another one done.


c.gingerich

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 748
    • View Profile
    • The Blind House
    • Donate to Member
Re: DONE: Append/Concatenate/Merge text files, but prepend filenames
« Reply #9 on: October 04, 2018, 10:11 PM »
Guess that’s a no on mine. Well I tried  ;)

I’ll delete the download.

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,190
    • View Profile
    • Donate to Member
Re: DONE: Append/Concatenate/Merge text files, but prepend filenames
« Reply #10 on: October 04, 2018, 11:29 PM »
Guess that’s a no on mine. Well I tried  ;)

I’ll delete the download.

Always good to have alternatives in case needed.  He seems to go with the first solution, which makes sense.  All of the rest of them unless something is wrong with the first solution end up being noise, but not useless.

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Re: DONE: Append/Concatenate/Merge text files, but prepend filenames
« Reply #11 on: October 05, 2018, 02:33 AM »
c.gingerich, 4wd, wraith808, cranioscopical...  you guys are great.

(Who else did I miss??)

Thanks for your help.

Skwire, another one done.


4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: DONE: Append/Concatenate/Merge text files, but prepend filenames
« Reply #12 on: October 05, 2018, 06:26 PM »
Plus you can add simple GUI's to Powershell scripts, (so much easier than AutoIt):

2018-10-06 09_09_07-Concat.pngDONE: Append/Concatenate/Merge text files, but prepend filenames

For the last few Powershell GUIs I've been using PoshGUI to create them rather than screw around in Visual Studio.

Concat-GUI.ps1
Code: PowerShell [Select]
  1. <#
  2. .NAME
  3.     Concat-GUI.ps1
  4. #>
  5.  
  6. Add-Type -AssemblyName System.Windows.Forms
  7. [System.Windows.Forms.Application]::EnableVisualStyles()
  8.  
  9. #region begin GUI{
  10.  
  11. $Form                            = New-Object system.Windows.Forms.Form
  12. $Form.ClientSize                 = '386,237'
  13. $Form.text                       = "Concat"
  14. $Form.TopMost                    = $false
  15.  
  16. $TextBox1                        = New-Object system.Windows.Forms.TextBox
  17. $TextBox1.multiline              = $false
  18. $TextBox1.width                  = 230
  19. $TextBox1.height                 = 20
  20. $TextBox1.location               = New-Object System.Drawing.Point(83,25)
  21. $TextBox1.Font                   = 'Microsoft Sans Serif,10'
  22.  
  23. $Label1                          = New-Object system.Windows.Forms.Label
  24. $Label1.text                     = "Folder:"
  25. $Label1.AutoSize                 = $true
  26. $Label1.width                    = 25
  27. $Label1.height                   = 10
  28. $Label1.location                 = New-Object System.Drawing.Point(22,29)
  29. $Label1.Font                     = 'Microsoft Sans Serif,10'
  30.  
  31. $Button1                         = New-Object system.Windows.Forms.Button
  32. $Button1.text                    = "..."
  33. $Button1.width                   = 34
  34. $Button1.height                  = 25
  35. $Button1.location                = New-Object System.Drawing.Point(328,24)
  36. $Button1.Font                    = 'Microsoft Sans Serif,10'
  37.  
  38. $TextBox2                        = New-Object system.Windows.Forms.TextBox
  39. $TextBox2.multiline              = $false
  40. $TextBox2.width                  = 100
  41. $TextBox2.height                 = 20
  42. $TextBox2.location               = New-Object System.Drawing.Point(83,65)
  43. $TextBox2.Font                   = 'Microsoft Sans Serif,10'
  44.  
  45. $Label2                          = New-Object system.Windows.Forms.Label
  46. $Label2.text                     = "Filter:"
  47. $Label2.AutoSize                 = $true
  48. $Label2.width                    = 25
  49. $Label2.height                   = 10
  50. $Label2.location                 = New-Object System.Drawing.Point(22,69)
  51. $Label2.Font                     = 'Microsoft Sans Serif,10'
  52.  
  53. $TextBox3                        = New-Object system.Windows.Forms.TextBox
  54. $TextBox3.multiline              = $false
  55. $TextBox3.width                  = 230
  56. $TextBox3.height                 = 20
  57. $TextBox3.location               = New-Object System.Drawing.Point(83,104)
  58. $TextBox3.Font                   = 'Microsoft Sans Serif,10'
  59.  
  60. $Button2                         = New-Object system.Windows.Forms.Button
  61. $Button2.text                    = "..."
  62. $Button2.width                   = 34
  63. $Button2.height                  = 25
  64. $Button2.location                = New-Object System.Drawing.Point(328,103)
  65. $Button2.Font                    = 'Microsoft Sans Serif,10'
  66.  
  67. $Label3                          = New-Object system.Windows.Forms.Label
  68. $Label3.text                     = "Output:"
  69. $Label3.AutoSize                 = $true
  70. $Label3.width                    = 25
  71. $Label3.height                   = 10
  72. $Label3.location                 = New-Object System.Drawing.Point(22,110)
  73. $Label3.Font                     = 'Microsoft Sans Serif,10'
  74.  
  75. $CheckBox1                       = New-Object system.Windows.Forms.CheckBox
  76. $CheckBox1.text                  = "Reverse Sort"
  77. $CheckBox1.AutoSize              = $false
  78. $CheckBox1.width                 = 113
  79. $CheckBox1.height                = 20
  80. $CheckBox1.location              = New-Object System.Drawing.Point(234,149)
  81. $CheckBox1.Font                  = 'Microsoft Sans Serif,10'
  82.  
  83. $Button3                         = New-Object system.Windows.Forms.Button
  84. $Button3.text                    = "Start"
  85. $Button3.width                   = 60
  86. $Button3.height                  = 30
  87. $Button3.location                = New-Object System.Drawing.Point(170,185)
  88. $Button3.Font                    = 'Microsoft Sans Serif,10'
  89.  
  90. $Label4                          = New-Object system.Windows.Forms.Label
  91. $Label4.text                     = "Sort by:"
  92. $Label4.AutoSize                 = $true
  93. $Label4.width                    = 25
  94. $Label4.height                   = 10
  95. $Label4.location                 = New-Object System.Drawing.Point(22,146)
  96. $Label4.Font                     = 'Microsoft Sans Serif,10'
  97.  
  98. $ComboBox1                       = New-Object system.Windows.Forms.ComboBox
  99. $ComboBox1.text                  = ""
  100. $ComboBox1.width                 = 100
  101. $ComboBox1.height                = 20
  102. @('Name','Size','Creation','Modification','Accessed') | ForEach-Object {[void] $ComboBox1.Items.Add($_)}
  103. $ComboBox1.location              = New-Object System.Drawing.Point(83,144)
  104. $ComboBox1.Font                  = 'Microsoft Sans Serif,10'
  105. $ComboBox1.SelectedIndex         = 0
  106.  
  107. $Form.controls.AddRange(@($TextBox1,$Label1,$Button1,$TextBox2,$Label2,$TextBox3,$Button2,$Label3,$CheckBox1,$Button3,$Label4,$ComboBox1))
  108.  
  109. #region gui events {
  110. $Button1.Add_Click({
  111.   $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
  112.   $objForm.Description = "Select folder containing files"
  113.   $objForm.SelectedPath = [System.Environment+SpecialFolder]'MyComputer'
  114.   $objForm.ShowNewFolderButton = $false
  115.   $result = $objForm.ShowDialog()
  116.   if ($result -eq "OK") {
  117.     $TextBox1.Text = $objForm.SelectedPath
  118.   } else {
  119.     $TextBox1.Text = ""
  120.   }
  121. })
  122.  
  123. $Button2.Add_Click({
  124. $SaveChooser = New-Object -TypeName System.Windows.Forms.SaveFileDialog
  125. $SaveChooser.ShowDialog()
  126. $TextBox3.Text = $SaveChooser.FileName
  127. })
  128.  
  129. $Button3.Add_Click({
  130.   if ($TextBox2.Text -eq "") { $TextBox2.Text = '*.txt'}
  131.   if (($TextBox1.Text -ne "") -and ($TextBox3.Text -ne "")) {
  132.     Sort-Files
  133.   }
  134. })
  135.  
  136. #endregion events }
  137.  
  138. #endregion GUI }
  139.  
  140. #Write your logic code here
  141.  
  142. Function Sort-Files {
  143.   $files = Get-ChildItem ($TextBox1.Text + '\' + $TextBox2.Text)
  144.   if ($CheckBox1.Checked) {
  145.     switch ($ComboBox1.SelectedItem) {
  146.       'Name' { $sorted = $files | Sort-Object -Property Name -Descending }
  147.       'Size' { $sorted = $files | Sort-Object -Property Length -Descending }
  148.       'Creation' { $sorted = $files | Sort-Object -Property CreationTime -Descending }
  149.       'Modification' { $sorted = $files | Sort-Object -Property LastWriteTime -Descending }
  150.       'Accessed' { $sorted = $files | Sort-Object -Property LastAccessTime -Descending }
  151.     }
  152.   } else {
  153.     switch ($ComboBox1.SelectedItem) {
  154.       'Name' { $sorted = $files | Sort-Object -Property Name }
  155.       'Size' { $sorted = $files | Sort-Object -Property Length }
  156.       'Creation' { $sorted = $files | Sort-Object -Property CreationTime }
  157.       'Modification' { $sorted = $files | Sort-Object -Property LastWriteTime }
  158.       'Accessed' { $sorted = $files | Sort-Object -Property LastAccessTime }
  159.     }
  160.   }
  161.   Merge-Files $sorted
  162. }
  163.  
  164. Function Merge-Files {
  165.   param (
  166.     $objects
  167.   )
  168.   for ($I = 0; $i -lt $objects.Count; $i++) {
  169.     Add-Content -Path $TextBox3.Text "`r`n----------`r`n$($objects[$i].Fullname)`r`n----------"
  170.     Add-Content -Path $TextBox3.Text -Value (Get-Content $objects[$i])
  171.   }
  172. }
  173.  
  174. [void]$Form.ShowDialog()

Filter defaults to *.txt if it's empty when Start is pressed.

Create a shortcut to run it from with the Target as: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy bypass -File "Concat-GUI.ps1"

Guess that’s a no on mine. Well I tried  ;)

I’ll delete the download.

No need to, the more the merrier ... I only do this for some mental exercise and I use little to no error checking in my scripts, (because I'm lazy :D ), so something someone else writes is more likely to be PEBKAC proof.
« Last Edit: October 05, 2018, 06:40 PM by 4wd »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,190
    • View Profile
    • Donate to Member
Re: DONE: Append/Concatenate/Merge text files, but prepend filenames
« Reply #13 on: October 05, 2018, 07:48 PM »
That site is so cool!  Thanks for it!

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: DONE: Append/Concatenate/Merge text files, but prepend filenames
« Reply #14 on: October 05, 2018, 08:46 PM »
That site is so cool!  Thanks for it!

You're welcome, can't get my GUIs to save, (after creating an account), using Save never seems to work ... maybe I'm just running too much anti-tracking/ad stuff in the browser, haven't spent the time to work it out yet.

I just end up using copy/paste to get the rough raw code into PoSh ISE and then fiddle around with the values to add/remove/move things.