首先,用dir /ad /b 将目录名提取出来,然后利用findstr /v把不删除的目录过滤,最后用for对列表进行遍历操作。
@echo off
for /f "tokens=*" %%i in {'"dir d: /ad /b | findstr /i /v "Lgames Ngames 游戏菜单""') do rd /s /q "%%i"
注意:
1.在findstr的查找字符串中若包含中文字符,需要加上/i参数。
2.考虑到文件夹名中可能包含空格,所以for /f要指定tokens=*,以及rd命令调用%%i的时候要加上引号。
3.在将某命令的输出作为for的循环条件时,最好在括号内的单引号里面再加一对双引号,可以避免显视地进行字符转义以及可能出现的问题。
First, extract the directory names using dir /ad /b, then use findstr /v to filter out the directories that are not to be deleted, and finally use for to traverse the list.
@echo off
for /f "tokens=*" %%i in {'"dir d: /ad /b | findstr /i /v "Lgames Ngames 游戏菜单""') do rd /s /q "%%i"
Notes:
1. If there are Chinese characters in the findstr search string, the /i parameter needs to be added.
2. Considering that there may be spaces in the folder name, for /f should specify tokens=*, and when the rd command calls %%i, quotes should be added.
3. When using the output of a certain command as the loop condition of for, it is best to add another pair of double quotes inside the single quotes in the parentheses, which can avoid explicit character escaping and possible problems.