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 15:18
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Delete audio/video files under folders on drive D whose folder names are numbers (0-999999) View 5,676 Replies 28
Original Poster Posted 2006-09-26 22:52 ·  中国 广东 广州 天河区 电信
新手上路
Credits 18
Posts 7
Joined 2006-09-26 22:39
19-year member
UID 63739
Status Offline
I want to write a batch file to delete all audio/video files under multiple folders (folder names are 0-99999, not consecutive, some may be missing). Please, experts, help answer this. Thanks.
Floor 2 Posted 2006-09-26 23:43 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
Why not just delete the parent directory directly?
Floor 3 Posted 2006-09-26 23:54 ·  中国 广东 广州 天河区 电信
新手上路
Credits 18
Posts 7
Joined 2006-09-26 22:39
19-year member
UID 63739
Status Offline
There are other files in the directories too,
like web page files and so on...
The requirement is only to delete audio/video files

At present, deleting the audio/video files has already been solved

But I don't know how to determine the folders...

Begging the experts for an answer
Floor 4 Posted 2006-09-27 00:05 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
The simplest method:
del /s /a /q *.avi

Check whether the folder exists, then execute the code to delete audio/video files under that folder:
@echo off
for /l %%i in (0,1,9999) do (
if exist %%i (
del /s /a /q %%i\*.avi
)
)
pause
Floor 5 Posted 2006-09-27 01:30 ·  中国 广东 广州 天河区 电信
新手上路
Credits 18
Posts 7
Joined 2006-09-26 22:39
19-year member
UID 63739
Status Offline
This has a bit of a problem. If the folder name starts with 0, for example 0123, then the files in that kind of folder can't be deleted.
Floor 6 Posted 2006-09-27 02:01 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Explain the pattern of the folder names!
Floor 7 Posted 2006-09-27 02:36 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
setlocal ENABLEDELAYEDEXPANSION
for /f %%i in ('dir/ad/b') do (
set /a varnum=%%i
if not !varnum!==0 (
del /q %%i\*.avi
del /q %%i\*.wmv
del /q %%i\*.mpg
del /q %%i\*.mpeg
del /q %%i\*.rm
del /q %%i\*.rmvb
del /q %%i\*.mov
del /q %%i\*.divx
rem If that's still not enough, add some more yourself
)
)
0 00 000 0000 00000 000000 ...these directories aren't handled, but I guess you probably don't have directories like these anyway ^_^

[ Last edited by NaturalJ0 on 2006-9-27 at 02:45 ]
Floor 8 Posted 2006-09-27 02:36 ·  中国 广东 广州 天河区 电信
新手上路
Credits 18
Posts 7
Joined 2006-09-26 22:39
19-year member
UID 63739
Status Offline
They contain only numbers, no other characters at all, and no spaces

The way the numbers are combined is unrestricted. It can be 0, or 654, or 0654, etc. The position of 0 is unrestricted
Floor 9 Posted 2006-09-28 04:45 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
  Try the code below, it might be useful (the code is for demonstration purposes; if you think it's reliable, just change the later echo %%i to del /q %%i):

@echo off
for /f %%i in ('dir /ad /b') do (
echo %%i| findstr "^*$">nul && echo %%i
)
pause
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 10 Posted 2006-09-28 05:34 ·  中国 广东 广州 电信
新手上路
Credits 18
Posts 7
Joined 2006-09-26 22:39
19-year member
UID 63739
Status Offline
Someone gave me a code snippet yesterday, pretty similar to this one

But there's a bit of a problem

This will delete audio/video files in all number-named folders under a certain drive letter

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


Waiting for an expert's guidance
Floor 11 Posted 2006-09-28 05:36 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
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 04:45:
  Try the code below, it might be useful (the code is for demonstration purposes; if you think it's reliable, just change the later echo %%i to del /q %%i):

@echo off
for /f %%i in ('dir /ad /b') do (
...


Brother, that regular expression was used beautifully. The OP's folder names had no pattern to follow (if you talk about a pattern, then only stripping leading zeros and comparing, which isn't advisable). But the regular expression matches folders named with 0-9, successfully solving this difficult problem.
Floor 12 Posted 2006-09-28 05:58 ·  中国 广东 广州 电信
新手上路
Credits 18
Posts 7
Joined 2006-09-26 22:39
19-year member
UID 63739
Status Offline
pengfei, the program you gave above can solve this new requirement of mine

Originally posted by tide2046 at 2006-9-28 05:34:
Someone gave me a code snippet yesterday, pretty similar to this one

But there's a bit of a problem

This will delete audio/video files in all number-named folders under a certain drive letter

Now my boss's instruction is: ...





But the execution efficiency is too low, especially when the amount of data is large
Floor 13 Posted 2006-09-28 07:00 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
  After the OP tested it, the code below can achieve the intent of 'post #10', and the speed is not bad (for now, paths containing spaces have not been considered):

@echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir /ad /b /s') do (
set route=%%i
set route_=!route:%cd%=!
set route_=!route_:\=!
echo !route_!|findstr "^*$">nul && (
for %%j in (avi wmv mpg mpeg rm rmvb mov divx mp3 wav wma swf) do (
if exist %%i\*.%%j del /a /f %%i\*.%%j
)
)
)
pause


  I actually wrote the English word for "path" as root, fixing it a bit, sigh~~~

  Discussed it with pengfei and found that pushd and popd can be avoided, but then the del statement can't be replaced with an echo statement for testing. Deleting has risks, so please make a backup first before deleting.

[ Last edited by namejm on 2006-9-28 at 11:55 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 14 Posted 2006-09-28 07:01 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
at 2006-9-28 05:34:

This will delete audio/video files in all number-named folders under a certain drive letter

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



Finally solved this difficult problem the group owner mentioned, but the speed is still very slow! Please remove the deletion first for testing... Just after posting I found that brother namijm had already written it, and the idea is pretty similar too!

@echo off
::__Delete the specified files in all subdirectories named with pure numbers under the current directory.
::__Before testing, please remove the line del /a /q "!route!\*.%%c" 1>nul 2>nul first; if it works, then execute the deletion!
set root=%cd%
setlocal enabledelayedexpansion
for /f "tokens=*" %%i in ('dir /ad /b /s') do (
set route=%%i
set "route_=!route:%root%=!"
set "route_=!route_:\=!"
echo !route_!|findstr "^*$" 1>nul 2>nul && call :go
)
echo Code execution finished^^!
pause


::__For more audio/video types, add them below in the FOR statement in the current format.
:go
for %%c in (avi mp3 rm rmvb mp4 wmv wav swf) do (
if exist "!route!\*.%%c" (
echo Folder to operate on__!route!
del /a /q "!route!\*.%%c" 1>nul 2>nul
)
)
goto :eof

[ Last edited by pengfei on 2006-9-28 at 20:35 ]
Floor 15 Posted 2006-09-28 07:02 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
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 directories that are not purely numeric (use with caution!). Tests showed that for the same task, post #14 needed about 28 seconds, while this code only needed about 9 seconds.

After repeated testing, it is completely normal and can be run in any directory. There is no problem of not being able to execute when directory names contain spaces. For safety, please first remove the line del /a /q "!route!\*.%%c" 1>nul 2>nul for testing~~~!


@echo off
::__Quickly delete specified files in all subdirectories named with pure numbers under the current directory.
::__Before testing, please remove the line del /a /q "!route!\*.%%c" 1>nul 2>nul first; if it works, then execute the deletion!
set root=%cd%
setlocal enabledelayedexpansion
for /f "tokens=*" %%i in ('dir /ad /b /s') do (
set route=%%i
set route_=!route!:
set "route_=!route_:%root%=!"
set "route_=!route_:\=!"
call :go
)
echo Code execution finished^^!
pause

:go
set num=!route_:~0,1!
if not "!num!"==":" (
set term=
for %%a in (0 1 2 3 4 5 6 7 8 9) do (
if "!num!"=="%%a" (
set route_=!route_:~1!
set term=
goto go
)
)
if "!term!"=="" goto :eof
goto go
)
::__For more audio/video types, add them below in the FOR statement in the current format.
for %%c in (avi mp3 rm rmvb mp4 wmv wav swf) do (
if exist "!route!\*.%%c" (
echo Folder to operate on__!route!
del /a /q "!route!\*.%%c" 1>nul 2>nul
)
)
goto :eof


A little suggestion for the OP: change del /a /q "!route!\*.%%c" 1>nul 2>nul to echo Folder to operate on__!route!>>file.txt

Then when you open file.txt, the folders where deletion needs to be executed will be clear at a glance. Please change it back to del only after confirming it is safe.

[ Last edited by pengfei on 2006-9-28 at 23:05 ]
Forum Jump: