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.

Community
  • 1
  • 1
Rik Telner
  • 179
  • 1
  • 10
  • 1
    Ok to clarify the asker wants to process all files containing "BLABLABLA" – joojaa Jul 13 '13 at 11:33
  • Oh, you mean that. Why didn't you just tell, and started to complicate everything. *busy with editing*. – Rik Telner Jul 13 '13 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? – foxidrive Jul 13 '13 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. – Rik Telner Jul 13 '13 at 12:21

3 Answers3

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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Ok, but. I want to make changes to *.BLA files, where is it possible to do this? – Rik Telner Jul 13 '13 at 11:24
  • @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. – joojaa Jul 13 '13 at 11:28
  • Dear Ansgar, your script works correctly. But it finds all .BLA files, not only the ones that contain "BLABLABLA". – Rik Telner Jul 13 '13 at 11:31
  • Yes but your demo has an interdiction many to one how on earth are we supposed to understand that it is a many to many interdiction but you just dont know how to do that? – joojaa Jul 13 '13 at 11:31
  • Yes fine but then type that question in English don't let us guess what you think. – joojaa Jul 13 '13 at 11:34
  • I bet on my head, that my post is written in English. 8/ – Rik Telner Jul 13 '13 at 11:35
  • @RikTelner The proposed solutions only list files with the extension `.BLA` and only if they contain a string "BLABLABLA". If you get other files, you're doing something differently from what I suggested. – Ansgar Wiechers Jul 13 '13 at 11:51
  • @AnsgarWiechers I just directly copied and changed commando, file and extension. – Rik Telner Jul 13 '13 at 11:58
  • 1
    @RikTelner Then you'll get only .BLA files containing the string "BLABLABLA". – Ansgar Wiechers Jul 13 '13 at 12:03
  • @AnsgarWiechers Ehm, it works for `.BLA` but doesn't works for `.BAT` although I changed script for it. – Rik Telner Jul 13 '13 at 12:14
  • @RikTelner Then the .bat files don't contain the string "BLABLABLA". – Ansgar Wiechers Jul 13 '13 at 19:28
  • @AnsgarWiechers It did, it really did. But, someone else provided answer, thank you for help though. You helped much aswell. – Rik Telner Jul 14 '13 at 12:11
  • 1
    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. – Ansgar Wiechers Jul 14 '13 at 12:12
  • Sorry, I am dumb >,< if it workes correctly for you, that means I'd messed up. I try to figure out. – Rik Telner Jul 14 '13 at 12:15
  • Yep, I messed it up. My fault, I am so dumb. It works now. Thank you. Exactly what I wanted. – Rik Telner Jul 14 '13 at 12:18
  • Sorry for causing problems; and thank you very much once again. – Rik Telner Jul 14 '13 at 12:20
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

Community
  • 1
  • 1
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • 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. – Rik Telner Jul 14 '13 at 12:13
  • 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. – Rik Telner Jul 14 '13 at 12:14
  • 1
    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](http://dostips.com). – Endoro Jul 14 '13 at 13:17
  • 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! – Rik Telner Jul 15 '13 at 09:26
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

joojaa
  • 4,354
  • 1
  • 27
  • 45
  • There's no need for the inner loop if you're not processing the output anyway. Just do `findstr /m "BLABLABLA" "%%~f" && (...)`. – Ansgar Wiechers Jul 13 '13 at 11:55
  • @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. – joojaa Jul 13 '13 at 11:57