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,
how to use pipe and powershell commands to read/show all the words in the files.
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
add a comment |
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,
how to use pipe and powershell commands to read/show all the words in the files.
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
add a comment |
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,
how to use pipe and powershell commands to read/show all the words in the files.
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
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,
how to use pipe and powershell commands to read/show all the words in the files.
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
powershell pipe get-childitem
edited Nov 22 at 15:22
asked Nov 21 at 13:14
ElGamba
133
133
add a comment |
add a comment |
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
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 theGet-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 thePath
parameter, the value ofLiteralPath
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
add a comment |
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
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 theGet-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 thePath
parameter, the value ofLiteralPath
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
add a comment |
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
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 theGet-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 thePath
parameter, the value ofLiteralPath
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
add a comment |
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
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
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 theGet-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 thePath
parameter, the value ofLiteralPath
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
add a comment |
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 theGet-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 thePath
parameter, the value ofLiteralPath
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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