AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1669933
Accepted
TheCodeExpert
TheCodeExpert
Asked: 2021-08-18 08:55:26 +0800 CST2021-08-18 08:55:26 +0800 CST 2021-08-18 08:55:26 +0800 CST

通过命令提示符查看 .bat 文件的内容

  • 772

我想扫描整个桌面以查找 .bat,然后查看 .bat 文件的内容,如果该文件包含项目的ipconfig,则删除该文件。有什么办法可以做到吗?我不想使用第三方工具

windows command-line
  • 4 4 个回答
  • 1093 Views

4 个回答

  • Voted
  1. Best Answer
    Reddy Lutonadio
    2021-08-18T10:46:34+08:002021-08-18T10:46:34+08:00

    下面的 Powershell 脚本获取文件的内容myfile.bat,检查字符串ipconfig是否存在。如果是,则删除该文件。

    $file = 'c:\temp\myfile.bat`
    $a = Get-Content $file | Select-String ipconfig
    if( $a.Length -gt 0){Remove-Item $file}
    

    要包含一个文件夹,例如您的桌面,请更改脚本,如下所示。替换mthecodeexpert为您的真实用户名。

    $folder = 'c:\users\thecodeexpert\*.bat'
    $files = Get-ChildItem $folder -file
    $files | ForEach-Object{ $a = Select-String -Path $_.Fullname ipconfig; if ($a.Length -gt 0){Remove-Item $_.Fullname}}
    
    • 2
  2. postanote
    2021-08-18T16:29:13+08:002021-08-18T16:29:13+08:00

    继续我的评论...

    # Find the files by type
    (Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Filter '*.bat' -Recurse).FullName
    # Results
    <#
    C:\Users\WDAGUtilityAccount\Desktop\SomeBatFile.bat
    #>
    
    # Test planned action on the file to find the string pattern
    (Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Filter '*.bat' -Recurse).FullName | 
    ForEach-Object {
        If (Select-String -Path $PSItem -Pattern 'ipconfig')
        {Remove-Item -Path $PSItem -WhatIf}
    }
    
    # Results
    <#
    What if: Performing the operation "Remove File" on target "C:\Users\WDAGUtilityAccount\Desktop\SomeBatFile.bat".
    #>
    
    
    # Take action on the file to find the string pattern
    (Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Filter '*.bat' -Recurse).FullName | 
    ForEach-Object {
        If (Select-String -Path $PSItem -Pattern 'ipconfig')
        {Remove-Item -Path $PSItem -Force -Verbose}
    }
    # Results
    <#
    VERBOSE: Performing the operation "Remove File" on target "C:\Users\WDAGUtilityAccount\Desktop\SomeBatFile.bat".
    #>
    
    # Validate removal
    (Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Filter '*.bat' -Recurse).FullName
    # Results
    <#
    No results
    #>
    
    • 1
  3. Stephan
    2021-08-18T10:21:06+08:002021-08-18T10:21:06+08:00

    要搜索所有文件,请使用for循环:

    @echo off
    cd /d "%userprofile%\desktop"
    for %%a in (*.bat) do (
      type "%%a"|find /i "ipconfig" && echo del "%%a"
    )
    

    还有另一种方法(看起来稍微复杂一些,但在程序员看来更优雅,因为它不必处理不包含搜索字符串的文件)。如果有很多文件(我的意思 是很多),这会快得多。不过,您不会注意到桌面文件夹中的速度优势。

    findstr /m返回的不是找到的行,而是找到此类行的文件名。围绕for /f捕获这些文件名并删除它们:

    @echo off
    for /f "delims=" %%a in ('findstr /im "ipconfig" "%userprofile%\desktop\*.bat"') do del "%%a"
    

    (注意:您的桌面可能位于不同的位置 - 检查一下)

    • 0
  4. Abraham Zinala
    2021-08-18T16:10:58+08:002021-08-18T16:10:58+08:00

    您可以Select-String在 PowerShell 中使用,它允许您搜索目录(针对特定文件类型 - 扩展名),允许您在过滤的文件中找到模式匹配:

    $Paths = Select-String -Path "$env:USERPROFILE\Desktop\*" -Include '*.bat','*.cmd' -Pattern 'ipconfig'  | Select-Object -ExpandProperty Path 
        if ($null -ne $Paths) {
            foreach ($Path in $Paths){
                "Match found in filepath: $Path"
                Remove-Item -Path $Path -WhatIf
            }
        }
        else {
            'No file found containing pattern'
        }
    
    • 默认情况下,Select-String 在每一行中查找第一个匹配项,并且对于每个匹配项,它会显示文件名、行号以及包含匹配项的行中的所有文本。
    • 它还允许您将正则表达式模式匹配用于更复杂的任务。

    将输出存储到变量中,我们可以显式选择引用该Path属性的完整路径。最后,我们可以添加一些简单的if条件来评估我们的变量,并使用foreach循环我们可以遍历捕获输出。

    • 将输出存储到变量是必要的(至少在这种情况下),因此文件不会继续使用 - 感谢dave_thompson_085指出我的错误。

    仅使用此cmdlet的缺点是,它只允许列出单个目录。幸运的是,PowerShell 可以链接命令,您只需使用Get-ChildItem带有-Recurse开关的 a 稍微修改代码以允许目录(文件夹)递归。

    $Paths = Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Include '*.cmd','*.bat' -Recurse |
        Select-String -Pattern 'ipconfig' | Select-Object -ExpandProperty Path
    
        if ($null -ne $Paths) {
            foreach ($Path in $Paths){
                "Match found in filepath: $Path"
                Remove-Item -Path $Path -WhatIf
            }
        }
        else {
            'No file found containing pattern'
        }
    

    基本上是相同的概念,所以当你准备好时,删除-WhatIf common 参数,它将允许删除包含该模式的文件。

    • 0

相关问题

  • Python 的“pass”参数的批处理等价物是什么?

  • 禁用后无法启用 Microsoft Print to PDF

  • 在 Windows 上与 Docker 守护进程通信

  • 资源管理器侧面板中的桌面外壳快捷方式

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve