+ Reply to Thread
Results 1 to 5 of 5

Thread: [Help] Simultaneously Substitying A String By An Other In Multiple Files

  1. #1
    Advanced User Renk's Avatar
    Join Date
    17.08.08
    Location
    Elsewhere
    P2P Client
    utorrent
    Posts
    582
    Activity Longevity
    1/20 19/20
    Today Posts
    0/5 ssssss582

    [Help] Simultaneously Substitying A String By An Other In Multiple Files

    Hello,

    My need is very basic and trivial: I'm on w10, and have to substitute a given string, name it string1, by an other, name it string2, in multiple files (with the same extension) in some given directory DIR. Obviously, I could use any text editor for that but in each files there is only one occurrence of string1, and moreover there are dozens of files in which the substitution has to be done, and I want obviously the substitution to be done in all the file at the same time.

    So, I though to make a .bat file containing the following command:
    Code:
    powershell -Command "(gc C:\DIR\* .txt) -replace 'string1', 'string2' | Out-File -encoding ASCII C:\DIR\* .txt"
    But it doesn't work. Not string1 occurrence was replaced after I launched the .bat file.

    Nevertheless, when I precisely indicate the file (ie in using the command powershell -Command "(gc C:\DIR\file1 .txt) -replace 'string1', 'string2' | Out-File -encoding ASCII C:\DIR\file1 .text"), string1 is replaced by string2 in file1.text. But then I have to do that for each of my dozens of files, which doesn't fulfill my needs.


    Surely 1337 c0d3r2 here will have a solution: how to perform the desired substitution in all the files at the same time???
    Last edited by Renk; 17.06.21 at 21:07.
    Primo Avulso Non Deficit Alter
    Reply With QuoteReply With Quote
    Thanks

  2. #2
    Moderator anon's Avatar
    Join Date
    01.02.08
    Posts
    39,804
    Activity Longevity
    11/20 19/20
    Today Posts
    1/5 ssss39804
    I recommend installing busybox for Windows and then using tr or sed, depending on which sort of string replacement you need to do. You'll thank me in the long run, because PowerShell's syntax is so confusingly complicated, even minor tasks like these become difficult.

    If you absolutely can't rely on external dependencies, I'll have a look.
    "I just remembered something that happened a long time ago."
    Reply With QuoteReply With Quote
    Thanks

  3. Who Said Thanks:

    Renk (21.06.21)

  4. #3
    JohnareyouOK's Avatar
    Join Date
    31.01.19
    Location
    Earth
    P2P Client
    BiglyBT
    Posts
    254
    Activity Longevity
    0/20 7/20
    Today Posts
    0/5 ssssss254
    Open all files in Notepad++, Ctrl-H, Replace All in All Opened Documents

    Here's details: https://www.makeuseof.com/tag/how-to...ultiple-files/
    Reply With QuoteReply With Quote
    Thanks

  5. Who Said Thanks:

    Renk (21.06.21)

  6. #4
    Advanced User Renk's Avatar
    Join Date
    17.08.08
    Location
    Elsewhere
    P2P Client
    utorrent
    Posts
    582
    Activity Longevity
    1/20 19/20
    Today Posts
    0/5 ssssss582
    Thanks to both of you. I wasn't aware of BusyBox existence. Very Nice. But I was little in a hurry, too less time to learn precisely how to use it. Now that I've had a closer look at this tool, it's clearer. I just finally used a tool named WildReplace, found on a so 95' looking site. I realized only after these operations that I could use NotePad++ for as well for opening files in bulk and then perform the needed substitutions.

    But nevertheless, having to use apps or tools like np++ or be WildReplace for doing such an elementary task seems overkill to me: The substitution should be possible with 4 lines of code (as on Unix), not thousands. :(
    Primo Avulso Non Deficit Alter
    Reply With QuoteReply With Quote
    Thanks

  7. #5
    Moderator anon's Avatar
    Join Date
    01.02.08
    Posts
    39,804
    Activity Longevity
    11/20 19/20
    Today Posts
    1/5 ssss39804
    Quote Originally Posted by Renk View Post
    But nevertheless, having to use apps or tools like np++ or be WildReplace for doing such an elementary task seems overkill to me: The substitution should be possible with 4 lines of code (as on Unix), not thousands. :(
    I guess this illustrates the design philosophy differences between both systems quite well. The Subsystem for Linux bridges that gap somewhat...
    "I just remembered something that happened a long time ago."
    Reply With QuoteReply With Quote
    Thanks

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •