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-24 07:04
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Question] About the problem of finding and deleting empty folders with for and findstr View 7,581 Replies 22
Original Poster Posted 2006-12-26 00:05 ·  中国 广东 肇庆 四会市 电信
中级用户
★★
Credits 384
Posts 189
Joined 2005-10-19 13:12
20-year member
UID 43709
Gender Male
Status Offline
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.

::***/// Set the target directory \\\***
set keyword1=m:\test\
::***/// Set the folder names that need to be deleted (because some folders don't want to be deleted) \\\***
set keyword2=2006-12-

::***/// Create temporary files and directories \\\***
cd.>nuldir.txt
cd.>nuldir2.txt
md 目录回收
::***/// List all directory names in the current directory and output to nuldir.txt \\\***
for /f "delims=" %%i in ('dir /ad /b /s') do (dir /a /b "%%i"|findstr .>nul||echo %%i>>nuldir.txt)
::***/// Exclude directories with specified conditions in nuldir.txt and write to nuldir2.txt \\\***
for /f "delims=%keyword1% tokens=1*" %%i in (nuldir.txt) do echo %%j>>nuldir2.txt
type nuldir2.txt | findstr /i "%keyword2%">nuldir.txt
::***/// Create new directories under "Directory Recycling" and delete the original directories \\\***
for /f %%i in (nuldir2.txt) do md 目录回收\%%i && del /q %%i
::***/// Delete temporary files \\\***
del /q nuldir.txt & rd /q nuldir2.txt


[ Last edited by voiL on 2006-12-26 at 04:33 AM ]
Floor 2 Posted 2006-12-26 00:06 ·  中国 广东 肇庆 四会市 电信
中级用户
★★
Credits 384
Posts 189
Joined 2005-10-19 13:12
20-year member
UID 43709
Gender Male
Status Offline

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.


@echo off
color 17
mode con lines=25 cols=80
set targetdir1=n:\Government Websites
set targetdir2=n:\Township Websites

:input
cls & echo.
echo =======================This program can only run normally under Windows XP=======================
echo.
echo ┏━━━━━━Please select the directory━━━━━━┓
echo ┃ ┃
echo ┃ Government Websites Township Websites ┃
echo ┃ ┃
echo ┃ Read instructions Exit program ┃
echo ┃ ┃
echo ┗━━━━━━━━━━━━━━━━━┛
echo.
echo ==============================================================================
echo.
set /p choose= Please enter the bit rate code:
for %%i in (1 2 h H x X) do if "%choose%" == "%%i" goto check
echo & cls
echo.
echo.
echo ┏━━━━━━ Attention ━━━━━━┓
echo ┃ ┃
echo ┃ Your input parameter is incorrect! Please re-enter! ┃
echo ┃ ┃
echo ┗━━━━━━━━━━━━━━━━┛
ping 1.1 -n 1 -w 2000>nul
goto input

:check
for %%i in (%*) do if "!%%i" == "!" goto nofiles
if "%choose%" == "1" goto gov
if "%choose%" == "2" goto town
if "%choose%" == "x" exit
if "%choose%" == "X" exit
if "%choose%" == "h" goto help
if "%choose%" == "H" goto help
echo & cls
echo.
echo.
echo ┏━━━━━━ Attention ━━━━━━┓
echo ┃ ┃
echo ┃ The program has an accident, exiting in 3 seconds... ┃
echo ┃ ┃
echo ┗━━━━━━━━━━━━━━━━┛
goto end

:gov
cls
if not exist %targetdir1%\nul goto nodir
dir %1\. >nul 2>nul && goto nofolder
echo.
echo.
echo ┏━━━━━━━━━━━━━━━━━━━━━━━┓
echo ┃ ┃
echo ┃Copying files to the 〖Government Websites〗 directory, please wait... ┃
echo ┃ ┃
echo ┗━━━━━━━━━━━━━━━━━━━━━━━┛
copy /y %1 %targetdir1% 2>nul 1>nul
if %2! == ! goto nextgov
Shift
goto gov

:nextgov
cls
echo.
echo.
echo ┏━━━━━━━━━━━━━━━━━━━━━━━━━┓
echo ┃ ┃
echo ┃The file has been successfully copied to the 〖Government Websites〗 directory, exiting in 3 seconds...┃
echo ┃ ┃
echo ┗━━━━━━━━━━━━━━━━━━━━━━━━━┛
goto end

:town
cls
if not exist %targetdir1%\nul goto nodir
dir %1\. >nul 2>nul && goto nofolder
echo.
echo.
echo ┏━━━━━━━━━━━━━━━━━━━━━━━┓
echo ┃ ┃
echo ┃Copying files to the 〖Township Websites〗 directory, please wait... ┃
echo ┃ ┃
echo ┗━━━━━━━━━━━━━━━━━━━━━━━┛
copy /y %1 %targetdir2% 2>nul 1>nul
if %2! == ! goto nexttown
Shift
goto town

:nexttown
cls
echo.
echo.
echo ┏━━━━━━━━━━━━━━━━━━━━━━━━━┓
echo ┃ ┃
echo ┃The file has been successfully copied to the 〖Township Websites〗 directory, exiting in 3 seconds...┃
echo ┃ ┃
echo ┗━━━━━━━━━━━━━━━━━━━━━━━━━┛
goto end

.................................

:end
ping 1.1 -n 1 -w 3000 >nul 2>nul
exit


[ Last edited by voiL on 2006-12-26 at 11:50 PM ]
Floor 3 Posted 2006-12-26 00:33 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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.

@echo off
:: Please create a test directory to test, otherwise it will have no effect; do not test in the working directory, otherwise you will regret it.
for /f "delims=" %%i in ('dir /ad /b /s') do rd "%%i" 2>nul


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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
voiL +4 2006-12-26 22:18
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 4 Posted 2006-12-26 00:37 ·  中国 河南 郑州 电信
中级用户
★★
Credits 439
Posts 170
Joined 2006-01-09 20:29
20-year member
UID 48707
Status Offline
Give an example, what special characters, try adding "" to the parameters
bat c c++
Floor 5 Posted 2006-12-26 01:42 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 6 Posted 2006-12-26 04:16 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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:

@echo off
:: Idea: List all directory (including subdirectory) paths, then delete the folder at the last section of the first path;
:: If this folder can be deleted, delete the upper-level folder; if it can still be deleted, continue the previous step...
:: Until the current directory; if any section cannot be deleted, immediately jump out and start deleting the folder at the last section in the next path... and so on.

for /f "delims=" %%i in ('dir /ad /b /s') do call :rd_dir_empty "%%i"
goto :eof

rem ======= Delete empty directory =========
:rd_dir_empty
rd %1 2>nul||goto :eof
set dir_route=%1
for /f "delims=" %%i in (%dir_route%) do (
set dir_route="%%~dpi"
for /f "delims=" %%j in ('dir /ad /b "%%~dpi"')do rd "%%~dpi%%j" 2>nul||goto :eof
)
:: Remove the \ at the end of the path, so that set route_deepest="%%~dpi" can get the upper-level path
if "%dir_route:~-2,1%"=="\" set dir_route="%dir_route:~1,-2%"
if /i not "%cd%"==%dir_route% call :rd_dir_empty %dir_route%
goto :eof


[ Last edited by namejm on 2006-12-26 at 01:23 PM ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
ccwan +15 2006-12-26 04:17
voiL +4 2006-12-26 22:18
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 7 Posted 2006-12-26 04:18 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Hehe, waiting for him is exactly what I'm doing.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 8 Posted 2006-12-26 05:52 ·  中国 广东 肇庆 四会市 电信
中级用户
★★
Credits 384
Posts 189
Joined 2005-10-19 13:12
20-year member
UID 43709
Gender Male
Status Offline
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
Floor 9 Posted 2006-12-26 06:22 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The code on floor 6 has been corrected. Please test.

[ Last edited by namejm on 2006-12-25 at 06:38 PM ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 10 Posted 2006-12-26 07:44 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Haha, multiple tests have been okay.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 11 Posted 2006-12-26 22:17 ·  中国 广东 肇庆 四会市 电信
中级用户
★★
Credits 384
Posts 189
Joined 2005-10-19 13:12
20-year member
UID 43709
Gender Male
Status Offline
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 ]
Floor 12 Posted 2006-12-26 23:05 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 13 Posted 2006-12-26 23:49 ·  中国 广东 肇庆 四会市 电信
中级用户
★★
Credits 384
Posts 189
Joined 2005-10-19 13:12
20-year member
UID 43709
Gender Male
Status Offline
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.

@echo off
color 17
mode con lines=25 cols=80
set targetdir1=n:\Government Websites

:gov
cls
if not exist %targetdir1%\nul goto nodir
dir %1\. >nul 2>nul && goto nofolder
echo.
echo.
echo ┏━━━━━━━━━━━━━━━━━━━━━━━┓
echo ┃ ┃
echo ┃Copying files to the 〖Government Websites〗 directory, please wait... ┃
echo ┃ ┃
echo ┗━━━━━━━━━━━━━━━━━━━━━━━┛
copy /y %1 %targetdir1% 2>nul 1>nul
if %2! == ! goto nextgov
Shift
goto gov

:nextgov
cls
echo.
echo.
echo ┏━━━━━━━━━━━━━━━━━━━━━━━━━┓
echo ┃ ┃
echo ┃Files have been successfully copied to the 〖Government Websites〗 directory, exiting in 3 seconds...┃
echo ┃ ┃
echo ┗━━━━━━━━━━━━━━━━━━━━━━━━━┛
goto end

:end
ping 1.1 -n 1 -w 3000 >nul 2>nul
exit



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 ]
Floor 14 Posted 2006-12-27 00:27 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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:

@echo off
:begin
cls
set input=
set /p input=Please enter the test file name:
set "input=%input:"=%"
echo The file name you entered is: "%input%"
pause
goto begin

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):

@echo off
for /f "delims=" %%i in ('dir /ad /b^|findstr /b /e "--"') do echo "%%i"
pause
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 15 Posted 2006-12-30 05:31 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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:

for /f "tokens=*" %a in ('dir /b /ad /s 目录路径^|sort /r') do rd "%a" /q 2>nul
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Forum Jump: