简体   繁体   English

如何在所有子目录中搜索并删除Windows命令提示符下与文件类型不匹配的所有文件?

[英]How can I search within all subdirectories and delete all files that do not match the filetype in windows command prompt?

For example, 例如,

Documents 
- Personal
    - abc.doc
    - 123.pdf
- Stuff
    - def.doc
    - 456.pdf

I want to delete all files that are not .doc under Documents directory. 我想删除Documents目录下不是.doc的所有文件。 How can I do this with a windows batch file or in the command prompt? 如何使用Windows批处理文件或在命令提示符下执行此操作?

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "folder=x:\somewhere\documents"

    for /f "delims=" %%a in ('
        dir /b /s /a-d "%folder%" ^| findstr /i /v /e /l /c:".doc"
    ') do echo del "%%a"

This code executes a dir command (only file paths/names /b , recursive /s , without folders in the list /ad ) to retrieve the list of files under the documents folder. 此代码执行dir命令(仅文件路径/名称/b ,递归/s ,列表/ad没有文件夹)以检索document文件夹下的文件列表。 This list is filtered with findstr to only get, ignoring case /i , the lines that do not contain /v at the end of the line /e the literal /l .doc /c:".doc" 使用findstr过滤此列表,以仅获取(忽略大小写/i在行/e末尾不包含/v的行,即文本/l .doc /c:".doc"

This command is executed from a for command that will process each line of the output. 该命令从for命令执行,该命令将处理输出的每一行。 For each line, the replaceable parameter %%a will hold the path/name of the file, so in the do clause we remove the indicated file. 对于每一行,可替换参数%%a将保存文件的路径/名称,因此在do子句中,我们删除了所指示的文件。

del operations are only echoed to console. 仅将del操作回显到控制台。 If the output is correct, remove the echo command 如果输出正确,请删除echo命令

Test this on a sample set of files. 在一组样本文件上对此进行测试。

@echo off
set "folder=c:\documents"
attrib +h "%folder%\*.doc" /s
del "%folder%\*.*?" /s
attrib -h "%folder%\*.doc" /s

使用forfiles命令:

forfiles /s /c "cmd /c if @isdir NEQ TRUE if [@ext] NEQ [doc] del @path"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以运行与当前目录和所有子目录中的所有文件匹配的 Windows Powershell 命令吗? - Can I run a Windows Powershell command matching all files in the current directory and all subdirectories? 如何使用命令提示符在所有可用的Windows驱动器中搜索文件夹? - How to search for a folder in all the available windows drive using the command prompt? 如何将目录及其子目录的所有文件的修改日期更新为当前日期? (Windows 10) - How can I update modified date to current date in all files of a directory and its subdirectories? (Windows 10) 仅当Windows命令提示符中存在父目录时,如何删除文件? - How do I delete files only if the parent directory exists in the Windows command prompt? Windows命令行:如何在没有位置的情况下列出所有文件夹中的所有文件只是文件列表 - Windows Command line: how can I list all files in all folders without the location just a list of files 如何删除目录和子目录中除某些文件夹之外的所有文件? - How to delete all files in a directory and subdirectories except for certain folders? Windows打开文件对话框,其中包含所有文件+子目录 - Windows open files dialog with all files + subdirectories Windows 驱动程序内核:如何枚举所有子目录和文件? - Windows driver kernel: How enumerate all subdirectories and files? Windows批处理文件 - 连接子目录中的所有文件 - Windows batch file - Concatenate all files in subdirectories 如何在 Windows 的命令提示符下删除特定目录中的文件/子文件夹 - How to delete files/subfolders in a specific directory at the command prompt in Windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM