Powershell求助,如何将目录中的文本文件合并为一个文件?

发布于 2024-11-02 10:39:57 字数 526 浏览 4 评论 0原文

我是 powershell 新手。我正在尝试将一些数据库脚本转换为 powershell 脚本。目前我们在脚本(DOS 批处理文件)中做的一件事是使用 Type 命令...

@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

它基本上进入指定的文件夹,并将所有带有 .sql 文件扩展名的文件连接起来并将它们组合起来到一个新文件中。

我的问题是如何在 powershell 中执行此操作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久伴你 2024-11-09 10:39:57
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
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
咿呀咿呀哟 2024-11-09 10:39:57

这是我使用的内容,改编自

就我而言,我往往有很多目录,每个目录都有很多 SQL 文件,我想为每个目录创建一个单独的合并 SQL 文件。我只想合并以数字为前缀的文件(例如001_blah.sql),并且我的合并文件被命名为upgrade_{DIRECTORY}.sql(例如merged_1370_to_1380.sql )。

我将 merge_sql_files.ps1 文件放在父目录中,并将 merge.bat 文件放在包含 SQL 文件的每个目录中。

merge.bat 文件看起来像这样

@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 看起来像这样:

# 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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文