0

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.

4
  • 1
    Ok to clarify the asker wants to process all files containing "BLABLABLA" Commented Jul 13, 2013 at 11:33
  • Oh, you mean that. Why didn't you just tell, and started to complicate everything. busy with editing. Commented Jul 13, 2013 at 11:36
  • Please give better details in your question. Do you want to find something like *.txt files on a set of drive letters? The files have to contain a "String" so I assume they need to be text format files, right? Does the script have to detect all drive letters? What if one is a DVD disk in a DVD drive? Can you set the list of drive letters in the batch file? Commented Jul 13, 2013 at 12:18
  • Yes. Files that contain specific strig, but not only .txt files, I want to scan all files that can be opened by notepad. All drive letters, including DVD disc and USBs. I would like to keep user away from setting list of drives to scan. Commented Jul 13, 2013 at 12:21

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

16 Comments

Ok, but. I want to make changes to *.BLA files, where is it possible to do this?
@RikTelner Your original question is not clear as to what you want to actually do so you must live with some sort of vagueness in answers. Because you yourself are being overly vague on the subject of what the error level is used for. Its not apparent form your question that you actually want to modify files since your demo only modifies ONE file while reading many. The reason I didn't suggest an answer much like the one above is that i didn't understand what the error checking is for. But id dint catch that your new description either.
Dear Ansgar, your script works correctly. But it finds all .BLA files, not only the ones that contain "BLABLABLA".
@RikTelner Then you'll get only .BLA files containing the string "BLABLABLA".
The script worked fine even with .bat files when I tested it. If you can't disclose more details, this is as much help as I can provide.
|
2

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

4 Comments

Works exactly how I wanted! You answered 3 of my questions correctly. You are really good! Thank you, it works very good, and how as I wanted. Could you provide me links to some information about %%i %%f etc. ? Because these are ones that I doesn't know yet, and I can't Google it since Google doesn't accept percentage signes.
Is there possibility to make file names, print in file? Changing >nul to >result.txt doesn't work. Since result.txt stays empty, even if prompt says there is one.
Try this: 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" >result.txt 2>nul. Basic help about loop parameters you can find at the command line for /?, more advanced help ... in the net, eg. dostips.
Thank you for your help, but someone already helpfully answered the question. Thanks for trying helping though, that's why you get upvotes. Thanks for link, helps a lot!
1

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

2 Comments

There's no need for the inner loop if you're not processing the output anyway. Just do findstr /m "BLABLABLA" "%%~f" && (...).
@AnsgarWiechers Yes, your right, the lines were just copy pasted form something that actually does need that. Mostly this answer is redundant now anyway because you updated yours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.