Announcement

Collapse
No announcement yet.

using batch script to delete or modify data in a text file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • using batch script to delete or modify data in a text file

    Hi All -

    Please show me how to modify or delete data in the text file using a batch script. I am having trouble when trying to use the "for" statement to get rid of the quotation (") in the text file. I was able to delims everything else except quotation (").

    Wonder whether anyone has any idea???

    Thanks,

    JT

  • #2
    Re: using batch script to delete or modify data in a text file

    Can you post your batch file so we know what and how you are trying to do it exactly.

    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: using batch script to delete or modify data in a text file

      Here is a sample of the text file that I was trying to parse through (contains 3 lines):

      528864&ctu=%2Fmailinglist%2Findex%2Ecfm%3Fstory%3D multilist%5EListID%3D7%5Eaction%3Dleave%5Eemail%3D gec460^ui=528864"
      1263225&ctu=%2Fmailinglist%2Findex%2Ecfm%3Fstory%3 Dmultilist%5EListID%3D7%5Eaction%3Dleave%5Eemail%3 Dmtrmouthb^ui=1263225"
      198266&ctu=%2Fmailinglist%2Findex%2Ecfm%3Fstory%3D multilist%5EListID%3D7%5Eaction%3Dleave%5Eemail%3D yy8740^ui=198266"

      I am trying to delims the quotation (") at the end of each line by using the for statement, but it never works:

      FOR /F "usebackq tokens=* delims=" " %G IN (textfile.txt) DO ECHO %G

      JT

      Comment


      • #4
        Re: using batch script to delete or modify data in a text file

        Originally posted by ttt123
        FOR /F "usebackq tokens=* delims=" " %G IN (textfile.txt) DO ECHO %G
        You showed the syntax for a command-line, in a batch one should use %%G of course.


        Here is an alternative you can use
        (batch):
        Code:
        @echo off
        setlocal enabledelayedexpansion
        
        FOR /F "usebackq delims=" %%G IN ("textfile1.txt") DO (
          Set "line=%%G" & echo !line:"=!
        )>>"textfile2.txt"
        If you like to set enabledelayedexpansion as default so it is also enabled when using a command-line from a command-prompt, you'll have to add a dword entry DelayedExpansion = 1 to the registry
        in: HKEY_CURRENT_USER\Software\Microsoft\Command Processor
        or in: HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor



        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: using batch script to delete or modify data in a text file

          Thank you so much! Your script works like a charm!

          JT

          Comment


          • #6
            Re: using batch script to delete or modify data in a text file

            Hi all!
            I'm new to batch scripts and petri!

            Rems, I was using your code to delete text from a .txt file, however the string that I need to delete contains "|" (pipe), the full string I need to delete is 11391|. I can either remove the 11391 with:

            @echo off
            setlocal enabledelayedexpansion
            FOR /F "usebackq delims=" %%G IN ("RBE.txt") DO (
            Set "line=%%G" & echo !line:11391=!
            )>>"RBE1.txt"

            or remove the entire line with:

            find /v "11391|" < RBE.txt > RBE1.txt

            I am so close but not quite there, I've even tried it in quotes or removing "|" seperately.
            Any help would be great!

            mrt2005

            Comment


            • #7
              Re: using batch script to delete or modify data in a text file

              You could make use of a subroutine that is called for each line from within the loop - so the string substitution can be performed outside the loop.

              example:
              Code:
              @echo off
              setlocal enabledelayedexpansion
               
              FOR /F "usebackq delims=" %%G IN ("c:\RBE.txt") DO (
                Set Line=%%G
                Set Line=!Line:"='!
                Call:replace "!Line!"
              )
               
              pause
               
               
              goto:eof ------------
               :replace subroutine
               (Set Line=%*&Set Line=!Line:~1,-1!)
               Set Line=!Line:'="!
               Set "Line=!Line:11391|=!"
               echo.!Line! >>"c:\RBE.2.txt"
              goto:eof ------------
              note:
              The lines:
              Set Line=%%G
              Set Line=!Line:"='!
              Replaces quote signs (if exist in the file) with a single-quote sign
              And in the subroutine singe-quotes will we reversed to quote signs again. This way you can avoid that quotes existing in the line will be seen as separate parameter on the call:replace line

              \Rems

              This posting is provided "AS IS" with no warranties, and confers no rights.

              __________________

              ** Remember to give credit where credit's due **
              and leave Reputation Points for meaningful posts

              Comment


              • #8
                Re: using batch script to delete or modify data in a text file

                Rems,

                Thank you so much for the quick response, it works a treat. I've been working on this for about a week now!

                Thanks again,

                mrt2005

                Comment

                Working...
                X