We help IT Professionals succeed at work.

batch script to find folders and del files

icecom4 used Ask the Experts™
The script below finds files in folders named "bquest", however I need the script edited to find folders that BEGIN with "bquest".  For example, the script should find the folder called "bquest" and also the folder called "bquestx86", and then delete the files.  

set folder=
for %%D in (C D E F G) do (
for /F "tokens=*" %%P in ('if exist %%D: dir /a:d /s/b %%D:\*"bquest" ^| findstr /L /E /i "\bquest"') do (
     del /s /q "%%~P\*.tmp" >NUL 2>&1
     del /s /q "%%~P\~~*.pk3" >NUL 2>&1
     del /s /q "%%~P\zz*.pk3" >NUL 2>&1
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi icecom4,
Perhaps this will help steer you in the right direction...

"directories only

for /D [%% | %]variable in (set) do command [command-parameters]

If set contains wildcards (* and ?), specifies to match against directory names instead of file names"

http://ss64.com/nt/for_d.html   For /D
Try this:
for %%D in (C D E F G) do (
  for /f "tokens=*" %%P in ('if exist %%D:\ dir /a:d /b /s %%D:\bquest*') do (
    del /s /q *.tmp ~~*.pk3 zz*.pk3 >nul 2>&1

Open in new window

I have not had time to work out the whole thing, but using  /d /r will recursively iterate the directory parsing on folders beginning with "string", which is where I think I'd start reworking this for adjusting the focus ferom files to folders.  
for /d /r %%P in (string*) do ----

Paul, wouldn't your recommendation still look at "files beginning with" the string?
wouldn't your recommendation still look at "files beginning with" the string?
Nope. DIR's '/a:d' (or just '/ad') command line option only returns foldernames - and not filenames.


Actually, I've just spotted an error in my code (above) - I haven't associated the search path %%P with the DEL command. Here's the code again:
for %%D in (C D E F G) do (
  for /f "tokens=*" %%P in ('if exist %%D:\ dir /a:d /b /s %%D:\bquest*') do (
    del /s /q "%%P\*.tmp" "%%P\~~*.pk3" "%%P\zz*.pk3" >nul 2>&1

Open in new window

I think that should do it...

Normally, yes, I would use 'FOR /D' and 'FOR /R' however, the user has this unconventional

    FOR /F IN (IF EXIST... DIR...) DO...

expression. If that's what he's comfortable with then so be it.

Another thing to bear in mind is you can't do this:
FOR /D %%a IN (*) DO (
  FOR /R "%%a" %%b IN (.) DO (

Open in new window

because DOS attempts to resolve "%%a" as FOR /R's variable rather than it's starting path (even though it's in double-quotes) and therefore, treats %%b as an error.

It won't even work when using an intermediate delayed expanded variable like this:
FOR /D %%a IN (*) DO (
  SET Drive=%%a
  FOR /R "!Drive!\" %%b IN (.) DO (

Open in new window

Infact, 'FOR /R' on it's own will work with "%Drive%:\" but not with "!Drive!:\" however, as you are probably aware, we can't use "%Drive%:\" because we're in a FOR-loop and therefore, we must delay it's expansion.

It's a pity because we could have continued with:
SET Folder=%%~nxb
IF /I "!Folder:~0,6" EQU "bquest" (

Open in new window

but that's not possible here.
Bill PrewTest your restores, not your backups...
Top Expert 2016

You can do this in a subroutine though, and pass in the folder name.  I'll leave adapting this approach to Paul for this question.

FOR /R "%~1\" %%b IN (.) DO (

Okay, billprew has forced my hand on this one...

As bill rightly points out, it can be accomplished with a CALL. See the following code:
@echo off
  setlocal enabledelayedexpansion
  for %%a in (C D E F G) do if exist %%a:\ call :searchdrive %%a
exit /b

  for /r %1:\ %%b in (.) do (
    title %%b
    set folder=%%~nxb
    if /i "!folder:~0,6!" equ "bquest" del "%%~fb\*.tmp" "%%~fb\~~*.pk3" "%%~fb\zz*.pk3" >NUL 2>&1
goto :eof

Open in new window

Line 8 provides a visual indication that the batch file is running by displaying paths in DOS's window's titlebar. It can be omitted if so desired.