【问题标题】:How to delete html file whose first name contains particular word with batch script?如何使用批处理脚本删除名字包含特定单词的html文件?
【发布时间】:2015-12-15 08:46:12
【问题描述】:

我有一些 html 文件:

pkg_mon_spt@id=6&page=1.html
pkg_mon_spt@id=7&page=1.html
pkg_mon_spt@id=8&page=1.html
pkg_mon_spt@id=6&page=2.html
pkg_mon_spt@id=7&page=2.html

我的批处理脚本要删除所有包含“page=1”字样的文件是:

@echo off
if exist *page=1.html del *page=1.html
pause

html 文件和批处理文件在同一个文件夹中。 当我运行批处理文件时,名称中包含单词“page=1”的 html 文件没有被删除。

怎么了?谢谢你的解释。

【问题讨论】:

  • 为什么是IF EXIST?简单批处理,只有这个命令,没有必要包含它。我的意思是DEL本身已经检查了文件是否存在,它不会删除不存在的东西。

标签: batch-file cmd


【解决方案1】:

当你的批处理文件在同一个文件夹中时,这个命令就足够了。

del *page=1.html

【讨论】:

    【解决方案2】:

    在文件名周围使用引号(它包含特殊字符 (=,&)):

    if exist "*page=1.html" del "*page=1.html"
    

    【讨论】:

    • 这并没有什么区别,因为& 不会在路径中逐字出现...
    • @aschimpfl:更多的是=del *page=2.html - *page konnte nicht gefunden werden。与if exist 相同的问题/解决方案。
    • 我同意,= 是命令行解释器的分隔符,就像 SPACE 一样;
    • +1 双引号 all 文件名似乎是最佳做法。见delimiters explanation
    【解决方案3】:

    我认为问题在于您使用的模式*page=1,除了缺少引号。您可以使用find 命令解决此问题:

    for /F "delims=| eol=|" %%L in (
      'dir /B /A:-D "\path\to\folder" | find "page=1"'
    ) do (
      ECHO del "%%~fL"
    )
    

    要实际删除文件,请删除 ECHO

    【讨论】:

      【解决方案4】:

      我终于找到了解决办法。

      if exist *"page=1.html" del *"page=1.html"
      

      感谢您的帮助和回复。

      【讨论】:

        猜你喜欢
        • 2021-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多