|
|
发表于 2021-12-9 13:27:30
|
显示全部楼层
- <#*,:&cls
- @echo off
- cd /d "%~dp0"
- powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath "%~0" -ReadCount 0 | Out-String ))) "
- exit /b
- #>
- # 源文件
- $srcfile = "src.txt"
- $encoding = [System.Text.Encoding]::Default
- $txt = [System.IO.File]::ReadAllText($srcfile, $encoding)
- $lines = $txt -split '(\r\n){2,}'
- while ($true) {
- try {
- $search = Read-Host -Prompt '请输入搜索内容(直接回车退出脚本)'
- if ($search -eq [string]::Empty) { break }
- $result = $lines | ForEach-Object {
- if ($_.Contains($search)) {
- ''
- $_
- }
- }
- if ($null -eq $result) {
- Write-Host '未找到内容' -ForegroundColor Red
- } else {
- [System.IO.File]::WriteAllLines($search, $result, $encoding)
- }
- } catch {
- $_ | Write-Host -ForegroundColor Red
- }
- }
复制代码 回复 5# 长风踏雪 |
|