Announcement

Collapse
No announcement yet.

.bat Find and replace text

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

  • .bat Find and replace text

    I have some files that contain some of if not all of the following:

    Program RxBIN RXPCN RxGroup MemberID

    not all files will have all of those headers but the ones that do I will need to replace so it looks like this:

    @Program @RxBIN @RXPCN @RxGroup @MemberID


    Thanks in Advance,
    Joe

  • #2
    Re: .bat Find and replace text

    If it is a word document, you can write a macro to do it -- you do not have to use a bat file. if it is some other format, please give us a clue!
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      Re: .bat Find and replace text

      Solved

      I found the solution http://www.dostips.com/?t=Batch.FindAndReplace

      had to rewite it a bit to actually get the job done.

      I am sure there is an easier way of doing this but I got it to work

      call BatchSubstitute.bat Program @Program InDesignData.txt >newfile.txt

      Code:
      if not exist newfile.txt goto skip
      del "InDesignData.txt"
      ren "newfile.txt" "InDesignData.txt"
      :skip
      
      call BatchSubstitute.bat RxBIN @RxBIN InDesignData.txt >newfile.txt 
      if not exist newfile.txt goto skip
      del "InDesignData.txt"
      ren "newfile.txt" "InDesignData.txt"
      :skip
      
      call BatchSubstitute.bat RXPCN @RXPCN InDesignData.txt >newfile.txt 
      if not exist newfile.txt goto skip
      del "InDesignData.txt"
      ren "newfile.txt" "InDesignData.txt"
      :skip
      
      call BatchSubstitute.bat RxGroup @RxGroup InDesignData.txt >newfile.txt 
      if not exist newfile.txt goto skip
      del "InDesignData.txt"
      ren "newfile.txt" "InDesignData.txt"
      :skip
      
      call BatchSubstitute.bat MemberID @MemberID InDesignData.txt >newfile.txt 
      if not exist newfile.txt goto skip
      del "InDesignData.txt"
      ren "newfile.txt" "InDesignData.txt"
      :skip
      Last edited by biggles77; 7th September 2010, 13:58. Reason: Wrap script in CODE tag

      Comment


      • #4
        Re: .bat Find and replace text

        Thanks for posting back with your find.
        1 1 was a racehorse.
        2 2 was 1 2.
        1 1 1 1 race 1 day,
        2 2 1 1 2

        Comment


        • #5
          Re: .bat Find and replace text

          Names of the labels that are used in a batch must be unique. Your batch is using :skip multiple times and therefore the batch won't work as expected.
          Further more, your batch will add an "@" also if there was already a leading @ added to the header text.

          A good command line utility for text processing that I would recommend is gnu sed for Windows (free for download)!

          But if you don't like to use tools (like sed or gawk) - you can try this "all in one batch" solution,
          Code:
          @echo off & SETLOCAL ENABLEEXTENSIONS
          
          Set "file=InDesignData.txt"
          
          color 6A & echo please wait.. & title parsing file, "%file%"
          
          call:BatchSubstitude "%file%" > "newfile.txt"
          
          MOVE /Y "newfile.txt" "InDesignData.txt"
          
          goto:EOF
          :BatchSubstitude *** source http://www.dostips.com/?t=Batch.FindAndReplace
             SETLOCAL DISABLEDELAYEDEXPANSION
             for /f "tokens=1,* delims=]" %%A in ('"type "%~1"|find /n /v """') do (
               set "line=%%B"
               if defined line (
                   call:replace "Program" "@Program"
                   call:replace "RxBIN" "@RxBIN"
                   call:replace "RXPCN" "@RXPCN"
                   call:replace "RxGroup" "@RxGroup"
                   call:replace "MemberID" "@MemberID"
          
                   call set "line=echo.%%line%%"
                   for /f "delims=" %%* in ('"echo."%%line%%""') do (%%~*)
               ) ELSE (echo.))
             ENDLOCAL
             exit /b 0
             :replace
             >nul 2>&1 (
             call set line |Findstr.exe /RIC:"\<%~1\>" && call set line |Findstr.exe /RIVC:"\<@%~1\>" && call set "line=%%line:%~1=%~2%%")
          exit /b 0
          Instead of defining a single file (InDesignData.txt) it would also be possible to modify the batch so it loops through all files of a particular folder (or directory).

          \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


          • #6
            Re: .bat Find and replace text

            Might have to use this one

            Thanks!

            Comment


            • #7
              Re: .bat Find and replace text

              Here is a quick Powershell alternative (from Powershell.com iirc):

              Replace Text in Files
              Often, some text will need to be replaced in a text file. That's easy with Get-Content and Set-Content - or not?

              Get-Content c:\somefile.txt | Foreach-Object { $_ -replace 'old', 'new' } | Set-Content c:\somefile.txt

              If you try this, PowerShell will complain that the file is in use and can't be written to. PowerShell cannot read and write to a file at the same time. Your solution: use parenthesis so that PowerShell reads the file first and only and then processes the content:

              (Get-Content c:\somefile.txt) | Foreach-Object { $_ -replace 'old', 'new' } | Set-Content c:\somefile.txt

              Comment

              Working...
              X