重复删除脚本不执行任何操作

重复删除脚本不执行任何操作

我的同事为我写了一个脚本,理论上它可以删除同名文件,无论其文件类型如何。具体来说,它会检查 mp3、m4a、wma、wav 文件。但是,我试过了,它并没有删除任何文件。是脚本有问题,还是我的电脑设置有问题?

@Echo Off
Rem CMD file to look for duplicate media files and delete them.
Rem 1. x.y & x (1).y etc.
Rem 2. x.y and x.z
Rem
Rem Calling: %0 <Directory>
Rem
Rem Defaults to current directory if <directory> is not specified.

Rem Save environment and enable local extensions
SetLocal
SetLocal EnableExtensions EnableDelayedExpansion

Rem Set media extensions to check in priority sequence i.e. later extension will be deleted if earlier extension exists
Set mediaExts=.mp3,.m4a,.wma,.wav

Rem Get media directory
Set mediaDir=%1
If .%1 == . Set mediaDir="%CD%"

Rem Loop through media files
For /R %mediaDir% %%f In (*.*) Do Call :checkFile %%f
Pause
Goto :eof

:checkFile
Rem If file has been deleted in a previous loop then skip
If Not Exist %1 Goto :eof

Rem Check for name (*).ext etc. and delete if found
For %%d In ("%~dpn1 (*)%~x1") Do (
    Echo Deleting "%%d" because "%1" exists.
    If Not %1 == %%d Del /f "%%d"
)

Rem Check for alternative audio files
Set found=
For %%x In (%mediaExts%) Do (
    If Exist "%~dpn1%%x" (
        If .!found!==. (
            Set found=%~dpn1%%x
        ) Else (
            Echo Deleting "%~dpn1%%x" because "!found!" exists.
            Del /f "%~dpn1%%x"
        )
    )
)

Goto :eof

答案1

我不太熟悉 Dos,无法帮助你,但如果你的任务不需要自动化,你可能会发现重复清除器满足您的需求。

答案2

我想不妨以此作为答案。

  1. 脚本定义了 mediaExts 变量,但从未使用过。它不仅检查媒体文件,还检查所有文件。
  2. 在 CheckFile 例程中,if not exist...如果有空格,则检查将失败。应将其括在引号中。
  3. CheckFile 中的 for 循环永远找不到要删除的内容,因为它会检查传递的文件名 %1 是否具有相同的名称和扩展名。重复检查?不太确定。

换句话说,脚本没有删除任何内容,因为它从未找到任何要删除的内容。为什么要重新发明轮子?为什么不使用这样的方法呢?http://yadfr.sourceforge.net/

相关内容