中国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-10-24 06:34
47,815 topics / 349,928 posts / today 1 new / 48,287 members
DOS批处理 & 脚本技术(批处理室) » Delete audio/video files under folders on drive D whose folder names are numbers (0-999999)
Printable Version  6,464 / 28
Floor16 tide2046 Posted 2006-09-28 21:41
新手上路 Posts 7 Credits 18
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
Floor17 NaturalJ0 Posted 2006-09-28 21:48
银牌会员 Posts 533 Credits 1,181
For backup, just use the mouse.
Floor18 pengfei Posted 2006-09-28 22:52
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
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.
Floor19 namejm Posted 2006-09-28 22:53
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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.
Floor20 pengfei Posted 2006-09-28 23:03
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
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 ]
Floor21 3742668 Posted 2006-09-29 01:49
荣誉版主 Posts 718 Credits 2,013
Part 1:


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

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


Note: None of the above code has been tested.
Floor22 pengfei Posted 2006-09-30 10:01
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
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 ]
Floor23 kmlxk Posted 2006-09-30 11:13
初级用户 Posts 17 Credits 45
Looking up in admiration~~ really an expert
Floor24 3742668 Posted 2006-09-30 12:52
荣誉版主 Posts 718 Credits 2,013
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:"
Floor25 pengfei Posted 2006-10-01 03:44
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
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 ]
Floor26 3742668 Posted 2006-10-01 12:24
荣誉版主 Posts 718 Credits 2,013
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:

2.Non-findstr part:

Talked strategy on paper for half the night again, I'm off to sleep.
Floor27 pengfei Posted 2006-10-02 20:09
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
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???
Floor28 pengfei Posted 2006-10-02 20:10
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
CLS



[ Last edited by pengfei on 2006-10-2 at 20:22 ]
Floor29 iPoint Posted 2010-01-18 16:25
初级用户 Posts 26 Credits 22
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
Prev  1 2
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023