Powershell help, how can I combine text files in a directory to one file?
I'm new to powershell. I'm trying to convert some of our database scripts to powershell scripts.开发者_开发技巧 One thing we do in our script currently (DOS BATCH FILE) is use the Type command...
@ECHO OFF
DEL *.sql 1>NUL 2>&1
TYPE ..\db\database\TuscanyProfileDB.sql > CreateDB.sql
TYPE ..\db\tbls\*.sql > CreateTables.sql
TYPE ..\db\foreignKeys\*.sql > CreateForeignKeys.sql
TYPE ..\db\indexes\*.sql > CreateIndexes.sql
TYPE ..\db\sprocs\*.sql > CreateSprocs.sql
It basically goes into the specified folder, and concatenates all the files with the .sql
file extension and combines them into a new file.
My question is how can I do this in powershell?
Remove-Item *.sql
Get-Content ..\db\database\TuscanyProfileDB.sql | Add-Content CreateDB.sql
Get-Content ..\db\tbls\*.sql | Add-Content CreateTables.sql
Get-Content ..\db\foreignKeys\*.sql | Add-Content CreateForeignKeys.sql
Get-Content ..\db\indexes\*.sql | Add-Content CreateIndexes.sql
Get-Content ..\db\sprocs\*.sql | Add-Content CreateSprocs.sql
Here's what I use, adapted from here.
In my case I tend to have many directories, each one has many SQL files and I want to create a separate merged SQL file for each directory. I only want to merge the files that are prefixed with a number (e.g. 001_blah.sql
) and my merged file gets named upgrade_{DIRECTORY}.sql (e.g. merged_1370_to_1380.sql
).
I put my merge_sql_files.ps1
file in the parent directory and I put a merge.bat
file in each of the directories with the SQL files.
merge.bat
file looks like this
@ECHO OFF
SET PsFile=merge_sql_files.ps1
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%..\%PsFile%
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'";
merge_sql_files.ps1
looks like this:
# Script to merge SQL files together.
# Takes all the SQL files that start with a number and concats
# them together, putting a line with "GO" in betweeen each one.
# The output is saved as upgrade_[DirectoryName].sql, e.g.
# 'upgrade_1370_to_1380.sql'.
#
# This script should be run from the folder with the sql files,
# e.g. put a merge.bat file in the folder that runs this ps1 script.
$path = (Resolve-Path .\).Path
$dirName = (get-item $path).Name
$outFile = "${path}\upgrade_${dirName}.sql"
if((Test-Path $outFile) -eq $true) {Remove-Item -Path $outFile -Force}
# Get all the SQL files that start with a number. Sort them
$files = Get-ChildItem -LiteralPath $path -Include "*.sql" | where {$_.Name -match "^[0-9]" } | Sort-Object -Property Name
New-Item -ItemType file -Path $outFile -Force | Out-Null
foreach($file in $files)
{
Write-Host "Appending file $file..." -ForegroundColor Gray
$content = Get-Content -Path $file.FullName
Add-Content -Path $outFile "----------------------------------------------------------------------------------------------------------------------------------------------------------------"
Add-Content -Path $outFile "-- $File"
Add-Content -Path $outFile "----------------------------------------------------------------------------------------------------------------------------------------------------------------"
Add-Content -Path $outFile $content
Add-Content -Path $outFile "GO`r`n"
}
Write-Host "Completed file $outFile" -ForegroundColor DarkGreen
# If running in the console, wait for input before closing.
if ($Host.Name -eq "ConsoleHost")
{
Write-Host "Press any key to continue..."
$Host.UI.RawUI.FlushInputBuffer() # Make sure buffered input doesn't "press a key" and skip the ReadKey().
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null
}
精彩评论