0

I need to replace about 2000 strings with another string, i tried writting a VS Add-In to iterate through a list of strings and to replace one by one in all files, somthing like this:

foreah(item in strings)
{
    findWin.FindWhat = item;
    findWin.ReplaceWith = "string2";
    findWin.Action = vsFindAction.vsFindActionReplaceAll;
    findWin.Target = vsFindTarget.vsFindTargetFiles;
    findWin.KeepModifiedDocumentsOpen = true; 
    findWin.WaitForFindToComplete = true;                   
    findWin.Execute();
}

However, it only works if i set the find options to OpenDocuments, otherwise it throws an AccessViolation exception. There are about 8 projects in the solution and a lot files in there.

Is there any clean/better way to achieve that?

Thank you

somedude
  • 169
  • 3
  • 12
  • Is this something that you can accomplish with -> `Refactor -> Rename` ? – Bob Kaufman Jun 11 '14 at 19:05
  • If you're just trying to change the values in your code editor, just use ctrl+h, change the box to find in entire solution, then type what you want in the find/replace text boxes. If you are talking about replacing the *value* of 2000 strings at runtime, use an array and a loop. – Evan L Jun 11 '14 at 19:06
  • @BobKaufman: Renaming variable/classes, sure. But I think he's trying to replace within string values. – Jeff Mercado Jun 11 '14 at 19:07
  • 2
    There really is no safe way to do it automatically. I think you're better off searching for the string in the project and doing the replacements manually. Maybe use other static analysis tools to help you find search specifically within string values, but still do the replacement by hand. – Jeff Mercado Jun 11 '14 at 19:08
  • 1
    @JeffMercado - good point. Let's ask OP for clarification. csharp_beginner: what exactly are you looking to replace? A variable name? A frequently occurring string literal? – Bob Kaufman Jun 11 '14 at 19:09
  • Personally, if you are replacing 2000 strings in an entire directory of a large number of files, I would simply use Perl, which does this rather quickly, in about 15 lines of code or less, I believe perl has a replace command for files and command line switches to do *.cs for example. – CodeCowboyOrg Jun 11 '14 at 20:04
  • If I were you, I believe I'd be better off writing a simple batch file or C# or whatever language you're most comfortable with and just string replace the *.cs files... – kevin Jun 11 '14 at 20:13
  • @BobKaufman, it is an old solution that has hard coded strings for language resources, i am working on to convert them to strongly typed resource keys. so i wrote this add-in and when i run this, it launches VS 2012 and then i run this command from Tools menu – somedude Jun 11 '14 at 20:50
  • Is it really something that should be done in Visual Studio? There's an old DOS utility called FART http://stackoverflow.com/a/2363075/253938 that might fill your needs. – RenniePet Jun 12 '14 at 04:10
  • Thank you for your suggestions, i finally wrote a PS script to find/replace and it worked well. – somedude Jun 13 '14 at 15:12

1 Answers1

0

You can try the DTE.Find.FindReplace method which is better suited when you don't need to interact with the Find/Replace dialog.

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66