简体   繁体   English

Windows 命令删除所有文件,但指定的文件列表除外,这些文件的名称中可能包含空格

[英]Windows command to delete all files except the specified list of files which may contain spaces in their names

I have a folder contains a list of files.我有一个包含文件列表的文件夹。 I am using following command to delete all the files except the required files.我正在使用以下命令删除除所需文件之外的所有文件。 If there is a file with spaces in the name then following command is failing.如果文件名中包含空格,则以下命令将失败。 Say "File Name with space.txt" or "File 1.txt"说“带有 space.txt 的文件名”或“文件 1.txt”

for /f %F in ('dir /b /a-d ^| findstr /vile "file1 file2 file3"') do del "%F"

I tried putting the file names in "" but no success.我尝试将文件名放在“”中,但没有成功。

You have two options with the FINDSTR command to accomplish this. FINDSTR命令有两个选项可以完成此操作。

The first is to list each file individually with the /C option.第一种是使用/C选项单独列出每个文件。

for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /C:"file1" /C:"file2" /C:"file3"') do del "%F"

The other option is put all your search strings in a file, one on each line and use the /G option.另一个选项是将所有搜索字符串放在一个文件中,每行一个并使用 /G 选项。

for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /G:"search.txt"') do del "%F"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM