Announcement

Collapse
No announcement yet.

Extract part of a text file

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

  • Extract part of a text file

    Hi.
    I've got a text file (actually it is more like a json file, with the .opt extension). Anyway, somewhere in this file there is a plain-text line:

    InstalledPackagesPath "Specific_Path"

    Basically I need to open it, search thru it to find that line (it occurs just once) and create a variable from what is written in quotation marks. I would appreciate some help

  • #2
    --Read the file to table (the file referenced in the variable):
    mytable = TextFile.ReadToTable("%usercfg%");

    Basically I need to find the line which contains InstalledPackagesPath: and extract from that line information located between quotation marks.

    Comment


    • #3
      My preferred approach is using dkjson for reading JSON files in Lua.
      https://forums.indigorose.com/forum/...from-json-file

      Ulrich

      Comment


      • #4
        Thanks.
        This is not really a json file apparently... The very line is simple text, without any brackets etc. Last line:

        (...)long list(...)
        {SSSSS
        Enabled 1
        Quality 2
        }
        {AA
        Enabled 1
        Type 2
        Quality 2
        }
        {Bloom
        Enabled 1
        Quality 2
        }
        {DOF
        Enabled 1
        Quality 2
        }
        {Buildings
        Enabled 1
        Quality 2
        }
        {VegetationLarge
        Enabled 1
        Quality 2
        }
        {VegetationSmall
        Enabled 1
        Quality 2
        }
        {TextureSynthesis
        Quality 2
        }
        {PostProcess
        Enabled 1
        EyeAdaptation 1
        ColorGrading 1
        Sharpen 1
        Fringe 1
        LensDistortion 0
        Dirt 1
        LensFlare 1
        FilmGrain 1
        Vignette 1
        LensBlurMultiplier 1.000000
        FringeMultiplier 1.000000
        }
        {VectorDataTessellation
        Enabled 1
        Quality 2
        }
        }
        InstalledPackagesPath "C:\Path"

        Comment


        • #5
          Try this:

          Code:
          -- read the file into a string
          local sConfig = TextFile.ReadToString(SessionVar.Expand("%SourceFolder%\\config.opt"));
          -- find what matters
          local sPath = string.match(sConfig, ".*InstalledPackagesPath \"(.*)\".*");
          -- present the result
          Dialog.Message("", sPath);
          Ulrich

          Comment


          • #6
            I am shocked.

            You are my hero.

            LOL

            Comment


            • #7
              OK, I am probably a total idiot. :-/

              So how do I double the "" in the string for the correct path?

              Comment


              • #8
                Disregard. Too much work and too little coffee...

                Comment


                • #9
                  Sorry, I was away. Do you have a solution?

                  Ulrich

                  Comment


                  • #10
                    Sort of, but sometimes it works and sometimes it doesn't.
                    I am not sure if perhaps I need to use the tostring command somehow...

                    Comment


                    • #11
                      WOW, I think I just guessed it...

                      mytable = TextFile.ReadToTable("%usercfg%");
                      -- read the file into a string
                      local sConfig = TextFile.ReadToString(SessionVar.Expand("%usercfg% "));
                      -- find what matters
                      local sPath = string.match(sConfig, ".*InstalledPackagesPath "(.*)".*");
                      MYpath = tostring(sPath)

                      Comment

                      Working...
                      X