FolderItem.CreatAsFolder not working

So, trying to switch from using the Shell to create a folder on the LAN share to using the Xojo way, and it’s not working.

In a gist:

[code] dim PATH3 as string

PATH3 ="\\ANDC1WAPP0050.DS.SJHS.COM\source$\software" + VENDOR_NAME + “” + PRODUCT_NAME + “” + VERSION_NUMBER + “\1_Orig_media”

dim O_M as FolderItem
O_M = GetFolderItem(PATH3)
O_M.CreateAsFolder
[/code]

I have tried this basic step a variety of different ways with no luck, including tagging on the “PathTypeShell” and “PathTypeNative”.

Any ideas?

FYI…

2016 Rel 3.

So it’s still CreateAsFolder

Have you read the documentation ? FolderItem.CreateAsFolder

It says:

This item was deprecated in version 2019r2.1. Please use FolderItem.CreateFolder as a replacement.

So, it works now but for how many times ?

And FolderItem.CreateFolder is correct now and for some years (check in 19r3.1 for example).

If you use deprecated stuff, you will forget it is deprecated in some times and will be shocked when it will be removed (and that comes at a time where you will be in a hurry: bad times). So, please, do not use deprecated stuff.

Or live dangerously :wink:

CreateAsFolder and CreateFolder both seem to call the same underlying framework code from what I can tell

The difference is CreateFolder may throw exceptions where CreateAsFolder would require you to check for existence and any error codes
Both will still work and will continue to do so until such time CreateAsFolder is removed at which time it can easily be created as an extension method that behaves the same as the existing version

[quote=472137:@Emile Schwarz]Have you read the documentation ? FolderItem.CreateAsFolder

It says:

This item was deprecated in version 2019r2.1. Please use FolderItem.CreateFolder as a replacement.

So, it works now but for how many times ?

And FolderItem.CreateFolder is correct now and for some years (check in 19r3.1 for example).

If you use deprecated stuff, you will forget it is deprecated in some times and will be shocked when it will be removed (and that comes at a time where you will be in a hurry: bad times). So, please, do not use deprecated stuff.

Or live dangerously ;)[/quote]

As I mentioned right below my first post…I’m using 2016 Rel 3.

What exactly happens if you try? CreateAsFolder silently fails, i.e. no folder is created? Did you check whether the parent of your FolderItem actually exists so a new folder can be created there?

Also the true Xojo way would be to use FolderItem.Child to drill down to the desired directory, rather than specifying a path – if anything goes wrong along the way it is much easier to detect.

[quote=472153:@Michael Hußmann]What exactly happens if you try? CreateAsFolder silently fails, i.e. no folder is created? Did you check whether the parent of your FolderItem actually exists so a new folder can be created there?

Also the true Xojo way would be to use FolderItem.Child to drill down to the desired directory, rather than specifying a path – if anything goes wrong along the way it is much easier to detect.[/quote]

Silent, no folder is created.

Nothing after “\\ANDC1WAPP0050.DS.SJHS.COM\source$\software” exists yet. I’m trying to create the whole rest of the path. I currently have it with one simple MKDIR Shell command, but wanted to go the native XOJO way.

So, should I set the FolderItem to “”\\ANDC1WAPP0050.DS.SJHS.COM\source$\software", then use multiple .childs to create each folder along the way?

Yes.
Xojo won’t create the nested folders ‘in one go’

[quote=472158:@Jeff Tullin]Yes.
Xojo won’t create the nested folders ‘in one go’[/quote]

So this works:

[code] PATH3 ="\\ANDC1WAPP0050.DS.SJHS.COM\source$\software"

dim OM as FolderItem
OM = GetFolderItem(PATH3)
OM=OM.child(VENDOR_NAME)
OM.CreateAsFolder
OM=OM.child(PRODUCT_NAME)
OM.CreateAsFolder
OM=OM.child(VERSION_NUMBER)
OM.CreateAsFolder[/code]

But is that the cleanest way to do it? Seems very inelegant compared to a quick one line Shell Command.

More or less it is. You should rather check for subfolder existence and only create if necessary of course.

As Michael stated, this way you can know exactly where things go wrong. It is no big deal to design an extends method that does this with a 1-liner too.
And nobody said that mkdir works differently under the hood. Or that you couldn‘t use Xojo‘s shell for a more comfortable shell command…

You could write a method CreateFoldersFromPath taking a path as its argument. You would use the Split function to split it into a chain of subfolders, then write a For loop through the resulting array to create successive children of the root directory. If any of those folders doesn’t yet exist you create it with CreateAsFolder.

That sounds even worse.

I’m fine with the syntax I have.

Thanks everyone!

It’s a general solution and thus quite elegant. You would call that method any time you needed to create a bunch of nested folders, in any project now or in the future.

I had ten minutes to try ¯\(?)

[code]Private Function CreatePath(sPath as String) as FolderItem
// This function only designed to handle a path that starts with “\”
if sPath.Left(2) <> “\” then
dim ex as new UnsupportedOperationException
ex.Message = "This function is not able to resolve the path " + sPath

raise ex

end

dim arsPath() as String = sPath.Split("")
dim fBase as FolderItem

for i as Integer = 0 to arsPath.Ubound
dim sThis as String = arsPath(i)

// Bail if empty, which occurs the prefix and suffixes
if sThis = "" then continue for i

if fBase = nil then
  // Haven't got the base item with \\\\ yet
  fBase = GetFolderItem("\\\" + sThis)
  
else
  // Get the child
  fBase = fBase.Child(sThis)
  
end

// Nothing to do with a nil item.
if fBase = nil then continue for i

// Create the folder
if fBase.Exists = false then
  fBase.CreateAsFolder
  
end

// We've got a problem
if fBase.LastErrorCode <> 0 then
  break
  
end

next i

// fBase will be nil if something went wrong,
// use this to test the success of the function
return fBase
End Function
[/code]