Reputation: 87
I am trying to build a powershell command to extract all files from multiple folder and combine into a single file.
Basefolder >
Release1 > sp01.sql > sp02.sql
Release2 > sp03.sql > sp04.sql > sp05.sql
I need to combine the scripts into single sql file.
The script I tried:
Get-ChildItem $(Pipeline.Workspace)/BaseFolder -Filter "Release*" -include "*.sql" -rec |
ForEach-Object {gc $_; "GO"} |
Out-File $(System.DefaultWorkingDirectory)/combined-script.sql
It returns an empty file. Can you help with this?
Upvotes: 0
Views: 2313
Reputation: 174795
The Get-ChildItem
provider cmdlet is perfectly capable of enumerating and filtering files from multiple directories at once:
$sqlScripts = Get-ChildItem -Path .\path\to\Release1,.\path\to\Release2 -File -Filter *.sql
You can also chain multiple Get-ChildItem
invocations to have the first one feed the base directories to the second:
$sqlScripts = Get-ChildItem -Path .\base\Release* -Directory |Get-ChildItem -File -Filter *.sql
Now you just need to read the contents of all the files and write them to a new one:
$sqlScripts |ForEach-Object { $_ |Get-Content; "GO" } |Set-Content .\path\to\concatenated.sql
Upvotes: 2