Using PowerShell, read several well-known file names, add the text of all files, create and write to one output file

I have five .sql files and know the name of each file. For this example, name them one.sql, two.sql, three.sql, four.sql, and five.sql. I want to add the text of all files and create a single file named master.sql. How to do it in PowerShell? Feel free to post some answers to this problem, because I'm sure there are several ways to do this.

My attempt does not work and creates a file with several hundred lines.

PS C:\sql> get-content '.\one.sql' | get-content '.\two.sql' | get-content '.\three.sql' | get-content '.\four.sql' | get-content '.\five.sql' | out-file -encoding UNICODE master.sql
+3
source share
6 answers
Get-Content one.sql,two.sql,three.sql,four.sql,five.sql > master.sql

Note that is >equivalent Out-File -Encoding Unicode. I just use Out-File when I need to specify a different encoding.

+9

, , , , , :

$vara = get-childitem -name "path"

$varb = foreach ($a in $vara) {gc "path\$a"}

$vara = get-childitem -name "c:\users\test"

$varb = foreach ($a in $vara) {gc "c:\users\test\$a"}

, , | add-content - , , .

+4

,

get-childitem "one.sql", "two.sql", "three.sql", "four.sql", "five.sql" | get-content | out-file -encoding UNICODE master.sql

+1

, Add-Content

$files = Get-ChildItem '.\one.sql', '.\two.sql', '.\three.sql', '.\four.sql', '.\five.sql'
$files | foreach { Get-Content $_ | Add-Content '.\master.sql' -encoding UNICODE }

hoverwer Get-Content . , : http://keithhill.spaces.live.com/blog/cns!5A8D2641E0963A97!756.entry

0

:

Get-Content .\one.sql,.\two.sql,.\three.sql,.\four.sql,.\five.sql | Set-Content .\master.sql
0

I needed something similar, Chris Berry's message helped, but I think it is more efficient:

gci -name "*PathToFiles*" | gc > master.sql

The first part gci -name "*PathToFiles*"will bring you a list of files. This can be done using wildcards to just get your .sql files, i.e.gci -name "\\share\folder\*.sql"

Then it connects to Get-Content and redirects the output to the master.sql file. As Kieth Hill noted, you can use Out-File instead of> to better control your output if necessary.

0
source

Source: https://habr.com/ru/post/1785232/


All Articles