What is the easiest way to replace one string with another sting in 1000 files?

  • VoldemarG

    Hall of Fame

    Points: 3571

    I need to once in a while replace a string of about 200 characters in each text file (~ 2 Mb each) with another string of similar length? There are 1000+ files in the directory, and I want to loop through each, replace the string if found, save file, move to next one, and so on in the entire directory. All files have different name but same extension, either .aspx or .txt.

    It doesn't have to be necessarily a T-SQL solution but ANY way that will not involve substantial programming or time-consuming efforts.


    Voldemar
    likes to play chess

  • VoldemarG

    Hall of Fame

    Points: 3571

    it is Windows 10 or Win Server 2016 OS.


    Voldemar
    likes to play chess

  • pietlinden

    SSC Guru

    Points: 62682

    There's probably a better way, but one option is to automate Word.

    Open file,

    Run Replace

    Save

    Go to next file. (you can use DIR() to get the next file)

  • frederico_fonseca

    SSChampion

    Points: 14392

    use powershell for that - easy in just a few lines.

    see https://powershell.org/forums/topic/find-and-replace-strings-in-multiple-files/

  • Thom A

    SSC Guru

    Points: 98522

    I agree that Powershell would be far easier, and could easily be executed by an Agent Task in SQL Server if needed. Should look something like this:

    $Directory = "C:\temp\YourDirectory"

    $Files = Get-ChildItem -Path $Directory #-Recurse #Uncomment if needed

    foreach ($File in $Files)
    {
    (Get-Content $File) -replace "Find String","Replacement String" | Set-Content $File
    }

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.

  • Jonathan AC Roberts

    SSCoach

    Points: 17166

    Notepad++ is quite good for this.

    You can open multiple files and perform a replace on all files in one go.

  • BTylerWhite

    Mr or Mrs. 500

    Points: 570

    Thom A wrote:

    I agree that Powershell would be far easier, and could easily be executed by an Agent Task in SQL Server if needed. Should look something like this:

    $Directory = "C:\temp\YourDirectory"

    $Files = Get-ChildItem -Path $Directory #-Recurse #Uncomment if needed

    foreach ($File in $Files)
    {
    (Get-Content $File) -replace "Find String","Replacement String" | Set-Content $File
    }

    I really like Thom's answer. I would just consider adding the following to the $Files variable to filter to specific extensions you're looking for:

    $Files = Get-ChildItem -Path $Directory -Include *.aspx, *.txt #-Recurse #Uncomment if needed
  • Thom A

    SSC Guru

    Points: 98522

    BTylerWhite wrote:

    I really like Thom's answer. I would just consider adding the following to the $Files variable to filter to specific extensions you're looking for:

    $Files = Get-ChildItem -Path $Directory -Include *.aspx, *.txt #-Recurse #Uncomment if needed

    Very good point, and an oversight on my part. I should have more carefully noted the OP was only looking at specific extensions. ????

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply