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-06-22 16:57
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to solve annoying problems with clever small DOS commands?! (DOS Expert Small Exam Question) View 5,799 Replies 36
Original Poster Posted 2004-11-06 00:00 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
You may have encountered such a situation:
You inadvertently extracted a batch of files (assuming all are files, no subdirectories) to the wrong directory (assuming C:\), and then extracted them again to the correct directory (D:\PROGRAM), you need to delete those previous files, but there are too many, and it's tedious to delete them one by one..
So how to solve it with a single line of DOS batch processing?

Also, if you increase the difficulty, like this:
Suppose you forgot where you originally extracted the files to (the wrong directory), (only knowing it's on the C drive), then how to write it?
(Here, it is especially necessary to make sure that there are no important files with the same file name elsewhere, or for safety, narrow the directory range to a useless folder for testing... For example, create a directory AB in C:\, then create a CD directory in AB, and then copy the files from D:\PROGRAM into it, and take C:\AB\CD as the test range...)
Also, what if the extracted files also have subdirectories? (One line is definitely not enough)

Floor 2 Posted 2004-11-06 00:00 ·  中国 广西 桂林 电信
初级用户
Credits 168
Posts 27
Joined 2004-11-03 00:00
21-year member
UID 33247
Gender Male
Status Offline
win2000 and above systems
for /r %a in (*.*) do @echo %a >>err.txt
for /f %b in (err.txt) do @del %b
Floor 3 Posted 2004-11-07 00:00 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
Dude! If you do that, you'll delete all files! Isn't that equivalent to @del *.* /s? (Delete all files in the current directory) I'm really dizzy!!..8u73yr7398231u
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
lxhopp +1 2007-01-09 01:25
Floor 4 Posted 2004-11-07 00:00 ·  中国 北京 中移铁通
中级用户
★★
Credits 378
Posts 88
Joined 2004-05-05 00:00
22-year member
UID 23815
Gender Male
Status Offline
@echo offset /p drive=for /f %%i in ('dir *.* /b' do del %drive%:\%%i /q /s
www.51ct.net
Floor 5 Posted 2004-11-07 00:00 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
set /p drive= It seems to set DRIVE as the current directory. This is not very useful. Can't you just change the current directory to the directory you want?
Floor 6 Posted 2004-11-07 00:00 ·  中国 北京 中移铁通
中级用户
★★
Credits 378
Posts 88
Joined 2004-05-05 00:00
22-year member
UID 23815
Gender Male
Status Offline
The value of drive needs you to enter. For example, if you mistakenly extracted to a certain directory on drive F, then enter F and press Enter. The following method may be better:
@echo off
set /p filename=
set /p drive=
for /f "usebackq" %%i in ('dir %drive%:\%filename% /b /s' do echo rd /s /q %%i>clean.cmd
change /i clean.cmd /from "\%filename%" /to null
call clean.cmd
del clean.cmd
Enter the filename (including the suffix) of one of the extracted files and the partition of the wrong extraction directory in turn. For example, enter hehe.txt and press Enter, then enter F and press Enter. Then the batch will search for hehe.txt on drive F. If hehe.txt is in F:\haha\hehe, then the command rd /s /q "F:\haha\hehe" will be generated in clean.cmd
www.51ct.net
Floor 7 Posted 2004-11-07 00:00 ·  中国 北京 中移铁通
中级用户
★★
Credits 378
Posts 88
Joined 2004-05-05 00:00
22-year member
UID 23815
Gender Male
Status Offline
Note: The Windows 98 system cannot use set /p to manually input variable values.
chang.exe is a third-party program.
Also, there must be a directory generated due to decompression. If only files are released during decompression and no new directory is generated, do not use the batch processing in floor 6.
www.51ct.net
Floor 8 Posted 2004-11-07 00:00 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
The requirement of the question is: 1. It is not allowed to manually enter the file name (there are too many files, it's too troublesome! The annoying thing is that it needs to be done with batch processing! ) 2. Just one line of statement. Also, it seems that there is no CHANGE command in DOS, right..
Floor 9 Posted 2004-11-07 00:00 ·  中国 北京 中移铁通
中级用户
★★
Credits 378
Posts 88
Joined 2004-05-05 00:00
22-year member
UID 23815
Gender Male
Status Offline
Just input one of the file names, because it's only to find the wrong decompression directory through this file. Then delete this directory. chang.exe is in the forum.
www.51ct.net
Floor 10 Posted 2004-11-07 00:00 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
Not up to standard.. Hehe..

Floor 11 Posted 2004-11-08 00:00 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
If DOS could meet your such requirements, DOS wouldn't be DOS any more. I think it should be God or God!
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 12 Posted 2004-11-08 00:00 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
CLIMBING, it's okay, once a word is spoken, it's hard to take it back. You have to be responsible for your words. DOS isn't as bad as you said.. Let me say the answer: (Switch the directory to D:\PROGRAM), this is exactly the situation I encountered myself. D:\PROGRAM>for %1 in (*) do del c:\%1 If you don't know where it was extracted to on the C drive, you can add /s, like this: D:\PROGRAM>for %1 in (*) do del c:\%1 /s If the extracted files have both files and subdirectories, think harder..
Floor 13 Posted 2004-11-09 00:00 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
??? No one has solved it yet? (It seems this little question is not easy..) Then I'll say the answer..(I spent an afternoon researching to solve it!) If there are subdirectories, it's even more difficult. In 2000XP, it can be solved as follows: Switch to C, C:\for /r /d %1 in (*) do if %~n1==program rd %1 /s /q Haha! It's that simple! Okay, let's add a little more difficulty on this basis. But after analyzing the directory name and its path in the above problem, everything is easy.. Suppose you have many duplicate directories (the files have been solved in the above post) in drives C and D, but you don't know where they are. How to use a single batch command to delete the duplicate directories in drive D (assuming all attributes are deletable) Requirement: Use only one sentence
Floor 14 Posted 2004-11-10 00:00 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
The landlord has the suspicion of self - promotion. You didn't make the problem clear in the first place, how can others solve it? And there are many loopholes in your solution. It's possible that your system can't boot after deletion, which is really frustrating! Unless you can guarantee that there is no important thing in your C drive (usually this situation doesn't exist, right?) 1. You must ensure that the files in your compressed file are unique in the C drive. 2. You must ensure that the first - level directories in your compressed file are also unique in the C drive. Once either of the above two conditions is not met, your command will delete something that shouldn't be deleted. This is not what a rigorous user should do. So your command can only run in a specific environment that meets the above conditions, but you didn't make these conditions clear. So DOS is still DOS, not a god. Therefore, there is no need to show off like this! Moreover, the for commands you used are all for commands under 2000/xp/2003. The for command in the pure DOS environment (or win98 environment) doesn't support these parameters. You didn't make this clear. There is not even the most basic rigor, and users who want you to solve the problem should be careful. Regarding your second question, if the above factors are excluded, in the 2000/xp/2003 environment, you can use the following command: for /d /r c: %i in (*) do for /d /r d: %j in (*) do if %~nxi==%~nxj rd /s /q %j This command will delete all directories in the D: drive that have the same name as directories in the C: drive.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 15 Posted 2004-11-10 00:00 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
I spent some time solving some problems and felt quite happy, so I thought to share these methods with everyone. Let's not reveal the answers immediately, let everyone use their brains. If someone figures it out, they can say the answer; if not, I'll reveal the answer later. The main thing is to help everyone improve together. How can this be called "selling one's own goods boastfully" or "showing off like that"?

My original first question could be implemented in 98 DOS, and the situation was described fairly clearly, but I saw you say "If DOS could meet your requirements, DOS wouldn't be DOS, it would be a god," I think you might have made a conclusion too early.

As for the subsequent questions, I forgot to state the dangerous conditions, and they can only be implemented in 2000XP. I clarify this: You must be sure there are no important duplicate directories between the two disks before experimenting, otherwise it's very dangerous... You can narrow the disk range to large deletable directories for testing...

The solution method for your last question can solve the problem, but it seems that the x is redundant. It represents the file extension, and directories don't have extensions.

Forum Jump: