How to look for 8 different files

  1. 3 weeks ago

    Working on small project, not very familiar with xojo

    Wanted to see how would i be able to look for 8 different .config files.

    For example if A.config file is found run this command

    if B.config file if found than run this command

    every config file would run different commands.

  2. Paul R

    Jan 25 Pre-Release Testers, Xojo Pro Kirkland, WA

    Assuming the files were in a single folder, you could iterate through the files in that folder. For each file whose name ends with “.config” you could extract the name part and use a select case statement to run your particular commands.

  3. Yes, if all files were to be available they would be in the same folder.

  4. Emile S

    Jan 25 Europe (France, Strasbourg)

    What is difficult (for you) ?

    The idea is to search for the file names and load them if they are available…

    Get an eye on the FolderItem entry of the Language Reference for more details.

  5. Ivan T

    Jan 25 Pre-Release Testers

    Use the Documentation

  6. Tim H

    Jan 25 Pre-Release Testers Portland, OR USA

    In general, you'll want to obtain a FolderItem pointing to the folder. There are several ways to do this:
    GetFolderItem - point to a specific path
    SpecialFolder - Start from a system defined location
    SelectFolder - prompt the user for the location

    Then use FolderItem.Child to get a specific file in the folder.

    dim theFolder as Folderitem
    dim theFile as FolderItem
    
    theFolder = SelectFolder
    
    theFile = theFolder.Child("a.config")
    if theFile.Exists then
       // do something
    end if
    
    theFile = theFolder.Child("b.config")
    if theFile.Exists then
       // do something else
    end if
  7. Tim,

    My config files are being saved under C: \Program Files (x86)\MyApp\

    dim theFolder as Folderitem
    dim theFile as FolderItem

    theFolder = SelectFolder <<<<<< Would i change this to SpecialFolder.Applications?

    theFile = theFolder.Child("a.config")
    if theFile.Exists then
    // do something
    end if

    theFile = theFolder.Child("b.config")
    if theFile.Exists then
    // do something else
    end if

  8. Tim H

    Jan 25 Pre-Release Testers Portland, OR USA
    Edited 3 weeks ago

    @Sergio C theFolder = SelectFolder <<<<<< Would i change this to SpecialFolder.Applications?

    Yes. To get a feeling for how this works, start a new project, put a pushbutton on the window and add this to the Action event.

    dim f, f2 as FolderItem
    dim s as string
    
    f = SpecialFolder.Applications
    s = f.NativePath
    break
    f = SpecialFolder.Applications.Child("MyApp")
    s = f.NativePath
    
    f2 = f.Child("a.config")
    s= f2.NativePath
    
    f2 = f.Child("b.config")
    s = f2.NativePath

    Run the project and step through the code, observing how the path changes. I often spin up a throwaway project like this just to test how stuff works.

  9. Paul R

    Jan 26 Pre-Release Testers, Xojo Pro Kirkland, WA

    Here's how I would probably do it:

    // Get the folder containing the .config files
    dim fldr as FolderItem = SpecialFolder.Application.Child("MyApp").Child("ConfigFiles")
    
    // Iterate through all items in the folder
    for i as integer = 1 to fldr.Count
      dim f as FolderItem = fldr.Item(i)
      if f.Name.right(7) = ".config" then
        // It's a .config file (we hope)
        // Remove the .config part
        dim cname as string = f.Name.left(f.Name.Len - 7)
    
        select case cname
        case "a" // Process the "a.config" file. 
          DoSomethingHereWith(f)
    
        case "b" // Process the "b.config" file. 
          DoSomethingElseHereWith(f)
    
        case "init" // Process the "init.config" file. 
          DoSomethingDifferentHereWith(f)
    
        else
          // Oops, there isn't any code to process this .config file
          break
        end select
      end if
    next
  10. OK i attempting to test this scenario with me adding a.config file in "MyApp" folder located in C: \Program Files (x86)\MyApp\

    Im having it Display a msgBox

    But when i run it it wont display anything am i missing something.?

    dim fldr as FolderItem = SpecialFolder.Applications.child("MyApp")

    // Iterate through all items in the folder
    for i as integer = 1 to fldr.Count
    dim f as FolderItem = fldr.Item(i)
    if f.Name.right(7 ) = " .Config" then

    dim cname as string = f.Name.left(f.Name.Len - 7)

    select case cname

    case "a.config" // Process the "a.config" file.

    MsgBox("I Found a file")

    case "b" // Process the "b.config" file.

    MsgBox("I Found b file")

    case "c" // Process the "c.config" file.

    MsgBox("I Found c file")

    else
    // Oops, there isn't any code to process this .config file
    break
    end select
    end if
    next

    close

  11. John M

    Jan 31 Pre-Release Testers, Xojo Pro New York / New Jersey

    @Sergio C for i as integer = 1 to fldr.Count
    dim f as FolderItem = fldr.Item(i)
    if f.Name.right(7 ) = " .Config" then

    dim cname as string = f.Name.left(f.Name.Len - 7)

    select case cname

    case "a.config" // Process the "a.config" file.

    Step through this in the debugger and watch the value of cname. Among other things, you removed the ".config" part of the filename, but your select/case is looking for the name with the .config still on it.

  12. John where did i remove the.config part in the file name?

  13. Tim P

    Jan 31 Pre-Release Testers feedback://46303

    @Sergio C dim cname as string = f.Name.left(f.Name.Len - 7)

  14. 2 weeks ago

    Paul R

    Jan 31 Pre-Release Testers, Xojo Pro Kirkland, WA

    Why don't you simplify it to this:

    dim fldr as FolderItem = SpecialFolder.Applications.child("MyApp")
    
    // Iterate through all items in the folder
    for i as integer = 1 to fldr.Count
      dim f as FolderItem = fldr.Item(i)
      select case f.name
    
      case "a.config" // Process the "a.config" file.
    
        MsgBox("I Found a file")
    
      case "b.Config" // Process the "b.config" file.
    
        MsgBox("I Found b file")
    
      case "c.Config" // Process the "c.config" file.
    
        MsgBox("I Found c file")
    
      end select
    next

or Sign Up to reply!