中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-10 19:42
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » How to solve annoying problems with clever small DOS commands?! (DOS Expert Small Exam Question)
Printable Version  6,109 / 36
Floor1 GOTOmsdos Posted 2004-11-06 00:00
铂金会员 Posts 1,827 Credits 5,154
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)

Floor2 doslr Posted 2004-11-06 00:00
初级用户 Posts 27 Credits 168
win2000 and above systems
for /r %a in (*.*) do @echo %a >>err.txt
for /f %b in (err.txt) do @del %b
Floor3 GOTOmsdos Posted 2004-11-07 00:00
铂金会员 Posts 1,827 Credits 5,154
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
Floor4 死性不改 Posted 2004-11-07 00:00
中级用户 Posts 88 Credits 378
@echo offset /p drive=for /f %%i in ('dir *.* /b' do del %drive%:\%%i /q /s
Floor5 GOTOmsdos Posted 2004-11-07 00:00
铂金会员 Posts 1,827 Credits 5,154
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?
Floor6 死性不改 Posted 2004-11-07 00:00
中级用户 Posts 88 Credits 378
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
Floor7 死性不改 Posted 2004-11-07 00:00
中级用户 Posts 88 Credits 378
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.
Floor8 GOTOmsdos Posted 2004-11-07 00:00
铂金会员 Posts 1,827 Credits 5,154
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..
Floor9 死性不改 Posted 2004-11-07 00:00
中级用户 Posts 88 Credits 378
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.
Floor10 GOTOmsdos Posted 2004-11-07 00:00
铂金会员 Posts 1,827 Credits 5,154
Not up to standard.. Hehe..

Floor11 Climbing Posted 2004-11-08 00:00
铂金会员 Posts 2,753 Credits 6,962 From 河北保定
If DOS could meet your such requirements, DOS wouldn't be DOS any more. I think it should be God or God!
Floor12 GOTOmsdos Posted 2004-11-08 00:00
铂金会员 Posts 1,827 Credits 5,154
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..
Floor13 GOTOmsdos Posted 2004-11-09 00:00
铂金会员 Posts 1,827 Credits 5,154
??? 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
Floor14 Climbing Posted 2004-11-10 00:00
铂金会员 Posts 2,753 Credits 6,962 From 河北保定
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.
Floor15 GOTOmsdos Posted 2004-11-10 00:00
铂金会员 Posts 1,827 Credits 5,154
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.

1 2 3  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023