使用PowerShell,SQL文件如何根据其内容分割成多个文件?

问题描述:

我想使用PowerShell根据标题的位置自动将SQL文件划分为单独的文件。使用PowerShell,SQL文件如何根据其内容分割成多个文件?

被分裂的一个例子SQL文件如下:

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

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

/**************************************** 
Section 3 
****************************************/ 
Select 3 

我想新的文件将被命名为每个文件即“第1条”一节的标题,“第2”和“科3' 。第一个文件的内容应该如下:

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

字符串:/****************************************仅在为副标题的SQL文件中使用,因此可用于标识节的开始。文件名将始终是正下方的文本。

+3

你尝试过什么了吗? –

+0

@Mike不,因为我是PowerShell的新手。它看起来可以使用.NET StreamReader类和“Add-Content”cmdlet完成,如下面问题的答案:“http://stackoverflow.com/questions/1001776/how-can-i-split -a文本文件 - 使用 - PowerShell的”。但是,这个问题与我的不同之处在于我能够使用答案。 – Fletch

下面的代码使用注释块中找到的标题名称。它还根据注释块的位置将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 
} 

你可以尝试这样的(分开这里基于部分之间的空行):

#create an index for our output files 
$fileIndex = 1 

#load SQLite file contents in an array 
$sqlite = Get-Content "G:\input\sqlite.txt" 

#for each line of the SQLite file 
$sqlite | % { 


    if($_ -eq "") { 

     #if the line is empty, increment output file index to create a new file 
     $fileindex++ 

    } else { 

     #if the line is not empty 
     #build output path 
     $outFile = "G:\output\section$fileindex.txt" 

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

    } 

} 

#load generated files in an array 
$tempfiles = Get-ChildItem "G:\output" 

#for each file 
$tempfiles | % { 

    #load file contents in an array 
    $data = Get-Content $_.FullName 

    #rename file after second line contents 
    Rename-Item $_.FullName "$($data[1]).txt" 
} 
+0

+1,因为这个答案对我很有帮助。我已经添加了我的最终答案,因为我已经修改并添加了脚本的各个部分,以便在我的实际SQL文件中正确地查找段名称和中断,这些文件可能包含段中的多个行返回。 – Fletch