![]() |
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-09-29 18:59 |
47,813 topics / 349,918 posts / today 0 new / 48,278 members |
| DOS批处理 & 脚本技术(批处理室) » How to solve annoying problems with clever small DOS commands?! (DOS Expert Small Exam Question) |
| Printable Version 6,749 / 36 |
| Floor16 Climbing | Posted 2004-11-10 00:00 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
You try the command "md a.b", and you'll know that x is not redundant. This is also an embodiment of the program's strictness. I'm not arguing with you, just reminding you to pay attention to strictness. Those in technology must pay attention to this point.
|
|
| Floor17 GOTOmsdos | Posted 2004-11-10 00:00 |
| 铂金会员 Posts 1,827 Credits 5,154 | |
|
I tried it. For directory names with dots, there's no problem. I tested directories like A.B.F, and they all worked fine. This shows that X indeed controls the file extension, not directory names with dots. Because our batch processing's /d has already determined it's a directory.. Then X doesn't work.
|
|
| Floor18 Climbing | Posted 2004-11-10 00:00 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
In my experiment, it won't work without x. For example, in directory a on drive C:; if you don't have x, it will delete directories like a.1, a.2, etc., on drive D:.
|
|
| Floor19 Climbing | Posted 2004-11-10 00:00 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
@echo off
:: testfor.bat Test for command and command extensions :: Function: Delete all subdirectories in directory 2 that have the same name as those in directory 1 rd /s /q 1 rd /s /q 2 md 1\a md 1\a.1 md 1\b md 1\c md 2\a md 2\a.1 md 2\a.2 md 2\b md 2\c md 2\d md 2\e echo ================================================================ echo Contents of directory 1: echo ================================================================ dir /b 1 echo ================================================================ echo Contents of directory 2: echo ================================================================ dir /b 2 echo ================================================================ echo After running the first for command (if %%~ni==%%~nj), the contents of directory 2 become: for /d /r 1 %%i in (*) do for /d /r 2 %%j in (*) do if %%~ni==%%~nj rd /s /q %%j echo ================================================================ dir /b 2 echo ================================================================ echo Note: The a.2 directory was mistakenly deleted. pause echo. echo ================================================================ echo Preparing to run the second for command... echo ================================================================ rd /s /q 1 rd /s /q 2 md 1\a md 1\a.1 md 1\b md 1\c md 2\a md 2\a.1 md 2\a.2 md 2\b md 2\c md 2\d md 2\e echo Contents of directory 1: echo ================================================================ dir /b 1 echo ================================================================ echo Contents of directory 2: echo ================================================================ dir /b 2 echo ================================================================ echo After running the second for command (if %%~nxi==%%~nxj), the contents of directory 2 become: for /d /r 1 %%i in (*) do for /d /r 2 %%j in (*) do if %%~nxi==%%~nxj rd /s /q %%j echo ================================================================ dir /b 2 echo ================================================================ echo Look, the a.2 directory was not deleted. echo. echo ================================================================ echo Test result: The directory name also has an extension. |
|
| Floor20 GOTOmsdos | Posted 2004-11-10 00:00 |
| 铂金会员 Posts 1,827 Credits 5,154 | |
|
Oh, I didn't understand your meaning just now. I tried again, and it's indeed as you said. Now I find that there's a problem with the explanation (or program) of the FOR command. It says it handles extensions, but actually it handles all that have dots (including directories). I also found that this X parses the ones with dots, only taking the last dot as the boundary. If there's a dot and you don't add X, it will analyze all before the last dot; if you add X, it will analyze all. So, if there are multiple dots and you don't add X, for example, A.B.C, it won't think it's the same as A. In short, this is a defect. The complete handling is: directories are all included with dots, directories should be treated as a whole, and only when analyzing files are extensions separated.
|
|
| Floor21 Climbing | Posted 2004-11-11 00:00 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
Whether it's Windows or Linux systems, the naming rules for files and directories are the same, and the handling methods are also identical. So this is not a defect of the system, but simply that your program hasn't been considered carefully enough.
|
|
| Floor22 qzwqzw | Posted 2004-11-11 00:00 |
| 银牌会员 Posts 636 Credits 2,343 | |
|
Oh, Hit!!! I crashed three times in order to reply to this question. Why is it so hard to be a master? I don't know much about DOS. I just remember learning a sentence that seems related to this: for %%f in (d:\program\*.*) do if exist c:\%%f del c:\%%f But it doesn't seem to work here. Is my head too stupid or is the owner's question wrong? I always think, why not just use dir to see where it is, and then use deltree or rd /s to delete? You can gradually confirm if you are afraid of deleting by mistake. Why must you use such a long for command and such a complicated batch processing? Do you, the owner, install the software in the wrong place over and over again? Using a simple one when there is a simple one available and using a complicated one is a sin. So, I have been using "Find" to find it, then select delete, which is convenient and fast, with no aftereffects.
|
|
| Floor23 tired_bird | Posted 2004-11-11 00:00 |
| 中级用户 Posts 58 Credits 307 | |
|
After reading this post, I have deepened my understanding of the usage and functions of the for statement. At the same time, I have learned a lot from Climbing and GOTOmsdos and benefited a lot. However, I have a different opinion on Climbing's statement that "whether it is the Windows or Linux system, the naming rules for files and directories are the same, and the processing methods are also the same". The naming rules for files and directories under Linux can support colons :, while the naming of files and directories under XP and in the bash of cygwin does not support colons.
|
|
| Floor24 insert | Posted 2004-11-11 00:00 |
| 中级用户 Posts 52 Credits 282 | |
|
Agree with the statement of building 22. It's not that every time you will misplace it. Why make it so complicated? It has no practical use.
|
|
| Floor25 GOTOmsdos | Posted 2004-11-11 00:00 |
| 铂金会员 Posts 1,827 Credits 5,154 | |
|
to qzwqzwYou didn't see my requirement clearly. One of my requirements is: Just because there are too many files, it's too troublesome to look at and find them one by one! So using DIR is definitely not okay! for %%f in (d:\program\*.*) do if exist c:\%%f del c:\%%fYour above method can be streamlined into the answer in my post. Let's look at the following content for the subsequent requirements...
|
|
| Floor26 GOTOmsdos | Posted 2004-11-11 00:00 |
| 铂金会员 Posts 1,827 Credits 5,154 | |
|
OF course, not every DOS technique is very common, but for those who study and research DOS, this is one of the tasks of delving in. But once you or your friends encounter a similar situation, the power of DOS skills will be demonstrated..
|
|
| Floor27 Climbing | Posted 2004-11-12 00:00 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
The following is the speech of tired_bird on November 11, 2004 at 21:00:11: I'm not saying that the file naming rules of Windows and Linux are the same (of course there are many restrictions under Windows), but within the same system, the naming rules of file and directory names are the same. |
|
| Floor28 bush | Posted 2004-11-12 00:00 |
| 银牌会员 Posts 730 Credits 2,165 | |
|
The LZ said: D:\PROGRAM>for %1 in (*) do del c:\%1 /s is not good. You must ensure that the first-level directory in your compressed file is unique on the C drive. The first-level directory in the compressed file must be unique on the C drive and there are no other files with the same name. It's still better to locate it first.
|
|
| Floor29 bush | Posted 2004-11-12 00:00 |
| 银牌会员 Posts 730 Credits 2,165 | |
|
Actually, sorting by time with `dir /d` and then deleting files created after decompression is okay. Although it's one more step, it's more reliable.
|
|
| Floor30 GOTOmsdos | Posted 2004-11-12 00:00 |
| 铂金会员 Posts 1,827 Credits 5,154 | |
|
to bush
Yes, you must make sure there are no files with the same name elsewhere in drive C. It has nothing to do with the root directory. This sentence only addresses the file. These exam questions of mine are actually just to achieve an intention theoretically. If you really want to operate, you must definitely add statements to control other conditions. |
|
| Prev 1 2 3 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |