中国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-08 23:19
47,811 topics / 349,895 posts / today 0 new / 48,253 members
DOS批处理 & 脚本技术(批处理室) » [Question] About the problem of finding and deleting empty folders with for and findstr
Printable Version  7,785 / 22
Floor1 voiL Posted 2006-12-26 00:05
中级用户 Posts 189 Credits 384
Declaration: The codes on floors 1 and 2 run under the XP sp2 environment.

Because of work needs, I have to create many directories named with the current date every day in the working directory (each subdirectory may be created), but sometimes it is inevitable that after creating a directory, I don't put files in it. Over time, many empty directories will accumulate. It's impossible to delete them manually. So I want to use a batch script to do it, but I encountered a problem:

The program can't handle directories with special characters or spaces correctly.

Here I ask some experts in the forum who are good at for and findstr to see if there is any good way.


[ Last edited by voiL on 2006-12-26 at 04:33 AM ]
Floor2 voiL Posted 2006-12-26 00:06
中级用户 Posts 189 Credits 384

The following script is also my daily work batch.

For the convenience of work, I made it so that files can be dragged in and then the target directory can be selected.

But I also encountered a problem that if the file name has special characters, it cannot be processed correctly.

Here I also ask the experts, is there any good solution?

I read some articles on the forum about filtering anti-space characters and special characters, but it didn't work.

After all, I started late, I don't understand programming, I only know how to play some simple batch processing usually.

Now I regret it too late.



[ Last edited by voiL on 2006-12-26 at 11:50 PM ]
Floor3 namejm Posted 2006-12-26 00:33
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Only consider deleting all empty folders in the current directory and all its subdirectories. You can consider first dir all the folders, and then use the characteristic that rd cannot delete non-empty directories to delete empty folders. For example, the following demo code:

———————————————————————————————————————
There are considerations that are not well thought out in this plan, please see the description in floor 5.

As for only deleting empty folders named by date in the current directory and its subdirectories, we will discuss it later. I'm going to eat first~~

[ Last edited by namejm on 2006-12-25 at 06:43 PM ]
Floor4 a9319751 Posted 2006-12-26 00:37
中级用户 Posts 170 Credits 439
Give an example, what special characters, try adding "" to the parameters
Floor5 namejm Posted 2006-12-26 01:42
荣誉版主 Posts 1,737 Credits 5,226 From 成都
There are still some considerations lacking in the code on the 3rd floor, because: if there is only an empty folder in a certain deep directory, after deleting the deepest empty folder, the directory above it becomes empty again, but before that, the upper directory has already been checked and cannot be deleted, resulting in the defect that empty directories are not completely deleted. The solution is: start deleting from the deepest subdirectory. This requires sorting the directories by layer depth. Hehe, it's quite challenging. Thanks to ccwan for the reminder.
Floor6 namejm Posted 2006-12-26 04:16
荣誉版主 Posts 1,737 Credits 5,226 From 成都
The following code can delete all empty folders (including those in subdirectories) in the current directory without the omission phenomenon mentioned in floor 5, and is compatible with paths containing special characters, and the problem in floor 8 is also solved. Please create new directories for testing to avoid unnecessary losses:


[ Last edited by namejm on 2006-12-26 at 01:23 PM ]
Floor7 ccwan Posted 2006-12-26 04:18
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
Hehe, waiting for him is exactly what I'm doing.
Floor8 voiL Posted 2006-12-26 05:52
中级用户 Posts 189 Credits 384
There are still some issues with the code of moderator namejm on my side:

Directory before running:

E:\test\6
E:\test\123
E:\test\voil
E:\test\a\adf
E:\test\2008034
E:\test\a\tga\adsf\adffwqa
E:\test\s h\s wer\1122
E:\test\s h\s132\dfads
E:\test\s h\s wer\New Folder
E:\test\t\tsad


Remaining directories after running:

E:\test\s h\s wer\1122
E:\test\s h\s wer\New Folder
E:\test\t\tsad
Floor9 namejm Posted 2006-12-26 06:22
荣誉版主 Posts 1,737 Credits 5,226 From 成都
The code on floor 6 has been corrected. Please test.

[ Last edited by namejm on 2006-12-25 at 06:38 PM ]
Floor10 ccwan Posted 2006-12-26 07:44
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
Haha, multiple tests have been okay.
Floor11 voiL Posted 2006-12-26 22:17
中级用户 Posts 189 Credits 384
The code of moderator namejm is really powerful. I have tested it more than ten times, and each time the speed is very fast.

Clearing more than 10 to more than 30 empty directories is basically done in a flash...

As for deleting date folders, I'll leave it to myself to study (if the moderator has a good trick, I won't refuse:))...

Add points, add points... Besides, I can't think of a better way to repay... Hehe...

I don't know if the moderator has a good solution for the code on floor 2???

Special characters such as ()<>^& and so on...

The code about detection and moving in it, I referred to the 12th floor of brother kcdsw in the following post.

How to obtain multiple variables from the outside to support multiple files? Solved

And this post.

A brief analysis of the helpless signature code

Directly using brother kcdsw's code can indeed move some files with special symbols, but it doesn't work when I modified it to the code on floor 2...

[ Last edited by voiL on 2006-12-26 at 10:31 PM ]
Floor12 namejm Posted 2006-12-26 23:05
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Please list some names of the date folders that need to be deleted, and then explain the pattern. It shouldn't be difficult.

Is the code on the 2nd floor intended to achieve file copying? Specifically, what is the intended purpose? Please explain in words. Just looking at the code is easy to mislead.
Floor13 voiL Posted 2006-12-26 23:49
中级用户 Posts 189 Credits 384
Hehe... Thanks to the enthusiastic help of moderator namejm...

The date naming method is all unified...

YYYY-MM-DD

___________________________________________

The purpose of the second floor is also just to drag several files onto the batch processing and then select the destination directory, and then the batch processing automatically copies the files to the target directory...

This can be achieved in the code of the second floor...

The problem is just how the batch processing handles special symbols in file names after dragging several files onto the batch processing...

I have tried before that files with characters like 【()^&+】 cannot be copied correctly...

So I came to the forum to find it. I found it, but it didn't work after I modified it myself...

I don't know where the problem is...

I simplified it to the following, and it can handle some characters like (), but it goes wrong when encountering a name like 1+1.txt. But the problem is also that I don't want the simplified one, because my files need to be backed up to several directories. If I make several batch processing, each batch processing only points to one target directory, it's not very practical.



The program must have the following several checks (I don't know if it is because of the following code that causes the program to not handle special characters correctly?)

::To check if there is a source file, if not, jump to the nofiles label
for %%i in (%*) do if "!%%i" == "!" goto nofiles

::Check if the target directory exists, if not, jump to the nodir label
if not exist %targetdir1%\nul goto nodir

::Check if there are directories among the dragged files, if so, jump to the nofolder label
dir %1\. >nul 2>nul && goto nofolder



[ Last edited by voiL on 2006-12-27 at 12:02 AM ]
Floor14 namejm Posted 2006-12-27 00:27
荣誉版主 Posts 1,737 Credits 5,226 From 成都
To handle file names with special characters, the path must be enclosed in quotes; ^ is considered an escape character, which is not easy to handle. It is recommended that the file name does not contain it. In the case of using the set /p statement, when dragging a file to the CMD window, the CMD interpreter will automatically add quotes to paths with spaces. In other cases, it will not add them automatically. Therefore, after receiving input with set /p, you need to first remove possible quotes and then uniformly add quotes to ensure that file names with special characters can be processed. For example, the following demo code:

As for finding folder names in date format, you can use the regular expression of findstr. Please see the following demo code (the detection is not very strict):
Floor15 namejm Posted 2006-12-30 05:31
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Comrade 9527 is too unscrupulous. He went to the Wuyou CD Boot Forum to post a thread but didn't release his good code here. Now I'll post a more concise code on his behalf:
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023