在cmd中搜索和替换名称

问题描述

我真的需要你的帮助我在我的硬盘驱动器中得到了病毒 Win32/Delf.NRJ,它在文件名的开头添加字母“g”并隐藏文件并将属性更改为 system 所以我需要运行 attrib -s -r -a -h /D /S 以取消隐藏它。

如果我可以在所有驱动器和每个以“g”开头并隐藏的 exe 文件上使用 cmd 或 powershell 运行一些搜索删除属性删除“g”并取消隐藏它?

在这个驱动器中有很多 exe,它需要永远浏览所有文件(完整的 2TB)

谢谢

解决方法

您可以使用 Get-ChildItem -Attributes Hidden,System 来发现带有 System 属性集的隐藏文件:

$affectedExecutables = Get-ChildItem -Attributes Hidden,System -Filter g*.exe -Recurse

您可以使用带有 -replaceRename-Item 正则表达式运算符从名称中删除 g,如下所示:

$renamedExecutables = $affectedExecutables |Rename-Item -NewName { $_.Name -replace '^g' } -PassThru

最后,PowerShell 允许您直接修改文件属性:

$systemHiddenAttributes = [System.IO.FileAttributes]'Hidden,System'
$renamedExecutables |ForEach-Object {
  $_.Attributes = $_.Attributes -bxor $systemHiddenAttributes
}

这将从文件中删除隐藏和系统属性