guoyu/Archive/Study-Vue-redis_docs/generate_sql_fixed.ps1
2026-01-30 15:08:32 +08:00

100 lines
3.3 KiB
PowerShell

# Courseware SQL Generator (Fixed Encoding)
$sourceFile = "c:\Users\Administrator\Desktop\文件名列表(含子文件夹).txt"
$outputFile = "courseware_import_generated.sql"
$videoExtensions = @('.mp4', '.avi', '.mov', '.wmv', '.flv', '.mkv', '.webm', '.m4v', '.3gp', '.ts', '.MP4')
$documentExtensions = @('.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.txt', '.pdf')
$imageExtensions = @('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp')
function Get-FileType {
param($filename)
$ext = [System.IO.Path]::GetExtension($filename).ToLower()
if ($videoExtensions -contains $ext -or $videoExtensions -contains ".$ext") {
return 'video'
}
elseif ($imageExtensions -contains $ext) {
return 'image'
}
else {
return 'document'
}
}
# Try different encodings
$content = $null
try {
$content = Get-Content $sourceFile -Encoding Default
} catch {
try {
$content = [System.IO.File]::ReadAllLines($sourceFile, [System.Text.Encoding]::GetEncoding('GB2312'))
} catch {
$content = [System.IO.File]::ReadAllLines($sourceFile, [System.Text.Encoding]::UTF8)
}
}
$sqlHeader = @"
-- ========================================
-- Courseware Import SQL Script
-- Generated: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')
-- ========================================
-- Note:
-- 1. file_size field needs to be updated with actual file size
-- 2. subject_id field needs to be filled according to actual situation
-- 3. upload_user_id defaults to 1 (admin)
-- 4. File path format: /profile/upload/2025/11/18/filename
-- ========================================
LOCK TABLES `courseware` WRITE;
/*!40000 ALTER TABLE `courseware` DISABLE KEYS */;
INSERT INTO `courseware` (
`title`, `type`, `file_path`, `file_size`, `file_name`,
`subject_id`, `grade`, `course_id`, `class_id`, `upload_user_id`,
`description`, `duration`, `create_by`, `create_time`,
`update_by`, `update_time`, `remark`
) VALUES
"@
$values = @()
$count = 0
foreach ($line in $content) {
if ($line -match '\\2025\\11\\18\\(.+)$') {
$filename = $matches[1]
$ext = [System.IO.Path]::GetExtension($filename)
$title = [System.IO.Path]::GetFileNameWithoutExtension($filename)
$type = Get-FileType $filename
$filepath = "/profile/upload/2025/11/18/$filename"
# Escape single quotes for SQL
$titleEscaped = $title -replace "'", "''"
$filenameEscaped = $filename -replace "'", "''"
$filepathEscaped = $filepath -replace "'", "''"
$value = "('$titleEscaped', '$type', '$filepathEscaped', 0, '$filenameEscaped', NULL, NULL, NULL, NULL, 1, NULL, NULL, 'admin', NOW(), '', NULL, NULL)"
$values += $value
$count++
}
}
$sqlFooter = @"
/*!40000 ALTER TABLE `courseware` ENABLE KEYS */;
UNLOCK TABLES;
-- ========================================
-- SQL Script Completed
-- Total records: $count
-- ========================================
"@
# Combine all parts
$fullSql = $sqlHeader + ($values -join ",`n") + ";" + $sqlFooter
# Save with UTF-8 BOM encoding
$utf8WithBom = New-Object System.Text.UTF8Encoding $true
[System.IO.File]::WriteAllText($outputFile, $fullSql, $utf8WithBom)
Write-Host "SQL file generated: $outputFile" -ForegroundColor Green
Write-Host "Total files processed: $count" -ForegroundColor Green