Announcement

Collapse
No announcement yet.

Grab text from txt file?

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

  • Grab text from txt file?

    I have a batch file that pipes output to a txt file. What I would like to do is assign a variable (say %FRAMES%, %RES%, and %FRAMERATE%) to a number in the txt file.

    For example the txt file has the following output:

    Code:
    C:\Personal\Videos\BSG_S3.0_E3_Exodus Part 1.avs: 720x480, 48000/2002 fps, 62770 frames
    fopen("b3") failed
    I would like to assign 62770 to the variable %FRAMES%
    720x480 to the variable %RES%
    and 48000/2002 to the variable %FRAMERATE%

    Any help is appreciated. Thanks.
    Last edited by rack04; 11th February 2010, 21:40.

  • #2
    Re: Grab text from txt file?

    Try using the set command:

    Code:
    set res=720x480
    set frames=62770
    set frames=48000/2002

    Comment


    • #3
      Re: Grab text from txt file?

      Originally posted by rack04 View Post
      I would like to do is assign a variable (say %FRAMES%, %RES%, and %FRAMERATE%) to a number in the txt file.
      This one is hard, as the usual string tokenizing with spaces as separators won't work. Some Galactica episode names contain spaces and some do not. This is an ideal case for regular expressions, but in plain old batch the support is limited indeed.

      Any modernish scripting language like Powershell or VBScript is way more suitable. And they are free too!

      Here is a Powershell example that parses the required info with regular expressions.

      Code:
      $str = "C:\Personal\Videos\BSG_S3.0_E3_Exodus Part 1.avs: 720x480, 48000/2002 fps, 62770 frames"
      
      $resoPattern = "\d+x\d+"
      $fpsPattern = "\d+/\d+"
      $framesPattern = "(\d+)(?: frames)"
      
      $mc = [regex]::match($str, $resoPattern)
      $mc.groups[0].Value
      
      $mc = [regex]::match($str, $fpsPattern)
      $mc.groups[0].Value
      
      $mc = [regex]::match($str, $framesPattern)
      $mc.groups[1].value
      -vP

      Comment


      • #4
        Re: Grab text from txt file?

        Originally posted by georgeeye View Post
        Code:
        set res=720x480
        set frames=62770
        set frames=48000/2002
        You miss the point. The question is how to get the variable parameters from a text file. Setting the variable is easy, getting the value to set is not.

        -vP

        Comment


        • #5
          Re: Grab text from txt file?

          Originally posted by georgeeye View Post
          Try using the set command:
          Code:
          set res=720x480
          set frames=62770
          set frames=48000/2002
          Correct, that shows how to set variables for this cmd.exe instance environment only.

          note,
          To set Environmental Variable for the machine or for the currently logged on user one should use SETX.exe OR SetEnv.exe.


          Below is a sample batch that reads a txt file. Only the lines containing the word "frames" are broken into words. These words are tested by the following patterns;

          pattern 1 = word must start and end with digits and in between is a singel "x"
          pattern 2 = word must start and end with digits and in between is a single "/"
          pattern 3 = word (number) starts and ends with digits no other characters embedded.

          If the word (number) matches pattern 3, it will be set as value to the FRAMES variable
          If the word matches pattern 2, it will be set as value to the FRAMERATE variable
          If the word matches pattern 1, it will be set as value to the RES variable

          Al three Values must be found on one and the same line - if not then none of the Variables will be Set by that line.

          Code:
          @echo off
          Set "txtFile=MyFile.txt"
          
          For /f "tokens=*" %%! in (
             'findstr.exe /ri "\<frames\>" "%txtFile%"') do (
             call:fetchValues %%!
          )
          
          :: echo results
          Set FRAMES
          Set RES
          Set FRAMERATE
          
          echo\&pause
          
          goto:eof
          :fetchValues - subroutine -
             Setlocal
             for %%* in (%*) do (
               >nul (echo\%%*|Findstr.exe /ri "^[0-9]*\x[0-9]*$") &&(
                   Set t$tRES=%%*)
               >nul (echo\%%*|Findstr.exe /r "^[0-9]*/[0-9]*$") &&(
                   Set t$tFRAMERATE=%%*)
               >nul (echo\%%*|Findstr.exe /r "^[0-9]*$") &&(
                   Set t$tFRAMES=%%*)
             )
             If defined t$tRES If defined t$tFRAMERATE If defined t$tFRAMES (
               Endlocal
               Set "RES=%t$tRES%"
               Set "FRAMERATE=%t$tFRAMERATE%"
               Set "FRAMES=%t$tFRAMES%"
             )
          exit /b 0
          \Rems
          Last edited by Rems; 13th February 2010, 21:43.

          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

          Working...
          X