求bat批处理删除:

2025-03-25 12:59:38
推荐回答(1个)
回答1:

1.备份源文件

2.此为powershell脚本,保存为ps1扩展名文件,右键单击脚本,以powershell运行

3.未对powershell进行过设置的,可以管理员身份运行CMD,复制以下命令到命令提符 以解除脚本限制

powershell -c "set-executionpolicy unrestricted"

当然也可以管理员运行POWERSHELL,输入以下命令解除脚本限制

set-executionpolicy unrestricted

4. WIN10以下系统如出错,可能需要去微软官网下载补丁升级powershell

$ScDir="F:\Test Dir";   #设置文本目录
$patern="图片|帮助";    #设置关键字,多个关键字用“|”(无引号,且是英文标点)隔开
Get-ChildItem -Path $ScDir|ForEach-Object{    #扫描文件
    $str=$null
    $content=Get-Content -Path $_.FullName;   #读取文本
    for($i=0;$i -lt $content.Count;){
        #删除空行与空行包含的三行;删除包含关键字的行
        if(($i -le $content.Count-3) -and !($content[$i]) -and ($content[$i+1]) -and !($content[$i+2])){
        $i+=3;continue;} elseif (($content[$i]) -and ($content[$i] -notmatch $patern)){
            $str+=$content[$i++]+"`r`n"}else{$i++}
     }#for
     $str|Out-File -FilePath $_.FullName|notepad $_.FullName   #保存结果并打开文件  
}