Deleting folder with {
and/or [
in the name, by invoking:
cmd.exe /s rm some_folder_with_{special}_character_they_[name]`:
cmd /v/s/c "del /q /f "%%~dpnxi*.*" && rmdir /q "%%~dpnxi" && echo\Deleted:"%%~dpnxi"
@echo off && setlocal enabledelayedexpansion
set "_mp3dir=F:\SUPER_USER\Q1507690\Organize"
set "_filter=%temp%\_filter_audio_!random!.log"
set "_audios=aa3,aac,aif,flac,m3u,m4a,mka,mp3,mpa,mpc,oga,ogg,pla,wav,wma"
type nul >"!_filter!" && for %%i in (!_audios!)do >>"!_filter!" echo=.%%~i
for /d /r "%_mp3dir%" %%i in (*)do tree /a /f "%%~fi"|find "\-" >nul || set "_rm_dir=yEp" && (for /f tokens^=* %%# in ('
2^>nul %__APPDIR__%where.exe "%%~dpnxi:*"')do type "!_filter!" | find /i "%%~x#" >nul && set "_rm_dir=nOp"
if "!_rm_dir!"=="yEp" cmd /v/s/c "echo\del /q /f "%%~dpnxi\*.*" && echo\rmdir /q "%%~dpnxi" && echo\Deleted:"%%~dpnxi"
)
set "_filter="<nul & del /q /f "%temp%\_filter_audio_*.log" & endlocal & goto :EOF
Test and if its seems zOk to you, just remove this echo
command below:
if "!_rm_dir!"=="yEp" cmd /v/s/c "echo\del /q /f "%%~dpnxi*.*" && echo\rmdir /q "%%~dpnxi" && echo\Deleted:"%%~dpnxi"
@echo off && setlocal enabledelayedexpansion
set "_mp3dir=F:\Organize"
set "_filter=%temp%\_filter_audio_!random!.log"
set "_audios=aa3,aac,aif,flac,m3u,m4a,mka,mp3,mpa,mpc,oga,ogg,pla,wav,wma"
type nul >"!_filter!" && for %%i in (!_audios!)do >>"!_filter!" echo=.%%~i
for /d /r "%_mp3dir%" %%i in (*)do tree /a /f "%%~fi"|find "\-" >nul || set "_rm_dir=yEp" && (for /f tokens^=* %%# in ('
2^>nul %__APPDIR__%where.exe "%%~dpni:*"')do type "!_filter!" | find /i "%%~x#" >nul && set "_rm_dir=nOp"
if /i "!_rm_dir!" == "yEp" echo\rmdir /q "%%~dpni" ^&^& Folder: "%%~dpni" was deleted^!!
)
set "_filter="<nul & del /q /f "%temp%\_filter_audio_*.log" & endlocal & goto :EOF
- Define (
set
) all the necessary extensions to keep your audio files in the folder tree:
set "_audios=aa3,aac,aif,flac,m3u,m4a,mka,mp3,mpa,mpc,oga,ogg,pla,wav,wma"
- Save all extensions in a file to use with
findstr
when checking the output of the listing for each folder (the last one at each level), using a for
loop in the _audios
variable previously defined:
type nul >"!_filter!" && for %%i in (!_audios!)do >>"!_filter!" echo=.%%~i
- Use a specific loop for folders and recursive (
for /d /r
) at the same time checking if the current folder in the loop is the last folder at the current level (tree ..|find "\-"
)that is, without subfolder below...
for /d /r "%_mp3dir%" %%i in (*)do tree /a /f "%%~fi"|find "\-" >nul
Note: This method is explained in detail in this answer
- Use a
for /f
loop additional to list files at the last level of each subfolder, and with the where
command redirecting the file extension to the find
to search the log file for the occurrence.
(for /f tokens^=* %%# in ('
2^>nul %__APPDIR__%where.exe "%%~dpni:*"')do type "!_filter!" | find /i "%%~x#" >nul
- When
find
command finds an audio extension at the output of the loop, the variable _rm_dir
will change the string from yEp
to nOp
, preventing the current folder from being deleted/removed, and when the opposite happens, where no existing file or none with an audio extension, the variable will keep the value of _rm_dir
for yEp
.
set "_rm_dir=yEp" && (for /f tokens^=* %%# in ('
2^>nul %__APPDIR__%where.exe "%%~dpni:*"')do type "!_filter!" | find /i "%%~x#" >nul && set "_rm_dir=nOp"
- After the previous loop is finished,
if
will take the action (if necessary) to delete/remove the folder without audio files, when the condition is true for the comparison of variable _rm_dir
equal to string yEp
:
if /i "!_rm_dir!" == "yEp" echo\rmdir /q "%%~dpni" ^&^& Folder: "%%~dpni" was deleted^!!
- Test and check the output, if it looks correct, just remove the
echo\
and specific
^
from ^&^&
:
if /i "!_rm_dir!" == "yEp" echo\rmdir /q "%%~dpni" ^&^& Folder: "%%~dpni" was deleted^!!
- The
echo
command is to give you a preview of the actions resulting from the script
if /i "!_rm_dir!" == "yEp" echo\rmdir /q "%%~dpni" ^&^& Folder: "%%~dpni" was deleted^!!
[Refer to the Update]
To use your command to delete folders with a special character in the name, just invoke cmd
with flag /s
:
cmd /v/s/c "del /q /f "%%~dpnxi*.*" && rmdir /q "%%~dpnxi" && echo\Deleted:"%%~dpnxi"
rmdir /q "F:\SUPER_USER\Q1553051\Organize\540x21" && Folder: "F:\SUPER_USER\Q1553051\Organize\540x21" was deleted
rmdir /q "F:\SUPER_USER\Q1553051\Organize\54x320" && Folder: "F:\SUPER_USER\Q1553051\Organize\54x320" was deleted
rmdir /q "F:\SUPER_USER\Q1553051\Organize\12x349" && Folder: "F:\SUPER_USER\Q1553051\Organize\12x349" was deleted
rmdir /q "F:\SUPER_USER\Q1553051\Organize\543x21" && Folder: "F:\SUPER_USER\Q1553051\Organize\543x21" was deleted
rmdir /q "F:\SUPER_USER\Q1553051\Organize\123x45" && Folder: "F:\SUPER_USER\Q1553051\Organize\123x45" was deleted
rmdir /q "F:\SUPER_USER\Q1553051\Organize\12x346" && Folder: "F:\SUPER_USER\Q1553051\Organize\12x346" was deleted
rmdir /q "F:\SUPER_USER\Q1553051\Organize\empty_" && Folder: "F:\SUPER_USER\Q1553051\Organize\empty_" was deleted
But, please consider paying attention to the comment @somebadhat:
Rename F:\!Organize
to F:\Organize
Try to rename your folder by removing !
, even if only for running this script.
f:\!organize
tof:\organize
. Modify your question to include every music file extension you want to search for and post it this way:*.aac, *.m4a, ...
Or list the extensions of the files you would like to delete:*.txt, *.log, *.jpg
. I would prefer an actualdir /b /s /a-d
in text that represents the directory structure of the problem you are trying to solve. Does something like this occur?:music103\1.jpg
music103\6\1.flac
?