How to look for 8 different files

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.

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.

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

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.

Use the Documentation

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

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

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.

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

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

[quote=423166:@Sergio Campos]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.

[/quote]

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.

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

Why don’t you simplify it to this:

[code]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
[/code]