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-29 16:41
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Delete audio/video files under folders on drive D whose folder names are numbers (0-999999) View 5,678 Replies 28
Floor 16 Posted 2006-09-28 21:41 ·  中国 广东 广州 电信
新手上路
Credits 18
Posts 7
Joined 2006-09-26 22:39
19-year member
UID 63739
Status Offline
Then I want to back up these files before deleting them, including the directory structure and all the files inside

That is to say

For example, if deleting D:/music/123/*.rm, then back up all files in the 123 folder together with the directory structure

If it can back up the whole music folder, that would be even better


Please advise, brother
Floor 17 Posted 2006-09-28 21:48 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
For backup, just use the mouse.
Floor 18 Posted 2006-09-28 22:52 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Originally posted by tide2046 at 2006-9-28 21:41:
Then I want to back up these files before deleting them, including the directory structure and all the files inside

That is to say

For example, if deleting D:/music/123/*.rm, then back up all files in the 123 folder together with the directory structure ...


The poster above is right. Do your backup under Windows. Using cmd will be very troublesome. I worked on this all night yesterday, pretty tired, going to rest now.
Floor 19 Posted 2006-09-28 22:53 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by pengfei at 2006-9-28 07:02:
Modified on the basis of the code in post #14, completely abandoning the regular expression usage in posts #13 and #14. So the speed has been greatly improved. Another advantage is that it can match non-purely numeric directories (use with caution!). After te...

  The method of judging by cutting characters is indeed good, much more efficient than echo|findstr. Thumbs up first.

  Next you can consider the issue of paths containing spaces, hehe.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 20 Posted 2006-09-28 23:03 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Originally posted by namejm at 2006-9-28 22:53:

  The method of judging by cutting characters is indeed good, much more efficient than echo|findstr. Thumbs up first.

  Next you can consider the issue of paths containing spaces, hehe.


Hehe~ A lot of my batch-writing ideas come from algorithms in C...

The code in posts 15 and 16 can already handle folders with paths containing spaces.

[ Last edited by pengfei on 2006-9-28 at 23:06 ]
Floor 21 Posted 2006-09-29 01:49 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Part 1:


@echo off
set strOldDir=%cd%
set strRegExp=".*\\*\\*$"
rem set strRegExp=".*\\*$"
set strFileExtension=*.wmv *.rm *.rmvb *.avi *.mp3 *.wma *.wav
for /f "delims=" %%i in ('dir /ad /s /b ^| findstr /rc:%strRegExp%') do cd "%%i" && echo %%i && dir /b %strFileExtension%
rem for /f "delims=" %%i in ('dir /ad /s /b ^| findstr /rc:%strRegExp%') do cd "%%i" && del %strFileExtension%
cd %strOldDir%
pause

Notes:
1.This script is for demonstration purposes. If you want to delete audio/video files, you can move the rem before the second for statement to before the first for statement.
2.If you want to add backup directory functionality, you can directly add xcopy inside the for statement yourself.
3.The default directory format is: x:\xxx\ files (if you want to include subdirectories, you can add the /s parameter after the del/dir command in the for statement). If you move the rem before line 4 to the beginning of line 3, then the default directory format is: x:\xxx\ files
4.None of the above code has been tested.


Part 2:

Two thoughts regarding the method mentioned in the replies that doesn't use findstr:
1.On obtaining the last two directories

for ..... do (
set "strDir="%%i""
set "strDir=!strDir:\=" "!"
for %%? in (!strDir!) do strTmp=%%? !strTmp!
call :dosomething !strTmp!
)
....

:dosomething
rem In this label section you only need to judge %1 and %2.
rem %1 corresponds to the lowest-level subdirectory, %2 is the parent directory of %1.
goto :eof

2.On judging whether a directory consists entirely of numbers:

:IsNumber
set "strNum=%~1%~2"
for /l %%i in (0,1,9) do set "strNum=!strNum:%%i=!"
if "%strNum%" == "" echo All numbers!
goto :eof


Note: None of the above code has been tested.
Floor 22 Posted 2006-09-30 10:01 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Moderator 3742668 is really strong in the use of statements and parameters. The code is concise and efficient, and the approach is original. It's worth learning from very well...

As for me, I've always been a bit intimidated by the two commands find and findstr. Their execution efficiency is relatively low, especially when processing a large amount of data, where the speed is comparatively slow. I came to this conclusion through a lot of testing.

The two suggestions the moderator made about findstr are very reasonable, especially the second idea, which is very good.

Also, it seems the moderator's code only considers at most two directory levels (not sure whether I read it wrong). If there are more directory levels, it would need to be rewritten. I still think it's better to list all directories under the root directory to be processed and judge whether they are all numbers, and using a regular expression to judge whether they are all numbers will also affect speed.

Using this section from the moderator is very good, so I still insist on not using findstr:
:IsNumber
set "strNum=%~1%~2"
for /l %%i in (0,1,9) do set "strNum=!strNum:%%i=!"
if "%strNum%" == "" echo All numbers!
goto :eof

Thanks to 3742668 for the wonderful comments, learned a lot~~~!

[ Last edited by pengfei on 2006-9-30 at 10:06 ]
Floor 23 Posted 2006-09-30 11:13 ·  中国 重庆 电信
初级用户
Credits 45
Posts 17
Joined 2006-09-23 13:24
19-year member
UID 63469
Status Offline
Looking up in admiration~~ really an expert
Floor 24 Posted 2006-09-30 12:52 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Re pengfei:
1.About the low efficiency when using findstr.
It's also using findstr, but the efficiency is completely different when used inside a loop versus outside a loop. The low efficiency you mentioned is because you used it inside the loop. When the number of executions increases, of course the efficiency becomes low. If it is executed outside the loop, I can boldly say it will definitely be much faster than writing code yourself to complete the corresponding function.
2.About only being able to handle two directory levels.
A hint: after executing for %%? in (!strDir!) do strTmp=%%? !strTmp!, if the path is c:\111\222\333\444, then the content of strTmp will be: "444" "333" "222" "111" c:"
Floor 25 Posted 2006-10-01 03:44 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Originally posted by 3742668 at 2006-9-29 01:49:
@echo off
set strOldDir=%cd%
set strRegExp=".*\\*\\*$"
rem set strRegExp=".*\\*$"
set strFileExtension=*.wmv *.rm *.rmvb *.avi *.mp3 *.wma *.wav
for /f "delims=" %%i in ('dir /ad /s /b ^| findstr /rc:%strRegExp%') do cd "%%i" && echo %%i && dir /b %strFileExtension%
rem for /f "delims=" %%i in ('dir /ad /s /b ^| findstr /rc:%strRegExp%') do cd "%%i" && del %strFileExtension%
cd %strOldDir%
pause


This code is extremely efficient, much faster than the code in posts 13 and 14, and also faster than the code in post 15.

Could moderator 3742668 explain the meaning of findstr /rc:".*\\*\\*$"?

Is the general meaning of this code to list all subdirectories under the current directory, determine whether the lowest-level directory is entirely numeric, and if so delete all audio/video files under that folder?

If so, then it does not match what the OP proposed. d:\music\1234\1345 (with the current directory being drive d), as long as there is a folder name that is not numeric, the files under that kind of folder must not be deleted.

So it is still safer to list all folder names under the current directory and judge whether they are all numeric. If a non-numeric name appears anywhere in the path, then no deletion should be performed.

[ Last edited by pengfei on 2006-10-1 at 03:55 ]
Floor 26 Posted 2006-10-01 12:24 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Re pengfei:
1.findstr /rc:".*\\*\\*$" means the content of the searched string is: ++++. In the search characters of findstr, when special characters are encountered they need to be escaped with \, for example \ should be written as: \\
2.The function of this code is: if among the subdirectories under the current directory, the last two directory levels consist entirely of numbers, then delete all audio/video files under the lowest-level directory.
3.Obviously, you and I do not understand the OP's intent in the same way (that shameless guy, we argued ourselves red in the face over him, and he went missing on us, hehe).
Your understanding is: the lowest two levels are all numeric, and the parent directories are also all numeric (awkward to say, that is, every level of subdirectory under the current directory is all numeric).
My understanding is: the lowest two levels are all numeric, regardless of the parent directories.
However, no matter what the OP's intent is, it isn't important. But as for what you said, "So it's still better to list all folder names under the current directory and judge whether they are all numeric," I personally don't think it's very necessary:
1.findstr part:

@echo off
set strOldDir=%cd%
set Filter="%cd:\=\\%\\*\\*$"
rem set Filter=".*\\*\\*$"
rem set Filter=".*\\*$"
set FileExtension=*.wmv *.rm *.rmvb *.avi *.mp3 *.wma *.wav
set "cmd1=dir /ad /s /b ^| findstr /rc:"
set "cmd2=cd "%%i" && echo %%i && dir /b"
for /f "delims=" %%i in ('%cmd1%%Filter%') do %cmd2% %FileExtension%
cd %strOldDir%
pause


2.Non-findstr part:

for .... do (
set "strPath=%%i"
set "strPath=!strPath:*%cd%\=!"
call :dosomething "!strPath:\="
)
goto :eof

:dosomething
rem Here, besides the code in 21F for filtering numbers,
rem spaces also need to be filtered
rem parameters should use %~* rather than %~1%~2
goto :eof

Talked strategy on paper for half the night again, I'm off to sleep.
Floor 27 Posted 2006-10-02 20:09 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Originally posted by tide2046 at 2006-9-28 05:34:
Now my boss's instruction is: only audio/video files in folders whose parent directory is numeric, and whose subdirectory name is also numeric, may be deleted

That is: for example D:/music/123/*.rm files like this cannot be deleted

But: D:3213/321/*.rm files like this need to be deleted


Hehe~ The OP has been lurking so long without even showing up.

My understanding of the hint he gave above is that files under the music folder in the root of drive D cannot be deleted, but under the music directory there appears a numerically named folder. In that case, moderator 3742668's matching pure numbers from the lowest-level directory is risky, because the 123 folder under music meets the condition, while the hint says files under directories like that must not be deleted.

So I think matching from the current root directory is safest, and the OP mentioned that only audio/video files in folders whose parent directory is numeric and whose child directory is also numeric may be deleted.

What moderator 3742668 mentioned about still needing to filter spaces is very important. I'm wondering why your code runs so fast, I'm a bit puzzled. And yet the code in post 13 by moderator namejm and the code in post 14 use the same regular expression but are much slower. (Although findstr is inside the FOR loop, I don't think that's the reason.)

In post 21, the findstr is connected after dir /s with a pipe command, while the code in posts 13 and 14 does dir /s first, then processes it, then echo followed by a pipe command. Could that be the reason???
Floor 28 Posted 2006-10-02 20:10 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
CLS



Sorry, my network is really bad. It disconnected as soon as I got online. I had to submit several times before it succeeded, and then I saw it had actually submitted duplicate content twice. Sigh...



[ Last edited by pengfei on 2006-10-2 at 20:22 ]
Floor 29 Posted 2010-01-18 16:25 ·  中国 湖北 联通
初级用户
Credits 22
Posts 26
Joined 2009-12-08 14:36
16-year member
UID 156140
Gender Male
Status Offline
Originally posted by 3742668 at 2006-10-1 12:24:
Re pengfei:
1.findstr /rc:".*\\*\\*$" means the searched string content is: ++++. In findstr's search characters ...

Is moderator 3742668 still around... I really admire him... a treasure post worth saving
Forum Jump: