![]() |
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-25 23:27 |
47,812 topics / 349,910 posts / today 0 new / 48,264 members |
| DOS批处理 & 脚本技术(批处理室) » Delete audio/video files under folders on drive D whose folder names are numbers (0-999999) |
| Printable Version 5,926 / 28 |
| Floor1 tide2046 | Posted 2006-09-26 22:52 |
| 新手上路 Posts 7 Credits 18 | |
|
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.
|
|
| Floor2 NaturalJ0 | Posted 2006-09-26 23:43 |
| 银牌会员 Posts 533 Credits 1,181 | |
|
Why not just delete the parent directory directly?
|
|
| Floor3 tide2046 | Posted 2006-09-26 23:54 |
| 新手上路 Posts 7 Credits 18 | |
|
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 |
|
| Floor4 pengfei | Posted 2006-09-27 00:05 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
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 |
|
| Floor5 tide2046 | Posted 2006-09-27 01:30 |
| 新手上路 Posts 7 Credits 18 | |
|
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.
|
|
| Floor6 pengfei | Posted 2006-09-27 02:01 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Explain the pattern of the folder names!
|
|
| Floor7 NaturalJ0 | Posted 2006-09-27 02:36 |
| 银牌会员 Posts 533 Credits 1,181 | |
|
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 ] |
|
| Floor8 tide2046 | Posted 2006-09-27 02:36 |
| 新手上路 Posts 7 Credits 18 | |
|
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 |
|
| Floor9 namejm | Posted 2006-09-28 04:45 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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):
|
|
| Floor10 tide2046 | Posted 2006-09-28 05:34 |
| 新手上路 Posts 7 Credits 18 | |
|
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 |
|
| Floor11 pengfei | Posted 2006-09-28 05:36 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
Originally posted by namejm at 2006-9-28 04:45: |
|
| Floor12 tide2046 | Posted 2006-09-28 05:58 |
| 新手上路 Posts 7 Credits 18 | |
|
pengfei, the program you gave above can solve this new requirement of mine
Originally posted by tide2046 at 2006-9-28 05:34: But the execution efficiency is too low, especially when the amount of data is large |
|
| Floor13 namejm | Posted 2006-09-28 07:00 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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):
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 ] |
|
| Floor14 pengfei | Posted 2006-09-28 07:01 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
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 ] |
|
| Floor15 pengfei | Posted 2006-09-28 07:02 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
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~~~! 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 ] |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |