<#
Nama Script : MdFileToFolder.ps1
Deskripsi :
Convert file .md menjadi folder dengan nama yang sama.
Opsi index per folder:
1 → buat index.md kosong di dalam folder
0 → tidak buat index.md
[teks] → buat index.md dengan isi = teks yang diinput
#>
# ========================================================
# 📂 Input path folder target
# ========================================================
do {
$targetFolder = Read-Host "📂 Masukkan path folder (contoh: D:\vault\02-Universitas)"
if (-not (Test-Path $targetFolder)) {
Write-Host "`n❌ Path tidak ditemukan. Periksa kembali!" -ForegroundColor Red
$retry = $true
} else {
$retry = $false
}
} while ($retry)
# ========================================================
# 📝 Input opsi index
# ========================================================
Write-Host "`n─────────────────────────────────────────" -ForegroundColor DarkGray
Write-Host " Opsi index.md di dalam setiap folder:" -ForegroundColor Cyan
Write-Host " 1 → buat index.md kosong" -ForegroundColor Cyan
Write-Host " 0 → tidak buat index.md" -ForegroundColor Cyan
Write-Host " [teks] → buat index.md dengan isi teks tersebut" -ForegroundColor Cyan
Write-Host "─────────────────────────────────────────" -ForegroundColor DarkGray
$indexInput = Read-Host "Pilih opsi"
$indexInput = $indexInput.Trim()
# Tentukan mode
if ($indexInput -eq "0") {
$indexMode = "none"
Write-Host "`n⚙️ Mode: Tidak membuat index.md" -ForegroundColor Yellow
}
elseif ($indexInput -eq "1") {
$indexMode = "empty"
Write-Host "`n⚙️ Mode: Buat index.md kosong" -ForegroundColor Yellow
}
else {
$indexMode = "content"
$indexContent = $indexInput
Write-Host "`n⚙️ Mode: Buat index.md dengan isi: `"$indexContent`"" -ForegroundColor Yellow
}
# ========================================================
# 🔍 Preview dulu sebelum eksekusi
# ========================================================
$mdFiles = Get-ChildItem -Path $targetFolder -Filter "*.md" -File | Sort-Object Name
if ($mdFiles.Count -eq 0) {
Write-Host "`n⚠️ Tidak ada file .md di folder ini." -ForegroundColor Red
exit
}
Write-Host "`n─────────────────────────────────────────" -ForegroundColor DarkGray
Write-Host " Preview: $($mdFiles.Count) file akan dikonversi" -ForegroundColor Cyan
Write-Host "─────────────────────────────────────────" -ForegroundColor DarkGray
foreach ($file in $mdFiles) {
$folderName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
Write-Host " 📄 $($file.Name) → 📁 $folderName\" -ForegroundColor DarkGray
}
Write-Host ""
$confirm = Read-Host "Lanjutkan konversi? (Y/N)"
if ($confirm.Trim().ToLower() -ne "y") {
Write-Host "❌ Dibatalkan." -ForegroundColor Yellow
exit
}
# ========================================================
# 🚀 Eksekusi konversi
# ========================================================
$successCount = 0
$skipCount = 0
$errorCount = 0
foreach ($file in $mdFiles) {
$folderName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
$newFolderPath = Join-Path $targetFolder $folderName
$newFilePath = Join-Path $newFolderPath $file.Name
try {
# Buat folder kalau belum ada
if (-not (Test-Path $newFolderPath)) {
New-Item -ItemType Directory -Path $newFolderPath | Out-Null
}
# Pindahkan file .md ke dalam folder
Move-Item -Path $file.FullName -Destination $newFilePath -Force
# Buat index.md sesuai mode
$indexPath = Join-Path $newFolderPath "index.md"
switch ($indexMode) {
"empty" {
New-Item -ItemType File -Path $indexPath -Force | Out-Null
}
"content" {
Set-Content -Path $indexPath -Value $indexContent -Encoding UTF8
}
"none" {
# tidak buat apa-apa
}
}
Write-Host " ✅ $($file.Name) → $folderName\" -ForegroundColor Green
$successCount++
}
catch {
Write-Host " ❌ Gagal: $($file.Name) — $_" -ForegroundColor Red
$errorCount++
}
}
# ========================================================
# 📊 Summary
# ========================================================
Write-Host "`n─────────────────────────────────────────" -ForegroundColor DarkGray
Write-Host " ✅ Berhasil : $successCount file" -ForegroundColor Green
Write-Host " ⏭️ Dilewati : $skipCount file" -ForegroundColor Yellow
Write-Host " ❌ Gagal : $errorCount file" -ForegroundColor Red
Write-Host "─────────────────────────────────────────`n" -ForegroundColor DarkGray