Rik Telner
Rik Telner

Reputation: 279

How to let batch script browse through folders?

I have a script, that modifies specific files all the time.

findstr /s /m "BLABLABLA" C:\BLABLABLA\*.BLA > bla.bla
if %errorlevel%=="0" (
    BLABLABLA
)

I know that "Blabla" isn't very transparent...

How to make a BAT file. That finds every file with BLA extension, that contains BLABLABLA in every place, every folder, every partition. Iterating through a folder using batch script It does work only in 1 folder.

Upvotes: 0

Views: 275

Answers (3)

Endoro
Endoro

Reputation: 37569

finds every file with BLA extension,

that contains BLABLABLA

in every [...], every folder, every partition

try:

 for %%i in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do findstr /lsimpc:"BLABLABLA" "%%i:\*.BLA" 2>nul

in every place, [...]

specify: place

Upvotes: 2

joojaa
joojaa

Reputation: 4434

Just guessing are you looking for something like:

@echo off

for /r %%f in (*.bla) do (
    for /f %%i in ('findstr /m "BLABLABLA" "%%~f"') do (
        echo do something to "%%~f"
    )
) 

add the drive letter resolution form Ansgar Wiechers post

Upvotes: 1

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

Try something like this:

@echo off

setlocal

set DRIVES=C D E F G H I J K L M N O P Q R S T U V W X Y Z

(for %%d in (%DRIVES%) do (
  if exist "%%d:\" (
    pushd "%%d:\"
    for /r %%f in (*.BLA) do (
      findstr /m "BLABLABLA" "%%~f" && (
        BLABLABLA
      )
    )
    popd
  )
)) > bla.bla

A somewhat more elegant approach would enumerate the local drives via WMI:

@echo off

setlocal

(for /f %%d in (
  'wmic logicaldisk where drivetype^=3 get caption ^| find ":"'
) do (
  pushd "%%d\"
  for /r %%f in (*.BLA) do (
    findstr /m "BLABLABLA" "%%~f" && (
      BLABLABLA
    )
  )
  popd
)) > bla.bla

Upvotes: 2

Related Questions