You need to enable JavaScript to run this app. PowerShell笔记管理脚本开发:实现按行号删除TXT文件指定行功能
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell笔记管理脚本开发:实现按行号删除TXT文件指定行功能

改造后完整可运行脚本
# 初始化全局配置
$answer = 1
$notePath = Join-Path (Get-Location) "DONTREMOVEME.txt"

DO {
    # 主菜单
    Write-Host "`nWhat u want?"
    Write-Host "1.Add note"
    Write-Host "2.Read notes"
    Write-Host "3.Delete note"
    Write-Host "4.Exit"
    $answer = Read-Host "Write a number between 1 to 4 and press Enter"

    # 新增笔记逻辑
    if ($answer -eq 1) 
    {
        $res = ""
        DO {  
            $msg = Read-Host "Write the note and press Enter:"
            Add-Content $notePath "- $msg"
            $res = Read-Host "Added, want to add one more? y/n"
        } while ($res -ne "n")

        Write-Host "...back to the main menu"
        Start-Sleep -s 2
    }

    # 读取笔记逻辑
    if ($answer -eq 2) 
    {
        if (-not (Test-Path $notePath)) {
            Write-Host "No notes saved yet!"
            Read-Host "Press Enter to go back to the main menu"
            continue
        }
        Get-Content $notePath
        Read-Host "Press Enter to go back to the main menu"
    }

    # 删除笔记核心逻辑
    if ($answer -eq 3)
    {
        # 无笔记场景拦截
        if (-not (Test-Path $notePath) -or (Get-Content $notePath).Count -eq 0) {
            Write-Host "No notes available to delete!"
            Start-Sleep -s 2
            continue
        }

        # 读取全量笔记到内存数组
        $allNotes = Get-Content $notePath
        Write-Host "`nAll saved notes:"
        # 行号仅控制台临时展示,不写入文件,和内容用固定分隔符隔开避免混淆
        for ($i = 0; $i -lt $allNotes.Count; $i++) {
            Write-Host ("{0}. {1}" -f ($i + 1), $allNotes[$i])
        }

        # 输入合法性校验
        $deleteInput = Read-Host "`nWhich note you want removed? Type the row number and press Enter"
        if (-not [int]::TryParse($deleteInput, [ref]$null)) {
            Write-Host "Invalid input! Please enter a number."
            Start-Sleep -s 2
            continue
        }
        $deleteLine = [int]$deleteInput
        if ($deleteLine -lt 1 -or $deleteLine -gt $allNotes.Count) {
            Write-Host "Row number out of range! Please enter a number between 1 and $($allNotes.Count)."
            Start-Sleep -s 2
            continue
        }

        # 数组切片剔除目标行,直接生成新内容
        $updatedNotes = $allNotes[0..($deleteLine - 2)] + $allNotes[($deleteLine)..($allNotes.Count - 1)]
        # 全量覆盖写回文件,实现真正删除无空行占位
        Set-Content $notePath $updatedNotes
        Write-Host "Note deleted successfully!"
        Start-Sleep -s 2
    }

} While ($answer -ne 4) 

Write-Host "Bye."

约束适配说明

  • 完全规避内容匹配逻辑:删除操作仅基于内存数组的固定索引执行,和笔记实际内容无关,哪怕多条笔记内容完全一致,也可通过行号精准删除
  • 行号仅临时展示:控制台输出的行号仅用于用户交互,不会修改DONTREMOVEME.txt的存储内容,笔记本身包含的数字不会和行号产生混淆
  • 实现真实行删除:通过数组切片直接剔除目标行后全量覆盖写回文件,不会留下空行占位
  • 兼容原脚本逻辑:保留原有新增、读取功能的使用习惯,仅修复了原脚本中Read-Host参数未加引号、变量拼写错误、循环未初始化等隐藏bug

内容的提问来源于stack exchange,提问作者Monkeyonkeyboard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.27 00:45:03