CreateAsFolder doesn't

There must be something real simple that I am missing here.

The situation: I have a pinewood derby race program that I am adding the ability to have pictures of the racers. The ability of drag/dropping the pictures and saving them in an array is already working. When you run the program the pictures are stored in ThePic(n) where n is the racer number.

What I’m trying to do is to specify where to create a folder that will hold the pictures and then iterate through ThePic(n) array and save each picture with the racer number and the racer’s name as a jpg file.

The only example of saving a picture as a jpg that I’ve found calls for loading the picture into an ImageWell and then saving the picture out of that image well.

If there is a way to save the picture directly from my picture array that would be better.

But for now I’m having trouble creating the folder that is going to hold the pictures.

[code] Dim f,SaveFolder As FolderItem
Dim FileName1,FileName2 as string
Dim i as integer

If ImageWell1.Image <> Nil Then
FileName1 = RaceSponsor
SaveFolder = GetSaveFolderItem("",FileName1) 'this gets the name of the scout pack to use as the filename that will hold the racers names,times,and division info as text
SaveFolder = SaveFolder.child(FileName1+"-pics") ’ I want to use the name of the scout pack + “-pics” as the name of the folder where the pictures will be saved
SaveFolder.createAsFolder ’ this SHOULD create that new folder …
For i = 1 to 80 'now iterate through the picture array
if HasPic(i) = true then ’ test this boolean to see if that racer has a picture available to be saved
f = getFolderItem(str(racerButton)+ RacerName(i)+".jpg") 'if a picture is available name it with the racerNumber (aka RacerButton) then add the racer’s name
ImageWell1.Image = ThePic(i) 'put the picture to be saved in the imagewell

    If Picture.IsExportFormatSupported(Picture.FormatJPEG) Then
      
      // Save the image out to the file
      ImageWell1.Image.Save(f, Picture.SaveAsJPEG) ' ... so that the picture can be saved as a jpg file
    End If
  end if
next i

End If[/code]

In the line that says SaveFolder.creatAsFolder I get a NilObjectException. I’m confused … isn’t that line supposed to CREATE a new object ( a folder ) so that the object is NOT Nil?

SaveFolder = GetSaveFolderItem("",FileName1)

Did you check that this did in fact return a non-NIL filename? and that the filename is a valid format for the OS?

    SaveFolder = SaveFolder.child(FileName1+"-pics") 

Did you check that this did in fact return a non-NIL filename? and if so, that the folder does not ALREADY exist?

Never trust the user to provide appropriate information, always verify that it meets the required criteria.
My gut instinct tells me that one or both of those above statements is not doing what you are assuming it is doing.

The problem is, you are presenting a dialog that allows a user to select a file, not a folder, so the result is FolderItem that represents a file. At that point, it either exists as a file or doesn’t exist at all, but in either case, you cannot use Child on it.

Instead of GetSaveFolderItem, try SelectFolder which will let the user choose or create a folder. After that, what you’re doing should work. Just remember that you will get Nil if the user cancels the dialog.

As for saving the picture directly, see:

http://documentation.xojo.com/index.php/Picture.Save

Hmmmm I think I see what you are saying … but let me explain further …

My intention is to:

Open a text file
save the racer names as text
save the racer division as text
save the racer’s times as text
close the text file
Create a new folder (inside the same folder where the text file was saved) with the same name as the text file + “-pics”
inside the “pics” folder save a series of jpg pictures with each named with the racer’s number + the racer’s name

So assuming that the text file has been created, saved, and closed …

  How do I take the string value of the previous text file name, append it with "-pics"
  Create the new "pics" folder
  then save the individual pictures in that "pics" folder

From what Kem said I’m not sure how I SelectFolder if I have already saved my text file in the folder where I want to create the “pics” folder.

Do I somehow ask the system which folder I currently has selected (where I have already saved the text file) and THEN specify that I want to create my new “pics” folder inside of there?

And just to complicate matters even further … The absolute ideal situation would be to somehow - before closing the text file - be able to save those pictures WITHIN the text file. But I don’t know if it’s even possible to save “pictures” inside a text file. That’s why I was going in the direction of saving the pictures separately.

As for the link to Picture.Save … That’s where I got the “save it from a image well” concept … which I already have working. Saving the picture is already working. It’s the “create a new folder to save the pictures in” that is giving me trouble.

[quote]save the racer names as text
save the racer division as text
save the racer’s times as text[/quote]
If ever a piece of code idea screamed “DATABASE” this is it. But let’s move on for now.

Then you already have a folder item to the folder you need if you just saved to there. You don’t need to ask the user for anything

dim f As folderitem = prevFolder.Child(prevFolder.Name + "-pics") if f <> Nil and not f.Exists Then f.CreateAsFolder End if
Now save your pics to f and you’re good to go.

Or if you have the FolderItem that points to the file and want to create a new folder next to it, use Parent.

dim parentFolder as FolderItem = file.Parent
dim picsFolder as FolderItem = parentFolder.Child( file.Name + "-pics" )
picsFolder.CreateAsFolder

And

ImageWell1.Image = ThePic(i) 'put the picture to be saved in the imagewell
ImageWell1.Image.Save(f, Picture.SaveAsJPEG) ' ... so that the picture can be saved as a jpg file

is equivalent to

ThePic(i).Save(f, Picture.SaveAsJPEG)