我刚刚开始学习powershell脚本,我遇到了一个我无法弄清楚的问题。我有很多.sql文件。我需要将其文件名插入文件的开头(在USE语句之后)和文件的结尾(在最后一个END语句之前)。有人知道怎么做吗?
非常感谢
问候
答案 0 :(得分:0)
There are a few ways to do that.
Here is the one I would use:
1. Load up a list of your files using get-item
2. loop through those files
3. for each file, load the sql file into a variable using get-content: $d = get-content -path $yourfilename
4. then add your data at the beginning and end
5. $beg = @'
stuff at the beginning
'@
6. $end = @'
stuff at the end
'@
7. $d = $beg + $d + $end
8. use set-content to write the file back
答案 1 :(得分:0)
像这样的东西。远非傻瓜证明,需要检查和填充,可能以更优雅的方式编写,但为您提供整体想法。
param (
[string] $testFile = "testfile.sql",
[string] $result = "testfilemodified.sql"
)
$fileContent = Get-Content $testFile
$lineCount = $fileContent.Count
$count = $lineCount
$fileContent | %{
$line = $_
$count--
$line | Out-File $result -Append
if ($line.Contains("use"))
{ "-- $testFile" | Out-File $result -Append; }
if ($count -eq 1)
{ "-- $testFile" | Out-File $result -Append; }
}