Create a Folder

I like to create a new folder, in to the current folder, without a dialog.
After that will create a new database in this new folder, also without a dialog.
It should be work on the Windows and Mac platform.
How to do??

Quick and dirty without error checking:

[code]dim f as FolderItem
f = SpecialFolder.Desktop.Child(“my new folder”)
f.CreateAsFolder

dim db as new SQLiteDatabase
db.DatabaseFile = f.Child(“my_new_database_file.sqlite”)

if db.CreateDatabaseFile then

'success

else

'failed to create

end if[/code]

OK it’s super, but it’s created in the Desktop.
I like it in the current folder (directory) is dat also posible?

You’ll obviously have to adapt the example to whatever you want to do. Instead of the desktop, give it a folderitem reference to wherever you want it to go.

[quote]I like to create a new folder, in to the current folder, without a dialog.
After that will create a new database in this new folder, also without a dialog.[/quote]

This is a bad idea.
On windows, it will try to create a folder in the same place as the exe
This is the Program Files folder and Windows will not allow you to do that.

Use the SpecialFolder.ApplicationData for this purpose.

[code]dim f as FolderItem
f = SpecialFolder.ApplicationData.Child(“my app name”)
if not f.exists then
f.CreateAsFolder
end if

dim dbf as FolderItem
dim db as new SQLiteDatabase

dbf = f.Child(“my_new_database_file.sqlite”)

if not dbf.exists then
db.DatabaseFile = f.Child(“my_new_database_file.sqlite”)

if db.CreateDatabaseFile then
'success
else
'failed to create
end if
end if
[/code]

But my program is by windows in a different folder as by Mac.
I like to use a sub directory Like “Data” on the current folder, and that is never a applcation folder.
The routine from “Jeff Tullin” works wihtout a error but it doesent make a new folder, also not in the application folder.
The statemant “currentfolder” doesent works.
Waths the next?

Where did you look?

I have look every where under “This Mac” also inthe aplication folder, no folder and no file.
This is my code:

[quote] dim f as FolderItem
f = SpecialFolder.ApplicationData.Child(“Test”)
if not f.exists then
f.CreateAsFolder
end if
dim dbf as FolderItem
dim db as new SQLiteDatabase
dim sql as String
dbf = f.Child(“TestFile.sqlite”)
if not dbf.exists then
db.DatabaseFile = f.Child(“TestFile.sqlite”)
if db.CreateDatabaseFile then
Dbf=GetFolderItem(“TestFile.sqlite”)
Db=New SQLiteDatabase
Db.DatabaseFile=Dbf
if Db.CreateDatabaseFile Then
sql=“CREATE TABLE Team (ID INTEGER PRIMARY KEY, Number INTEGER)”
Db.SQLExecute(SQL)
db.Close
MsgBox “OK”
else
MsgBox “failed to create”
end
end if
end if[/quote]

Hans,

sometimes, the language reference is your best friend. FolderItem:

Dim f as FolderItem f= GetFolderItem("MyTemplates")

Use “Data” instead of “MyTemplates” and you’re done.

That said, what previous answers hint have to be taken into consideration: things nowadays are a bit iferent from what they were in the past. As an example, and if your application is not located in OS X Applications folder, press the F4 key and search for your application…

Maybe not everywhere…

In Finder, click on the GO menu
While the menu is dropped down, press the ALT key
You will see a new entry called Library appear.
Select that.
In Library, open ‘Application Support
In there, look for the test folder you will have created using the code above.

Apple wants application support files hidden from ‘joe public’

A similar folder exists in LocalRoaming/AppData on Windows… also usually hidden from the general public, but writable and available for your application.

you can prove it in code too…
After the code above, try this

if SpecialFolder.ApplicationData.Child("Test").exists then
msgbox "The folder is there"
end if

if SpecialFolder.ApplicationData.Child("Test").Child("TestFile.sqlite").exists then
msgbox "The file is there also"
end if

Hai Emile,
OK, it’s works.
I give it the name “2014”, but now i like to create the next folder into “2014” it should be named “Week 36”.
And lateron this to folder names should be created automatic form the actual Date in to a variable (no problem) and this variable should be involved in to the statement GetfolderItem.(Problem)

Hi Emile,
I have found the aplcation folder and also the file, but that is for me not the real place, i must be make the program for a windows platform and i wil used it on my Mac, so i like to have somwere a folder for the program and when the program is running, it should be write automaticaly the sub folders Year and Week and in that sub sub folder my DataBase.

[quote=216040:@Hans Riemers]OK, it’s works.
I give it the name “2014”, but now i like to create the next folder into “2014” it should be named “Week 36”.
And lateron this to folder names should be created automatic form the actual Date in to a variable (no problem) and this variable should be involved in to the statement GetfolderItem.(Problem)[/quote]
Works: Goood.

Child folder into the 2014 folder ? No problem: use FolderItem.CreateAsFolder

say f is a reference for the 2014 folder:

[code]Dim g As Folder

g = f.Child(“Week 36”) // Define a Child in g
g.CreateAsFolder // Create the Child folder[/code]

Check what I wrote above (not tested); I am too deep intow hat I’m doing and so I may be wrong, but the idea is there.

You know what to do for subsequent nested folders.

Also, do not forget the error checkings.

And while I am at it, if this is something that you will have to do many times (as Year, Week names implies), be careful. After doing some working method, re-think the whole as a multi purpose (everyday use) Method: set (eventually) the reference to the main folder (beside your application) as property and think at how to do the followings in a fail free (as much possible) way.

Thanks