China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-06 17:11
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Ask: How to batch delete files based on the content of a TXT file? View 1,645 Replies 3
Original Poster Posted 2007-11-30 08:09 ·  中国 山东 烟台 联通
初级用户
Credits 31
Posts 15
Joined 2006-11-02 04:46
19-year member
UID 69254
Gender Male
Status Offline
How to batch delete files based on the content of a TXT file? The TXT file contains the file names to be deleted.
Thank you in advance!
Floor 2 Posted 2007-11-30 08:57 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
```batch
for /f "delims=" %a in (*.txt) do echo del /y "%a"
```
第一高手 第二高手

Floor 3 Posted 2007-11-30 09:15 ·  中国 广东 中山 电信
高级用户
★★★
Credits 940
Posts 374
Joined 2007-10-17 21:51
18-year member
UID 100011
Gender Male
From 湖南张家界
Status Offline
Just copy strings.com to the same directory. The first line of text.txt should not be an empty line, and there can be empty lines later (which will be automatically filtered).
Mainly use the read function provided by strings to read line content and the linesize function to read the number of lines in the file.
Suppose the content of test.txt is:

x.exe
y.txt
z.com

The content of the del.bat file to be deleted is:
@echo off
rem Initialize the test.txt file
strings linesize=linesize test.txt
strings files=read test.txt,1
if %files%#==# goto end
strings linesize=add %linesize%,1
set number=0

rem Traverse each file in test.txt through a loop and delete
rem Control the number of loops with the number variable
:cycle
if %number%#==%linesize%# goto next
strings number=add %number%,1
strings files=read test.txt,%number%
if %files%#==# goto cycle
del %files% >NUL
goto cycle

:next
echo The files in the test.txt list have been deleted!

:end
rem Clear environment variables
for %%a in (linesize files number) do set %%a=
Floor 4 Posted 2007-11-30 09:23 ·  中国 山东 烟台 联通
初级用户
Credits 31
Posts 15
Joined 2006-11-02 04:46
19-year member
UID 69254
Gender Male
Status Offline
Thanks, I understand.
Forum Jump: