1

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

Shei7141
  • 15
  • 1
  • 2
  • 7

4 Answers4

4

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!

  • Thanks mate, I was wondering if there a solution which would not require any third party software?? – Shei7141 Sep 04 '13 at 21:16
  • @Shei7141 then don't mark it as solved yet. people generally ignore solved questions when looking for problems to help solve :-) – UpAndAdam Sep 04 '13 at 21:25
  • The reason, I marked it solved was simply because this program does the trick.... but you're right... unmarked now.... If anyone know a script that would be great.... I can do find and replace for one file.... but can't seem to get my head around running global find and replace on all the files in the specified folder... – Shei7141 Sep 04 '13 at 21:29
  • What an awesome feature and nice design! – Lonnie Best Mar 29 '18 at 21:40
1

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.

Community
  • 1
  • 1
gmo
  • 8,260
  • 3
  • 36
  • 49
1

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.

Community
  • 1
  • 1
dbenham
  • 119,153
  • 25
  • 226
  • 353
  • Dave, the plain for loop will process some files twice. for /F is needed. – foxidrive Sep 05 '13 at 04:23
  • @foxidrive - Are you sure? That would surprise me, though I haven't tested. I thought since the modified file has the same name as the original, it would never reprocess. – dbenham Sep 05 '13 at 05:09
  • I just tested it and it doesn't reprocess the same filenames - it works fine. – foxidrive Sep 05 '13 at 07:01
0

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

Zac
  • 4,076
  • 3
  • 33
  • 40