Skip to content

Instantly share code, notes, and snippets.

@joram77
Last active July 15, 2023 10:55
Show Gist options
  • Save joram77/ac836874ac375d9645103c1f91696334 to your computer and use it in GitHub Desktop.
Save joram77/ac836874ac375d9645103c1f91696334 to your computer and use it in GitHub Desktop.
# Powershell script to split large sql files to separate sql files per table - joram77
$inputFile = "c:\users\money\dumps\db_slimmed.sql"
$fileInfo = New-Object System.IO.FileInfo($inputFile)
$newDir = $fileInfo.Directory.ToString() + '\' + $fileInfo.BaseName.ToString()
New-Item -ItemType Directory -Force -Path $newDir
$outputFile = "$newDir\db_slimmed_start.sql"
$reader = New-Object System.IO.StreamReader($inputFile)
$count = 0
#switch -File $inputFile { default { ++$count } }
$count= [System.Linq.Enumerable]::Count(
[System.IO.File]::ReadLines((ls $inputFile).FullName))
$tableName = ''
$lNum = 1
While (($line = $reader.ReadLine()) -ne $null) {
echo "Table $tableName $lNum / $count"
If ($line -match "-- Table structure for") { # eg -- Table structure for table `users`
$aLine = $line.split('`')
$tableName = $aLine[1]
echo "Table $tableName"
$outputFile = "$newDir\db_slimmed_$tableName.sql"
}
$lNum++
Add-Content $outputFile $line
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment