Just Code‎ > ‎

Powershell - Concat all .SQL Files into one big file, with GO statement in between

posted Oct 27, 2011, 3:36 AM by Peter Henell   [ updated Jan 16, 2012, 2:08 PM ]
This is a short example of how to take every .sql script file in a directory and create one big file from their content. Between every file we will add the GO statement and a NewLine.

Get All Files in $path
    For each file
        Read all the text content
        Append "GO" 
        Append a NewLine
        Write the result to the output file
        
$path = ".\src\"
$outputFile = "..\out.sql"

[System.IO.Directory]::GetFiles($path) | ForEach-Object { [System.IO.File]::ReadAllText($_) + [Environment]::NewLine + "GO " + [Environment]::NewLine } | out-file -filepath $outputFile -append
Comments