Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to search & replace a string in text file using a batch file  (Read 104531 times)

0 Members and 1 Guest are viewing this topic.

Salmon Trout

  • Guest
Re: How to search & replace a string in text file using a batch file
« Reply #15 on: November 18, 2019, 11:41:30 AM »
....


Salmon Trout

  • Guest
Re: How to search & replace a string in text file using a batch file
« Reply #16 on: November 18, 2019, 11:46:17 AM »
Quote
Does it mean: In string1, replace 'Mary' with 'Peter, Paul and Mary' and then put that in string 5, and then, in the resulting string 5, replace 'lamb' with 'lamb each'?

Yes, that's exactly right. I could have used a single ampersand (command1 & command2 means 'do command 1 and then do command 2") but I am in the habit from Unix/Linux of using && - (command1 && command2 means 'do command1 and if it is successful do command2'). Works the same in Windows.

They could just as easily have been on two lines like this, I guess I wanted to show what you can do on one line.

set string5=%string1:Mary=Peter, Paul and Mary%
set string5=%string5:lamb=lamb each%


H4ckRn00b

    Topic Starter


    Rookie

    Thanked: 2
    • Experience: Beginner
    • OS: Windows 7
    Re: How to search & replace a string in text file using a batch file
    « Reply #17 on: November 18, 2019, 11:51:18 AM »
    That VBScript has a feature I don't like. It overwrites the input file.
    Yes, that is by design.

    Quote
    As for Question 1, you can place a one-line batch script in your Startup folder

    cscript //nologo path/to/scriptname.vbs
    What does ' //nologo' do?

    Quote
    If you code the actual paths to the input and output .txt files in the script, you can put the .vbs script itself in the Startup folder.
    I've tried that, it's unsure if that works.

    Quote
    This all looks a bit shaky though. If this is corporate data in a work setting, should you be doing this?
    No, it's just about a multiboot situation where Win10 was installed later, and thus, changes the "D-station" to a "C-station". Home stuff. Corporate, I wish. Not savvy enough for that.
    Thanks Salmon for your efforts.
    Do you know the Salmon song?
    "Salmon, Salmon, Salmon,
    You're so delicious
    I love you more
    than all the other fishes"

    H4ckRn00b

      Topic Starter


      Rookie

      Thanked: 2
      • Experience: Beginner
      • OS: Windows 7
      Re: How to search & replace a string in text file using a batch file
      « Reply #18 on: November 18, 2019, 12:42:46 PM »
      If you code the actual paths to the input and output .txt files in the script, you can put the .vbs script itself in the Startup folder.

      I've tried it out now and it doesn't work.


      The Startup folder I use is this one:
      C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\

      And:
      D:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\


      If I put the .vbs file into these folders, how do I ensure that they are actually executed on start up?
      Excuting them manually is no problem

      Salmon Trout

      • Guest
      Re: How to search & replace a string in text file using a batch file
      « Reply #19 on: November 18, 2019, 12:46:16 PM »
      What does ' //nologo' do?

      There are two 'script engines' in Windows, cscript.exe and wscript.exe. The cscript engine uses a console window to run a script. This means you can have lines of text output that you can read all at once. By default it shows a text 'logo' like this at the start of a script:

      C:\Users\Mike>cscript //logo test.vbs
      Microsoft (R) Windows Script Host Version 5.812
      Copyright (C) Microsoft Corporation. All rights reserved.

      Hello World


      The //nologo switch gives just the script output

      C:\Users\Mike>cscript //nologo test.vbs
      Hello World


      This is test.vbs by the way

      wscript.echo "Hello World"

      The wscript.exe engine does not show a console window, and emits any output using message boxes (one for each wscript.echo statement, and you have to click OK with the mouse). You don't have to worry about a 'logo'.



      Quote
      I've tried that, it's unsure if that works.

      Can't you check?

      Quote
      Do you know the Salmon song?

      I like that song. However, I am named after a steam railway locomotive, London and North Eastern Railway No. 2506 Salmon Trout later renumbered to 60041 by British Railways in 1948. The loco was a member of a class all named after famous racehorses, so that's where it started.



      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: How to search & replace a string in text file using a batch file
      « Reply #20 on: November 18, 2019, 03:53:59 PM »
      I think this is a candidate for  'Thread of the Month' !  ;D

      H4ckRn00b

        Topic Starter


        Rookie

        Thanked: 2
        • Experience: Beginner
        • OS: Windows 7
        Re: How to search & replace a string in text file using a batch file
        « Reply #21 on: November 18, 2019, 08:33:20 PM »
        I think this is a candidate for  'Thread of the Month' !  ;D
        to quote 10 year old Ben from "Outnumbered (2007)": ............."Sarcasm!"

        H4ckRn00b

          Topic Starter


          Rookie

          Thanked: 2
          • Experience: Beginner
          • OS: Windows 7
          Re: How to search & replace a string in text file using a batch file
          « Reply #22 on: November 18, 2019, 08:43:01 PM »
          Can't you check?
          I did. Now, after several tries & reboots, I got it right. Joy. Yah. I can give you the full script and .... context, but I'm not sure that'd be interesting to you.

          Quote
          (...)later renumbered to 60041 by British Railways in 1948. (...)
          Oooooooh..... hobby's showing! ;) ;)  obsessive, a bit?  Pure dedication, I'm sure.... ;) ;)  <3 :D ;D
          Quote
          (...)named after famous racehorses, (...)
          Locs with fish names, named after horses ...... Ya can't get more British than that! <3  ;) ;) ;D