Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PowerShell,如何根据SQL文件的内容将其拆分为多个文件?_Sql_Powershell_Text - Fatal编程技术网

使用PowerShell,如何根据SQL文件的内容将其拆分为多个文件?

使用PowerShell,如何根据SQL文件的内容将其拆分为多个文件?,sql,powershell,text,Sql,Powershell,Text,我正在尝试使用PowerShell根据标题的位置将SQL文件自动划分为不同的文件 要拆分的SQL文件示例如下: /**************************************** Section 1 ****************************************/ Select 1 /**************************************** Section 2 **************************************

我正在尝试使用PowerShell根据标题的位置将SQL文件自动划分为不同的文件

要拆分的SQL文件示例如下:

/****************************************
Section 1
****************************************/
Select 1

/****************************************
Section 2
****************************************/
Select 2

/****************************************
Section 3
****************************************/
Select 3
我希望新文件按照文件中的章节标题命名,即“第1节”、“第2节”和“第3节”。第一个文件的内容应如下所示:

/****************************************
Section 1
****************************************/
Select 1
字符串:
/******************************************
仅在SQL文件中用于节标题,因此可用于标识节的开头。文件名将始终是直接在下面一行的文本。

您可以这样尝试(拆分是基于节之间的空行):


下面的代码使用注释块中的标题名称。它还根据注释块的位置将SQL文件拆分为多个SQL文件

#load SQL file contents in an array
$SQL = Get-Content "U:\Test\FileToSplit.sql"
$OutputPath = "U:\TestOutput"

#find first section name and count number of sections
$sectioncounter = 0
$checkcounter = 0
$filenames = @()

$SQL | % {
    #Add file name to array if new section was found on the previous line
    If ($checkcounter -lt $sectioncounter) 
    {
        $filenames += $_
        $checkcounter = $sectioncounter
    }
    Else
    {
        If ($_.StartsWith("/*")) 
        {
            $sectioncounter += 1
        }
    }
}

#return if too many sections were found
If ($sectioncounter > 50) { return "Too many sections found"}

$sectioncounter = 0
$endcommentcounter = 0

#for each line of the SQL file (Ref: sodawillow)
$SQL | % {

    #if new comment block is found point to next section name, unless its the start of the first section
    If ($_.StartsWith("/*") -And ($endcommentcounter -gt 0))
    {
        $sectioncounter += 1
    }

    If ($_.EndsWith("*/"))
    {
        $endcommentcounter += 1
    }

    #build output path
    $tempfilename = $filenames[$sectioncounter]
    $outFile = "$OutputPath\$tempfilename.sql"

    #push line to the current output file (appending to existing contents)
    $_ | Out-File $outFile -Append
}

你试过什么了吗?@Mike不,我没有试过,因为我是新来的PowerShell。似乎可以使用.NET StreamReader类和“添加内容”cmdlet完成此操作,如对以下问题的回答所示:“”。然而,这个问题和我的太不一样了,我无法使用这个答案。我已经添加了我的最终答案,因为我已经修改并添加了脚本的一些部分,以便在我的实际SQL文件中正确查找节名和中断,这些文件可能在一个节中包含多行返回。
#load SQL file contents in an array
$SQL = Get-Content "U:\Test\FileToSplit.sql"
$OutputPath = "U:\TestOutput"

#find first section name and count number of sections
$sectioncounter = 0
$checkcounter = 0
$filenames = @()

$SQL | % {
    #Add file name to array if new section was found on the previous line
    If ($checkcounter -lt $sectioncounter) 
    {
        $filenames += $_
        $checkcounter = $sectioncounter
    }
    Else
    {
        If ($_.StartsWith("/*")) 
        {
            $sectioncounter += 1
        }
    }
}

#return if too many sections were found
If ($sectioncounter > 50) { return "Too many sections found"}

$sectioncounter = 0
$endcommentcounter = 0

#for each line of the SQL file (Ref: sodawillow)
$SQL | % {

    #if new comment block is found point to next section name, unless its the start of the first section
    If ($_.StartsWith("/*") -And ($endcommentcounter -gt 0))
    {
        $sectioncounter += 1
    }

    If ($_.EndsWith("*/"))
    {
        $endcommentcounter += 1
    }

    #build output path
    $tempfilename = $filenames[$sectioncounter]
    $outFile = "$OutputPath\$tempfilename.sql"

    #push line to the current output file (appending to existing contents)
    $_ | Out-File $outFile -Append
}