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

Author Topic: Replace String with Wildcards using DOS Batch file!  (Read 30587 times)

0 Members and 1 Guest are viewing this topic.

ghostdog74



    Specialist

    Thanked: 27
    Re: Replace String with Wildcards using DOS Batch file!
    « Reply #15 on: August 24, 2010, 06:03:38 AM »
    Show some effort!!!!  You have no idea what your are talking about!  I have spent over 6 weeks reading books and researching several sites!  I work on it around the clock!  attempting to use the example below!  I just don't post all my efforts here!  I would have over 20 pages of crap!
    sidewinder has shown you how to do it with batch, although not to your requirement. But i am expecting you follow his guidance on that piece of snippet and work on it. That's the effort i am talking about. I am not expecting you to post all your already done scripts here.  So i am saying,  are you really waiting for him to solve your own (project/homework) problem?

    Quote
    If you decide not to help!  Then do so Keep your replies silents!  I don't need any negitive responces!  >:(  Thank you!
    we are not here to do your work/project for you. As you already saw, I have helped you a lot. I have shown you ways you could do it with ease, and others have shown you native ways to do it with batch and vbscript. BUT the problem is caused by you yourself. Only DOS is allowed ? Typical project/school homework restriction , isn't it ?

    If the DOS you mentioned is really the MSDOS 6.22 , it most probably can't be done in a pure DOS, except you have to use some extra tools. If it can be done, it would probably be obscure and arcane. Either way, you are on my blacklist of people not to help from now on.

    Sidewinder



      Guru

      Thanked: 125
    • Experience: Familiar
    • OS: Windows 10
    Re: Replace String with Wildcards using DOS Batch file!
    « Reply #16 on: August 24, 2010, 08:12:29 AM »
    I must have misinterpreted your original post about the numerics after the PT to be deleted too. In any case, that created a real problem when trying to create a batch file to do just that. Seems those special characters in the data file cause the NT interpreter to choke.

    By the way, the VBScript I posted only removed the PT not the following numerics. This little ditty fixes that problem:

    Code: [Select]
    Const ForReading = 1
    Const ForWriting = 2

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objRE = CreateObject("VBScript.RegExp")
    objRE.Global     = True
    objRE.IgnoreCase = False

    Set inFile = fso.OpenTextFile("d:\wfc\sniplib\WSH-RegEx-putSearchReplace.txt", ForReading)
    Set outFile = fso.OpenTextFile("c:\temp\Regex.chg", ForWriting, True)

    Do Until inFile.AtEndOfStream
    strLine = inFile.ReadLine
    objRE.Pattern = "^GOTO\s(.*?)\bPT\s{2}[0-9]{1,}\b"
    Set colMatches = objRE.Execute(strLine)
    If colMatches.Count > 0 Then 
    objRE.Pattern = "PT\s{2}"               'remove PT leave remaining digit(s)
      objRE.Pattern = "PT\s{2}[0-9]{1,}\b"   'remove PT and remaining digit(s) 
    strLine = objRE.Replace(strLine, "")
    End If
    outFile.WriteLine strLine
    Loop

    Considering you have Win7 and all those tools available, I would have thought a batch solution would be your last choice.

    Good luck.  8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    graymj

      Topic Starter


      Rookie

      Thanked: 1
      Re: Replace String with Wildcards using DOS Batch file!
      « Reply #17 on: August 24, 2010, 08:43:22 AM »
      Thank You Sidewinder!  I got your vb to work as well!  Yes there was issues
      understanding my orginal request!  I read everything I could find on the subject! But there seem to be no way to do this in DOS! Learn alot from U
      and your example!  Thank many time more! :)

      graymj

        Topic Starter


        Rookie

        Thanked: 1
        Re: Replace String with Wildcards using DOS Batch file!
        « Reply #18 on: August 24, 2010, 08:56:41 AM »
        Sorry ghostdog74 you feel that way! And as Sidewinder has come to undrstand that my orginal requirements were missunderstoud!  Which I attempted to point out with several examples!  I want no one to do my work!  I had a programming issue and tried to throw it out here to help myself and others learning batch programming on DOS, which I'm more
        than sure this solution will!  I'm well versed in Pearl & writing UNIX Scripts,
        both would only require one liners to solve this issue!  But there was a company standard for whatever reason to only use DOS!  which I'm attempting to do!   Best Wishes and Thanks for your help in the pass as well!

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1096
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 8
        Re: Replace String with Wildcards using DOS Batch file!
        « Reply #19 on: August 24, 2010, 09:38:11 AM »
          I'm well versed in Pearl

        Pearl?

        For some reason I suspect you meant:

        Perl

        I was trying to dereference Null Pointers before it was cool.

        graymj

          Topic Starter


          Rookie

          Thanked: 1
          Re: Replace String with Wildcards using DOS Batch file!
          « Reply #20 on: August 24, 2010, 09:50:56 AM »
          That would be correct!  Thanks for the correction! ;)

          Sidewinder



            Guru

            Thanked: 125
          • Experience: Familiar
          • OS: Windows 10
          Re: Replace String with Wildcards using DOS Batch file!
          « Reply #21 on: August 24, 2010, 10:00:50 AM »
          I hate unfinished business, so I came up with this monstrosity. I apologize if it looks something like Curly the Neanderthal would mark on the cave wall. I gotta learn to stop over thinking things.

          Code: [Select]
          @echo off
          setlocal enabledelayedexpansion
          if exist c:\temp\regex.chg del c:\temp\regex.chg

          for /f "tokens=* delims=" %%f in (c:\temp\regex.txt) do (
            set strLine=%%f
            set subLine=!strLine:~0,4!
            if /i .!subLine! EQU .GOTO call :getLoc
            echo !strLine! >> c:\temp\regex.chg
          )
          goto :eof

          :getLoc
            for /l %%i in (0, 1, 67) do (
              call set strChunk=%%strLine:~%%i,2%%
              if .!strChunk! EQU .PT call set strLine=%%strLine:~0,%%i%% & goto :eof

            )
            goto :eof

          Change the file paths as appropriate. If the position of PT changes you may need to change the 67 value in the for /l statement to increase the search range.

          Good luck. 8)
          The true sign of intelligence is not knowledge but imagination.

          -- Albert Einstein

          graymj

            Topic Starter


            Rookie

            Thanked: 1
            Re: Replace String with Wildcards using DOS Batch file!
            « Reply #22 on: August 24, 2010, 12:44:00 PM »
            WOW!  HA HA!  How did you come of with this?  I can't wait to try it!

            the logic seem wild!  I'm trying to break it down now! 8)  THANKSSSSSSSSSSS!

            graymj

              Topic Starter


              Rookie

              Thanked: 1
              Re: Replace String with Wildcards using DOS Batch file!
              « Reply #23 on: August 24, 2010, 03:10:58 PM »
              HI Sidewinder It Works Great!  Just having a few problems intergrateing into my code!  but thats a small problem!  also developeing a method to
              Identify which column the PT start and pass that value to your routine!
              You're the BEST!  THANKS AGAIN & AGAIN!

              Sidewinder



                Guru

                Thanked: 125
              • Experience: Familiar
              • OS: Windows 10
              Re: Replace String with Wildcards using DOS Batch file!
              « Reply #24 on: August 24, 2010, 03:50:22 PM »
              There is no need to send the starting position of PT to the routine. The whole point was to make the code generic. The code found PT in the data file you posted at offset 64 in all the records that contained it.

              The :getLoc routine processes records that have GOTO in the first four bytes. By starting at offset 0 (record position 1), it increases the record position by 1, reading 2 byte chunks of the record until PT is found  or offset 67 is reached, whichever comes first. The 67 was arbitrary. Once the offset of PT is found (stored in the %%i token), the code uses truncation to eliminate the high order bytes. The logic is fairly simple, it was the batch notation that was challenging.

              The FOR /L %variable IN (start,step,end) DO statement uses the "end" parameter as the indicator when to stop the loop. In this case it represents the highest offset to check before quitting the loop. You can exceed the record length without the code throwing an error. Ensure the "end" parameter is large enough to include the entire record without being so large as to needlessly waste CPU cycles.

              The code can probably be tweaked for more efficiency. For instance, the search for PT could start at offset 4; we already know offsets 0-3 contains GOTO

              Hope this helps.  8)
              « Last Edit: August 24, 2010, 04:10:53 PM by Sidewinder »
              The true sign of intelligence is not knowledge but imagination.

              -- Albert Einstein

              graymj

                Topic Starter


                Rookie

                Thanked: 1
                Re: Replace String with Wildcards using DOS Batch file!
                « Reply #25 on: August 24, 2010, 07:56:47 PM »
                Thanks!  I was busy changeing the 67 thinking it was a start position!  but after checking out other sites realized it was like an range!  Your explanation was great!  This is avery useful piece of code!  Thx Again for the info!

                drocks



                  Newbie

                  • Experience: Beginner
                  • OS: Unknown
                  Re: Replace String with Wildcards using DOS Batch file!
                  « Reply #26 on: December 29, 2010, 12:05:52 PM »
                  Is there a way to modify the VBS code example (posted by Sidewinder - thanks by the way) to replace the expression found, ie, find /.*$ (evrything from the slash to enf of line) and replace it with a blank?  I am new to vbs, and am not sure what the syntax is to substitute something for the found expression -

                  In advance, thanks!

                  drocks



                    Newbie

                    • Experience: Beginner
                    • OS: Unknown
                    Re: Replace String with Wildcards using DOS Batch file!
                    « Reply #27 on: December 30, 2010, 09:35:55 AM »
                    Scratch my previous post, I did not see the ealier post from Sidewinder with an example of exactly what I needed (once again, thanks very much!).  I modified it slightly for what I needed (below), and it works beautufully.
                    Thanks Sidewinder!!

                    Const ForReading = 1
                    Const ForWriting = 2

                    Set fso = CreateObject("Scripting.FileSystemObject")
                    Set objRE = CreateObject("VBScript.RegExp")
                    objRE.Global     = True
                    objRE.IgnoreCase = True

                    Set inFile = fso.OpenTextFile("c:\temp\test3.txt", ForReading)
                    Set outFile = fso.OpenTextFile("c:\temp\test33.txt", ForWriting, True)

                    Do Until inFile.AtEndOfStream
                       strLine = inFile.ReadLine
                       objRE.Pattern = "=/.[^/]*"
                       Set colMatches = objRE.Execute(strLine)
                       If colMatches.Count > 0 Then 
                           objRE.Pattern = "=/.[^/]*"   'remove everyting from = up to but not including next /, replace with =
                          strLine = objRE.Replace(strLine, "=")
                       End If
                       outFile.WriteLine strLine
                    Loop

                    kmwittko



                      Newbie

                      • Experience: Beginner
                      • OS: Unknown
                      Re: Replace String with Wildcards using DOS Batch file!
                      « Reply #28 on: March 29, 2011, 06:43:55 PM »
                      To replace all occurrences of one string in a file by another string, there is even a simpler solution:

                      @echo off
                      setlocal enabledelayedexpansion
                      for /f "tokens=* delims=" %%f in (%1) do (
                        set strLine=%%f
                        set strLine=!strLine:   = !
                      ::                  tab^   ^space
                        echo !strLine! >> %1
                      )

                      kmwittko



                        Newbie

                        • Experience: Beginner
                        • OS: Unknown
                        Re: Replace String with Wildcards using DOS Batch file!
                        « Reply #29 on: March 30, 2011, 02:14:14 PM »
                        Update:

                        1) The output file, of course should be #2 :-(
                        2) Only exclamation marks, the text between two exclamation marks, and empty lines that are lost. All the other known problem characters (both outside and inside of "..." and "%...%") are retained.