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 15:23
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to solve annoying problems with clever small DOS commands?! (DOS Expert Small Exam Question) View 5,796 Replies 36
Floor 16 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
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.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 17 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 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.
Floor 18 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
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:.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 19 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
@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.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 20 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
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.
Floor 21 Posted 2004-11-11 00:00 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
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.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 22 Posted 2004-11-11 00:00 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
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.
Floor 23 Posted 2004-11-11 00:00 ·  中国 江苏 南京 秦淮区 电信
中级用户
★★
Credits 307
Posts 58
Joined 2004-10-21 00:00
21-year member
UID 32780
Gender Male
Status Offline
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.
Floor 24 Posted 2004-11-11 00:00 ·  中国 广东 清远 电信
中级用户
★★
Credits 282
Posts 52
Joined 2004-09-29 00:00
21-year member
UID 31947
Gender Male
Status Offline
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.
Floor 25 Posted 2004-11-11 00:00 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
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...
Floor 26 Posted 2004-11-11 00:00 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
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..
Floor 27 Posted 2004-11-12 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 following is the speech of tired_bird on November 11, 2004 at 21:00:11:

After reading this post, I have deepened my understanding of the usage and function 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 slightly different view 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". In the Linux system, the naming rules for files and directories can support colons :, while in the XP system and the bash in cygwin, the naming of files and directories does not support colons.

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.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 28 Posted 2004-11-12 00:00 ·  中国 重庆 渝中区 电信
银牌会员
★★★
Credits 2,165
Posts 730
Joined 2004-04-21 00:00
22-year member
UID 22966
Gender Male
Status Offline
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.
Floor 29 Posted 2004-11-12 00:00 ·  中国 重庆 渝中区 电信
银牌会员
★★★
Credits 2,165
Posts 730
Joined 2004-04-21 00:00
22-year member
UID 22966
Gender Male
Status Offline
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.
Floor 30 Posted 2004-11-12 00:00 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
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.

Forum Jump: