No announcement yet.

Read each folder's ini file and compile the values in one txt.

  • Filter
  • Time
  • Show
Clear All
new posts

  • Read each folder's ini file and compile the values in one txt.

    I'm missing something here... I'm trying to go over a mountain when I know there is a easier way around it...

    Get a list of subfolders
    Read a specific value that resides in each subfolder's ini file
    Write the subfolder's name and the specific value of that ini file to a text/csv file in the parent folder 'C:\Folder\newtext.csv'
    Text File Example:
    Subfolder1, inivalue1, inivalue2
    Subfolder2, inivalue1, inivalue2

    Parent Folder: C:\Folder
    Subfolders: C:\Folder\Subfolder1 C:\Folder\Subfolder2.. etc
    Each Subfolder has a "info.ini"

    My Attempt:
    Currently, I have downloaded Ulrich's example here
    Which does not really help me too much... Where do I look for information on how to complete this task. I know it's something to do with tables but unsure...

    I've actually searched this forum for something similar and the above mentioned in the only thing relatively close to what I'm trying to achieve. Any help on this will be appreciated.

    I could just hard code the list of folders myself into the app and just release a newer version when there is a need for a new subfolder but this isn't the way I would want to go.

  • #2
    Hi gt4jk
    I have done a sample for you - when you run it it creates your folders on C; drive , it also copies info.ini files to 3 SubFolders
    It scans the folders and adds to listbox When you select an ini file it gives you the path, then click on populate button to see ini file sections in the other listbox
    It also dispays the value in a paragraph, selecting another ini file and clicking populate does the same and appends to a temp file
    the Save button you can view contents or save to C;\Folder [Note the file is overwritten] on each save
    I hope this will help you


    • #3
      With respect for Charlie's fine example, you might find this a little easier to follow:

      For the sake of somplicity, this example is not coded to check for errors, so the following 3 conditions must be met first.
      • the parent folder resides on the System Drive
      • the parent folder is named "Folder"
      • the INI files reside within the parent folder's subFolders (and contain valid data
      [COLOR=#008000][I]-- Establish which drive is the System Drive[/I][/COLOR]
      local tPath = String.SplitPath(_WindowsFolder);
      local sysDrive = tPath.Drive;
      [COLOR=#008000][I]-- Get a list of subFolders in the folder named "Folder" ...[/I][/COLOR]
      [COLOR=#000000]local[/COLOR] tFolderPaths = Folder.Find(sysDrive.."\\Folder", "*", false, nil);
      if tFolderPaths then
          for k,v in pairs (tFolderPaths) do
              local tPathParts = String.SplitPath(v);
              local subFolder =  tPathParts.Filename;
             [COLOR=#008000][I] -- and write the names of the subFolders to the CSV file in the parent folder[/I][/COLOR]
              TextFile.WriteFromString(sysDrive.."\\Folder\\newtext.csv", subFolder..": ", true);            
              [COLOR=#008000][I]-- Establish the filePath to the INI files[/I][/COLOR]
              local tFiles = File.Find(v, "*ini", false, false, nil, nil);
              if tFiles then
                  for k,v in pairs (tFiles) do
                      local sFilePath = Table.Concat(tFiles, ";", 1, -1);
                      [COLOR=#008000][I]-- Read the values from each subFolder's INI file ...[/I][/COLOR]
                      local tValueNames = INIFile.GetValueNames(sFilePath, "Section");
                      for k,v in pairs (tValueNames) do
                          local sValue = INIFile.GetValue(sFilePath, "Section", v);
                         [COLOR=#008000][I] -- and write those values to the CSV file[/I][/COLOR]
                          TextFile.WriteFromString(sysDrive.."\\Folder\\newtext.csv", sValue .. " ", true);
                      [COLOR=#008000][I]-- while ensuring that values from each INI file are seperated by a new line[/I][/COLOR]
                      TextFile.WriteFromString(sysDrive.."\\Folder\\newtext.csv", "\r\n", true);


      • #4
        It also assumes a 4th condition is met:
        - that the value assigned to the Section Name of the INI files is: "Section".