How to use FOR /R with wildcards

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
xteenf
Posts: 3
Joined: 22 Aug 2012 07:26

How to use FOR /R with wildcards

#1 Post by xteenf » 22 Aug 2012 07:52

Hi. This looks like a really good forum. Big hello to everyone.
I'm trying to understand the dos for loop. This works ..

Code: Select all

for /r d:\ %a in (redcar) do rmdir /s /q %a

but this doesn't

Code: Select all

for /r d:\ %a in (*dcar*) do rmdir /s /q %a

and this definitely doesn't work

Code: Select all

for %a in (dir d:\ | find /i /n "dcar") do rmdir /s /q %a

how do I use wildcards properly in this situation or do I have to use a different form of the for loop?
Big thanks in advance.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: first post

#2 Post by foxidrive » 22 Aug 2012 07:56

This is one way, based upon the method you used:

The /n switch in the find command seems wrong, so I removed it.

Code: Select all

for /f "delims=" %a in ('dir d:\ /b /s ^| find /i "dcar"') do rmdir /s /q "%a"


This can be used but dcar will match in the short filename and the long filename, and so can give you false hits.

Code: Select all

for /f "delims=" %a in ('dir d:\*dcar* /b /s ') do rmdir /s /q "%a"

xteenf
Posts: 3
Joined: 22 Aug 2012 07:26

Re: How to use FOR /R with wildcards

#3 Post by xteenf » 22 Aug 2012 15:32

Hi foxidrive. Thank you for replying. I admit the penny hasn't really dropped yet .. I had a mishap earlier and hard deleted a lot of stuff by accident. I think I'll practice only on the library computers in future :)

Can you explain why the caret symbol is needed in your first example?
Is it the escape character? I dont mean to take a lend of you, but could you parse the line into English for me? These dos for loops are completely foreign to anything I've seen before. If "delims=" is optional, what is its purpose here? (Sorry I must seem really thick :oops: )

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to use FOR /R with wildcards

#4 Post by Ed Dyreen » 22 Aug 2012 15:40

Code: Select all

:: -----------------------------------------------------------------------------------------------------------
:: - Looping through dirs or files with /D /R
:: -----------------------------------------------------------------------------------------------------------
:: (
   :: dirs
   :: (
      echo. &echo. dir absolute
      for /d            %%? in ("%~dp0*")   do echo.%%?

      echo. &echo. dir absolute, recursive
      for /d /r "%~dp0" %%? in ("*")        do echo(%%?

      echo. &echo. dir relative to active directory
      for /d            %%? in ("*")        do echo.%%?
   :: )

   :: files
   :: (
      echo. &echo. file absolute
      for               %%? in ("%~dp0*.*") do echo.%%?

      echo. &echo. file absolute, recursive
      for    /r "%~dp0" %%? in ("*.*")      do echo(%%?

      echo. &echo. file relative to active directory
      for               %%? in ("*.*")      do echo.%%?
   :: )
:: )
:: -----------------------------------------------------------------------------------------------------------


foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to use FOR /R with wildcards

#5 Post by foxidrive » 22 Aug 2012 19:07

xteenf wrote:Can you explain why the caret symbol is needed in your first example?
Is it the escape character?


Yes, it is. Some special characters need to be escaped, such as < > | etc.

could you parse the line into English for me? These dos for loops are completely foreign to anything I've seen before. If "delims=" is optional, what is its purpose here?


for /f "delims=" %a in ('dir d:\ /b /s ^| find /i "dcar"') do rmdir /s /q "%a"

for /f... and "delims=" ensures that no delimiters are applied so the output from the command is not altered.

dir d:\ /b /s ^| find /i "dcar"

This performs a dir from the root of d: and returns brief information and recurses through subdirectories. It provides a list of files and folders with paths. Add /a:-d to filter out the folders, and only use the list of files. It pipes the list through find, searching for dcar in the long file/folder names and is case insensitive.

The output of that is used in the "%%a" token.

xteenf
Posts: 3
Joined: 22 Aug 2012 07:26

Re: How to use FOR /R with wildcards

#6 Post by xteenf » 23 Aug 2012 03:06

for /f... and "delims=" ensures that no delimiters are applied so the output from the command is not altered.

Oh .. that makes sense now, thanks foxidrive. I was reading it 'use default delimiter'. Thanks again, great forum.

Post Reply