Recursively delete all folders starting with

2019-03-15 11:32发布

I need to write a command in a .bat file that recursively deletes all the folders starting with a certain string. How may I achieve this ?

5条回答
成全新的幸福
2楼-- · 2019-03-15 12:10

rm -rf -- "Directory name"

Ex : rm -rf -- "-2096378"

Above command will deletes the folders/directories starting with - or wildcard characters

查看更多
Fickle 薄情
3楼-- · 2019-03-15 12:14
FOR /F "tokens=*" %i IN ('DIR **[[SearchTerm]]** /A:D /s /b') do rd /S / Q %i
查看更多
放荡不羁爱自由
4楼-- · 2019-03-15 12:17

How about:

for /d %a in (certain_string*) do rd /s %a

This will work from the command prompt. Inside a batch file, you would have to double the %s, as usual:

@echo off
for /d %%a in (certain_string*) do rd /s %%a
查看更多
迷人小祖宗
5楼-- · 2019-03-15 12:30

This is the complete answer you are looking for:

FOR /D /R %%X IN (certain_string*) DO RD /S /Q "%%X"

where obviously you need to replace certain_string with the string your folders start with.

This deletes RECURSIVELY as you asked (I mean it goes throught all folders and subfolders).

查看更多
爷的心禁止访问
6楼-- · 2019-03-15 12:34

Unfinished, I think. If you meant "Recursively go down a directory hierarchy to delete all folders starting with a certain string", then the following might suffice:

for /f "delims=" %%x in ('dir /b /ad abc*') do rd /s /q "%%x"

This will recurse into the directory tree, finding all folders starting with "abc", iterate over that list and removing each folder.

Maybe you need to wrap an if exist around the rd depending on the order in which directories are found and returned. In general, iterating over something and changing it at the same time is rarely a good idea but sometimes it works :-)

查看更多
登录 后发表回答