如何删除以“ something”开头的子文件夹中的文件

时间:2019-02-15 10:11:50

标签: batch-file

我想删除许多以'an'开头的子文件夹中的特定文件(* .jpg),但我不想从其他子文件夹中删除jpeg文件。

我尝试了命令:

for /f "delims=" %%A in ('dir "your_path\an*" /b /ad') do echo (cd %%A & echo del /F /Q /S "*.jpg")

但是此命令删除所有子文件夹中的所有jpg文件。

目录不是很困难:

C:
  Users
    user_name
        My pictures
            goo [Main folder]
                inn_01 (sub folder)
                    a.jpg (to remove)
                inn_02 (sub folder)
                    a.jpg (to remove)
                ...
                001 (sub folder)
                    a.jpg (to keep)
                002 (sub folder)
                    a.jpg (to keep)
                ...

2 个答案:

答案 0 :(得分:0)

/S命令中删除del

<Prompt>del /?
Deletes one or more files.

DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names
ERASE [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

  names         Specifies a list of one or more files or directories.
                Wildcards may be used to delete multiple files. If a
                directory is specified, all files within the directory
                will be deleted.

  /S            Delete specified files from all subdirectories.

评论后编辑

由于这似乎行不通,ForFiles呢?像这样:

forfiles /s /C "cmd /c echo @path\@file"

(您可以在IF部分添加"cmd /c条款)

答案 1 :(得分:0)

请尝试一下,非常简单。准备好实际删除后,请删除echo

@echo off
for /f "delims=" %%i in ('dir "your_path\inn*" /b /ad') do (
        echo del /F /Q "%%~fi\*.jpg"
)

注意,请不要在del命令中添加/S