用于从特定命名文件夹中删除AVI文件的批处理文件

时间:2022-01-03 02:22:54

am looking to tidy up a full archive drive by deleting unwanted video (AVI) files.

我希望通过删除不需要的视频(AVI)文件来整理完整的存档驱动器。

What I need to do is delete AVI files from within folders and sub-folders of a main directory. However, I don't want to delete AVI files from within any folder whose folder name contains the text "PG" - typically of the form "G1244A PG Report" but the identifying code (in this case "G1244A") may vary in length. Thanks.

我需要做的是从主目录的文件夹和子文件夹中删除AVI文件。但是,我不想从文件夹名称包含文本“PG”的任何文件夹中删除AVI文件 - 通常是“G1244A PG报告”形式,但识别代码(在本例中为“G1244A”)的长度可能不同。谢谢。

Added after @tftd comment: Here's my effort so far (decided to move the vids, not delete them). Seems to work OK but not very elegant:

在@tftd评论之后添加:这是我到目前为止的努力(决定移动视频,而不是删除它们)。似乎工作正常但不是很优雅:

FOR /d /r %%d IN (*) DO (
ECHO "%%~nxd"|FIND " PG " >NUL
IF ERRORLEVEL 1 (ECHO Non-PG folder...move  vids in "%%d"
       Set NONPGFOLDER=%%d
       ECHO !NONPGFOLDER!
       DIR  "!NONPGFOLDER!\*.avi"
       MOVE "!NONPGFOLDER!\*.avi" "C:\AVIfiles\"
)ELSE (ECHO     PG folder...do nothing in "%%d"
)
)

PAUSE
Exit

1 个解决方案

#1


3  

Stack Overflow is not a free code writing service and with spending some minutes on searching, you would have found most likely the code by yourself required for this simple task asked very often.

Stack Overflow不是一个免费的代码编写服务,并且花费几分钟时间进行搜索,你很可能会发现这个简单的任务经常需要你自己需要的代码。

@echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%F in ('dir /A-D /B /S "F:\*.avi" 2^>nul') do (
    set "FilePath=%%~dpF"
    if "!FilePath:PG=!" == "!FilePath!" echo del /F "%%F"
)
endlocal
pause

The command DIR with the options /A-D /B /S returns a list of file names of AVI files found on drive F: with full path and without double quotes even for full file names with 1 or more spaces included.

带有选项/ A-D / B / S的命令DIR返回在驱动器F上找到的AVI文件的文件名列表:具有完整路径且没有双引号,即使包含1个或多个空格的完整文件名也是如此。

The path of each file is assigned to an environment variable.

每个文件的路径都分配给一个环境变量。

If this file path with all occurrences of PG case insensitive removed is equal the unmodified file path, no folder in file path contains PG and therefore this file can be deleted.

如果删除所有PG不区分大小写的文件路径等于未修改的文件路径,则文件路径中的文件夹不包含PG,因此可以删除此文件。

There is command echo before command del in above code to make it possible for you to verify the deletion list before using the batch file without echo and without pause.

在上面的代码中有命令del之前的命令echo,使您可以在使用批处理文件之前验证删除列表,而无需回显和没有暂停。

An alternate solution as suggested by Joey:

Joey建议的替代解决方案:

@echo off
setlocal EnableDelayedExpansion
for /R "F:\" %%F in (*.avi) do (
    set "FilePath=%%~dpF"
    if "!FilePath:PG=!" == "!FilePath!" echo del "%%F"
)
endlocal

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

要了解使用的命令及其工作方式,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • set /?
  • setlocal /?

#1


3  

Stack Overflow is not a free code writing service and with spending some minutes on searching, you would have found most likely the code by yourself required for this simple task asked very often.

Stack Overflow不是一个免费的代码编写服务,并且花费几分钟时间进行搜索,你很可能会发现这个简单的任务经常需要你自己需要的代码。

@echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%F in ('dir /A-D /B /S "F:\*.avi" 2^>nul') do (
    set "FilePath=%%~dpF"
    if "!FilePath:PG=!" == "!FilePath!" echo del /F "%%F"
)
endlocal
pause

The command DIR with the options /A-D /B /S returns a list of file names of AVI files found on drive F: with full path and without double quotes even for full file names with 1 or more spaces included.

带有选项/ A-D / B / S的命令DIR返回在驱动器F上找到的AVI文件的文件名列表:具有完整路径且没有双引号,即使包含1个或多个空格的完整文件名也是如此。

The path of each file is assigned to an environment variable.

每个文件的路径都分配给一个环境变量。

If this file path with all occurrences of PG case insensitive removed is equal the unmodified file path, no folder in file path contains PG and therefore this file can be deleted.

如果删除所有PG不区分大小写的文件路径等于未修改的文件路径,则文件路径中的文件夹不包含PG,因此可以删除此文件。

There is command echo before command del in above code to make it possible for you to verify the deletion list before using the batch file without echo and without pause.

在上面的代码中有命令del之前的命令echo,使您可以在使用批处理文件之前验证删除列表,而无需回显和没有暂停。

An alternate solution as suggested by Joey:

Joey建议的替代解决方案:

@echo off
setlocal EnableDelayedExpansion
for /R "F:\" %%F in (*.avi) do (
    set "FilePath=%%~dpF"
    if "!FilePath:PG=!" == "!FilePath!" echo del "%%F"
)
endlocal

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

要了解使用的命令及其工作方式,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • set /?
  • setlocal /?