Powershell recursive search to chose .txt files then output contents of all files into a single .txt file











up vote
0
down vote

favorite












Any help greatly appreciated.



I have a folder that contains 30+ folders which each have a .txt file that I can search for using:




Get-ChildItem -Filter *.txt -Recurse




I want to read the contents of each .txt file discovered and output the contents int a new .csv file on my desktop that also includes the directory of each .txt file contents being displayed.



The question is twofold,




  1. how to use pipe and powershell commands to read/show all the words in the files.


  2. how to create the csv data that will output both the directory name and the contents of the .txt files.



I can already pipe results to:




c:desktoptest.csv -Encoding ascii -noTypeInformation











share|improve this question




























    up vote
    0
    down vote

    favorite












    Any help greatly appreciated.



    I have a folder that contains 30+ folders which each have a .txt file that I can search for using:




    Get-ChildItem -Filter *.txt -Recurse




    I want to read the contents of each .txt file discovered and output the contents int a new .csv file on my desktop that also includes the directory of each .txt file contents being displayed.



    The question is twofold,




    1. how to use pipe and powershell commands to read/show all the words in the files.


    2. how to create the csv data that will output both the directory name and the contents of the .txt files.



    I can already pipe results to:




    c:desktoptest.csv -Encoding ascii -noTypeInformation











    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Any help greatly appreciated.



      I have a folder that contains 30+ folders which each have a .txt file that I can search for using:




      Get-ChildItem -Filter *.txt -Recurse




      I want to read the contents of each .txt file discovered and output the contents int a new .csv file on my desktop that also includes the directory of each .txt file contents being displayed.



      The question is twofold,




      1. how to use pipe and powershell commands to read/show all the words in the files.


      2. how to create the csv data that will output both the directory name and the contents of the .txt files.



      I can already pipe results to:




      c:desktoptest.csv -Encoding ascii -noTypeInformation











      share|improve this question















      Any help greatly appreciated.



      I have a folder that contains 30+ folders which each have a .txt file that I can search for using:




      Get-ChildItem -Filter *.txt -Recurse




      I want to read the contents of each .txt file discovered and output the contents int a new .csv file on my desktop that also includes the directory of each .txt file contents being displayed.



      The question is twofold,




      1. how to use pipe and powershell commands to read/show all the words in the files.


      2. how to create the csv data that will output both the directory name and the contents of the .txt files.



      I can already pipe results to:




      c:desktoptest.csv -Encoding ascii -noTypeInformation








      powershell pipe get-childitem






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 15:22

























      asked Nov 21 at 13:14









      ElGamba

      133




      133
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          The following script reads all .txt files within a specific directory, stores the full filename and path as well as the files content into an array and saves it as a csv.



          $csvOut = @()
          Get-ChildItem -LiteralPath C:temp -Filter *.txt -File -Recurse | foreach {

          $fileData = @{
          "File"=$_.FullName;
          "Content"=(Get-Content -LiteralPath $_.FullName -Raw)
          }
          $csvOut += (New-Object psobject -Property $fileData)
          }

          $csvOut | Export-Csv -LiteralPath "C:tempcsvout.csv" -NoTypeInformation





          share|improve this answer























          • Helpful, but one stage is still missing. I need to recursively find all the *.txt files because they are contained in separate directories within a grandparent directory. Example: C:Grandparentparent1test1.txt C:Grandparentparent2test2.txt I was able to use Get-ChildItem -Filter *.txt -Recursive to get the list of all the test1.txt and testn.txt files but then was getting stuck at how to pipe the search into the foreach statement.
            – ElGamba
            Nov 22 at 15:04












          • Just add -Recurse to the Get-ChildItem command. I've adjusted my answer accordingly.
            – TobyU
            Nov 22 at 15:13










          • Thank you. This is great. Could you point me in the direction of what -LiteralPath means (the resources online I can't find a simple explanation.)
            – ElGamba
            Nov 22 at 15:39










          • Unlike the Path parameter, the value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. Soure: docs.microsoft.com/en-us/powershell/module/…
            – TobyU
            Nov 22 at 15:40










          • Thank you Toby - Legend!
            – ElGamba
            Nov 22 at 15:42











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412874%2fpowershell-recursive-search-to-chose-txt-files-then-output-contents-of-all-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          The following script reads all .txt files within a specific directory, stores the full filename and path as well as the files content into an array and saves it as a csv.



          $csvOut = @()
          Get-ChildItem -LiteralPath C:temp -Filter *.txt -File -Recurse | foreach {

          $fileData = @{
          "File"=$_.FullName;
          "Content"=(Get-Content -LiteralPath $_.FullName -Raw)
          }
          $csvOut += (New-Object psobject -Property $fileData)
          }

          $csvOut | Export-Csv -LiteralPath "C:tempcsvout.csv" -NoTypeInformation





          share|improve this answer























          • Helpful, but one stage is still missing. I need to recursively find all the *.txt files because they are contained in separate directories within a grandparent directory. Example: C:Grandparentparent1test1.txt C:Grandparentparent2test2.txt I was able to use Get-ChildItem -Filter *.txt -Recursive to get the list of all the test1.txt and testn.txt files but then was getting stuck at how to pipe the search into the foreach statement.
            – ElGamba
            Nov 22 at 15:04












          • Just add -Recurse to the Get-ChildItem command. I've adjusted my answer accordingly.
            – TobyU
            Nov 22 at 15:13










          • Thank you. This is great. Could you point me in the direction of what -LiteralPath means (the resources online I can't find a simple explanation.)
            – ElGamba
            Nov 22 at 15:39










          • Unlike the Path parameter, the value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. Soure: docs.microsoft.com/en-us/powershell/module/…
            – TobyU
            Nov 22 at 15:40










          • Thank you Toby - Legend!
            – ElGamba
            Nov 22 at 15:42















          up vote
          1
          down vote













          The following script reads all .txt files within a specific directory, stores the full filename and path as well as the files content into an array and saves it as a csv.



          $csvOut = @()
          Get-ChildItem -LiteralPath C:temp -Filter *.txt -File -Recurse | foreach {

          $fileData = @{
          "File"=$_.FullName;
          "Content"=(Get-Content -LiteralPath $_.FullName -Raw)
          }
          $csvOut += (New-Object psobject -Property $fileData)
          }

          $csvOut | Export-Csv -LiteralPath "C:tempcsvout.csv" -NoTypeInformation





          share|improve this answer























          • Helpful, but one stage is still missing. I need to recursively find all the *.txt files because they are contained in separate directories within a grandparent directory. Example: C:Grandparentparent1test1.txt C:Grandparentparent2test2.txt I was able to use Get-ChildItem -Filter *.txt -Recursive to get the list of all the test1.txt and testn.txt files but then was getting stuck at how to pipe the search into the foreach statement.
            – ElGamba
            Nov 22 at 15:04












          • Just add -Recurse to the Get-ChildItem command. I've adjusted my answer accordingly.
            – TobyU
            Nov 22 at 15:13










          • Thank you. This is great. Could you point me in the direction of what -LiteralPath means (the resources online I can't find a simple explanation.)
            – ElGamba
            Nov 22 at 15:39










          • Unlike the Path parameter, the value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. Soure: docs.microsoft.com/en-us/powershell/module/…
            – TobyU
            Nov 22 at 15:40










          • Thank you Toby - Legend!
            – ElGamba
            Nov 22 at 15:42













          up vote
          1
          down vote










          up vote
          1
          down vote









          The following script reads all .txt files within a specific directory, stores the full filename and path as well as the files content into an array and saves it as a csv.



          $csvOut = @()
          Get-ChildItem -LiteralPath C:temp -Filter *.txt -File -Recurse | foreach {

          $fileData = @{
          "File"=$_.FullName;
          "Content"=(Get-Content -LiteralPath $_.FullName -Raw)
          }
          $csvOut += (New-Object psobject -Property $fileData)
          }

          $csvOut | Export-Csv -LiteralPath "C:tempcsvout.csv" -NoTypeInformation





          share|improve this answer














          The following script reads all .txt files within a specific directory, stores the full filename and path as well as the files content into an array and saves it as a csv.



          $csvOut = @()
          Get-ChildItem -LiteralPath C:temp -Filter *.txt -File -Recurse | foreach {

          $fileData = @{
          "File"=$_.FullName;
          "Content"=(Get-Content -LiteralPath $_.FullName -Raw)
          }
          $csvOut += (New-Object psobject -Property $fileData)
          }

          $csvOut | Export-Csv -LiteralPath "C:tempcsvout.csv" -NoTypeInformation






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 at 15:12

























          answered Nov 21 at 13:25









          TobyU

          1,915317




          1,915317












          • Helpful, but one stage is still missing. I need to recursively find all the *.txt files because they are contained in separate directories within a grandparent directory. Example: C:Grandparentparent1test1.txt C:Grandparentparent2test2.txt I was able to use Get-ChildItem -Filter *.txt -Recursive to get the list of all the test1.txt and testn.txt files but then was getting stuck at how to pipe the search into the foreach statement.
            – ElGamba
            Nov 22 at 15:04












          • Just add -Recurse to the Get-ChildItem command. I've adjusted my answer accordingly.
            – TobyU
            Nov 22 at 15:13










          • Thank you. This is great. Could you point me in the direction of what -LiteralPath means (the resources online I can't find a simple explanation.)
            – ElGamba
            Nov 22 at 15:39










          • Unlike the Path parameter, the value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. Soure: docs.microsoft.com/en-us/powershell/module/…
            – TobyU
            Nov 22 at 15:40










          • Thank you Toby - Legend!
            – ElGamba
            Nov 22 at 15:42


















          • Helpful, but one stage is still missing. I need to recursively find all the *.txt files because they are contained in separate directories within a grandparent directory. Example: C:Grandparentparent1test1.txt C:Grandparentparent2test2.txt I was able to use Get-ChildItem -Filter *.txt -Recursive to get the list of all the test1.txt and testn.txt files but then was getting stuck at how to pipe the search into the foreach statement.
            – ElGamba
            Nov 22 at 15:04












          • Just add -Recurse to the Get-ChildItem command. I've adjusted my answer accordingly.
            – TobyU
            Nov 22 at 15:13










          • Thank you. This is great. Could you point me in the direction of what -LiteralPath means (the resources online I can't find a simple explanation.)
            – ElGamba
            Nov 22 at 15:39










          • Unlike the Path parameter, the value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. Soure: docs.microsoft.com/en-us/powershell/module/…
            – TobyU
            Nov 22 at 15:40










          • Thank you Toby - Legend!
            – ElGamba
            Nov 22 at 15:42
















          Helpful, but one stage is still missing. I need to recursively find all the *.txt files because they are contained in separate directories within a grandparent directory. Example: C:Grandparentparent1test1.txt C:Grandparentparent2test2.txt I was able to use Get-ChildItem -Filter *.txt -Recursive to get the list of all the test1.txt and testn.txt files but then was getting stuck at how to pipe the search into the foreach statement.
          – ElGamba
          Nov 22 at 15:04






          Helpful, but one stage is still missing. I need to recursively find all the *.txt files because they are contained in separate directories within a grandparent directory. Example: C:Grandparentparent1test1.txt C:Grandparentparent2test2.txt I was able to use Get-ChildItem -Filter *.txt -Recursive to get the list of all the test1.txt and testn.txt files but then was getting stuck at how to pipe the search into the foreach statement.
          – ElGamba
          Nov 22 at 15:04














          Just add -Recurse to the Get-ChildItem command. I've adjusted my answer accordingly.
          – TobyU
          Nov 22 at 15:13




          Just add -Recurse to the Get-ChildItem command. I've adjusted my answer accordingly.
          – TobyU
          Nov 22 at 15:13












          Thank you. This is great. Could you point me in the direction of what -LiteralPath means (the resources online I can't find a simple explanation.)
          – ElGamba
          Nov 22 at 15:39




          Thank you. This is great. Could you point me in the direction of what -LiteralPath means (the resources online I can't find a simple explanation.)
          – ElGamba
          Nov 22 at 15:39












          Unlike the Path parameter, the value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. Soure: docs.microsoft.com/en-us/powershell/module/…
          – TobyU
          Nov 22 at 15:40




          Unlike the Path parameter, the value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. Soure: docs.microsoft.com/en-us/powershell/module/…
          – TobyU
          Nov 22 at 15:40












          Thank you Toby - Legend!
          – ElGamba
          Nov 22 at 15:42




          Thank you Toby - Legend!
          – ElGamba
          Nov 22 at 15:42


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412874%2fpowershell-recursive-search-to-chose-txt-files-then-output-contents-of-all-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...