以下为演示代码,必须放在E盘之外的分区运行:
@echo off
for /f "delims=" %%i in ('dir /a /b e:\') do (
set flag=
for /f "delims=" %%j in (test.txt) do if /i "%%i"=="%%j" set flag=1
if not defined flag echo "e:\%%i"
)
pause
test.txt必须放在E盘之外的分区,里面的内容为不带路径的文件夹名,一行一条记录,比如:
文件夹1
文件夹2
文件夹3
文件夹4
如果对以上演示代码的效果很满意,可以用下面的代码来删除E盘下指定文件夹外的其他所有文件或文件夹:
@echo off
for /f "delims=" %%i in ('dir /a /b e:\') do (
set flag=
for /f "delims=" %%j in (test.txt) do if /i "%%i"=="%%j" set flag=1
if not defined flag del /a /f "e:\%%i" 2>nul||rd /s "e:\%%i" 2>nul
)
pause
The following is a demonstration code, which must be run on a partition other than drive E:
@echo off
for /f "delims=" %%i in ('dir /a /b e:\') do (
set flag=
for /f "delims=" %%j in (test.txt) do if /i "%%i"=="%%j" set flag=1
if not defined flag echo "e:\%%i"
)
pause
test.txt must be placed on a partition other than drive E, and the content inside is the folder names without paths, one record per line, for example:
Folder 1
Folder 2
Folder 3
Folder 4
If you are satisfied with the effect of the above demonstration code, you can use the following code to delete all other files or folders under drive E except the specified folders:
@echo off
for /f "delims=" %%i in ('dir /a /b e:\') do (
set flag=
for /f "delims=" %%j in (test.txt) do if /i "%%i"=="%%j" set flag=1
if not defined flag del /a /f "e:\%%i" 2>nul||rd /s "e:\%%i" 2>nul
)
pause