Shei7141
Shei7141

Reputation: 15

find and replace in multiple batch files

I have over 1000 batch files in a folder.

Is there a way to run a script to find a particular text string (lets say Apples) and replace Apples with (Mangos) in all the files?

Thanks in advance

Shei

Upvotes: 1

Views: 16813

Answers (4)

Zac
Zac

Reputation: 4695

Ended up installing and using Notepad++ for the OP same goal while batch programming.

Loaded up Notepad++, then Ctrl+H, Find in Files tab, then filled the fields like this and struck the Replace in Files button.

enter image description here

Upvotes: 0

dbenham
dbenham

Reputation: 130829

There is a very simple solution using a FOR loop coupled with a hybrid JScript/batch utility called REPL.BAT. The utility is pure script that runs on any modern Windows machine from XP onward - no 3rd party executables required. It performs a regex search and replace on stdin, and writes the result to stdout.

Assuming REPL.BAT is in your current directory, or better yet, somewhere within your PATH:

for %%F in (*.bat) do (
  type "%%F"|repl Apples Mangos >"%%F.new"
  move /y "%%F.new" "%%F"
)

REPL.BAT has many options that make it quite powerful for such a small bit of code. Complete documentation is built into the script.

Upvotes: 1

gmo
gmo

Reputation: 9000

You can find many examples here in stackoverflow to do'it in pure batch-script.

Take a look here for the replace part:
How to replace string inside a bat file with command line parameter string
Or here:
How can you find and replace text in a file using the Windows command-line environment?

And here:
Loop through file names in a Batch Script
and here:
Iterate all files in a directory using a 'for' loop
to loopover the files in a directory....

Those are just a few ones, but there's many more.

Now If you are able to consider a third-party software...
... I can't go without recommend WinGrep for Windows, very very usefull, and of course, auto-replace for search results (also regEx search)

Good luck any way


[edit]
(I add this because st2 was the accepted solution)
I'm also a SublimeText user, and for me, ST2 + winGrep + DiffMerge it's a fantastic trio for development under Windows.

Upvotes: 1

DresNightfire
DresNightfire

Reputation: 41

You can use Sublime Text (it's a free unlimited evaluation) to 'Find > Find in Files'. The find and replace options will appear at the bottom. Type 'apples' in 'Find:', type your location in 'Where', and type 'mangos' in 'replace', then click 'Replace.' Goodluck!

Upvotes: 4

Related Questions