Writing to a file in Dropbox

Hi, guys,

I am trying to write from an XOJO-created application to a file in DropBox. For some reason, the following code does not work


Dim t As BinaryStream
Dim s as String
Dim f as FolderItem
f = Volume (0).Child("Users").Child("valeriykozmenko").Child("Dropbox").Child("LAB SIMULATOR").Child("RESULTS").Child("CURRENT.txt")

If f <> Nil then
  t = BinaryStream.Create(f)
  s = str(Hemoglobin) + EndOfLine
t.Write (s)
t.Close
else
return
end if

Any suggestions what I am doing wrong?

Thank you in advance,

VK

What error do you get?

You need to verify each part of the folderitem. You can replace

Volume (0).Child("Users").Child("valeriykozmenko")

with

Specialfolder.Userhome

I think.

you will also need to make sure the dropbox folder is named what you think it is. I tend to rename my dropbox folder to something other than “Dropbox”.

HI, Beatrix and Scott,

When I removed the “CURRENT.txt” file from the Dropbox folder, the original code worked:

  1. it created a file with the name “CURRENT.txt”
  2. wrote all parameters line by line

This is exactly what I wanted the app to do.

Now the question is how to check if the “CURRENT.txt” file is available (not Nil) and how to delete it.

Thank you,

VK

HI,

The following code deletes the text file


Dim f As FolderItem
f =Specialfolder.Userhome.Child("Dropbox").Child("LAB SIMULATOR").Child("RESULTS").Child("CURRENT.txt")
f.Delete

Even though the original code started working, I have no idea why it was giving me the Nil exception.

the dropbox folder can be found dynamically (at least on macOS and Windows)
on macOS there is in ~/.dropbox/info.json an entry for “path” which is the full path to the dropbox folder
on Windows this isstored in a registry key (I just dont have that handy this moment)
Linux has something similar I’m sure

Maybe Dropbox had it locked. Are you checking for IsWriteable?

https://documentation.xojo.com/index.php/FolderItem.IsWriteable

[quote=438930:@Norman Palardy]the dropbox folder can be found dynamically (at least on macOS and Windows)
on macOS there is in ~/.dropbox/info.json an entry for “path” which is the full path to the dropbox folder
on Windows this isstored in a registry key (I just dont have that handy this moment)
Linux has something similar I’m sure[/quote]

if you are using Dropbox’s client this is correct. If you are using a 3rd party to sync (like ExpanDrive) then it uses a different configuration file. Using a 3rd party client a person might sync more than one Dropbox folders to their system.

I basically dont use dropbox at all so … :stuck_out_tongue:

Either way trying to write to a dropbox folder that is synced seems like its ripe for issues
I’d write it to a temp location then move it to a dropbox folder rather than write one directly since Xojo uses older file system apis that do seem to have issues with dropbox
This is one thing I hope gets fixed for API 2.0 or so

[quote=438937:@Norman Palardy]I basically dont use dropbox at all so … :stuck_out_tongue:

Either way trying to write to a dropbox folder that is synced seems like its ripe for issues
I’d write it to a temp location then move it to a dropbox folder rather than write one directly since Xojo uses older file system apis that do seem to have issues with dropbox
This is one thing I hope gets fixed for API 2.0 or so[/quote]

I have found that if you are writing a file in a single pass (open file, write all the data, close file) it seems to work fine. but when you try to do the writes in multiple passes then it breaks down.

and yes I am hoping API 2.0 will make it much better.

yeah I expect multiple passes would have issue since dropbox may start syncing and then you’re locked out of the file

its one reason I’d avoid using drop box and copy projects in and out of it and not work on them IN drop box

and compilation fails for similar reasons to this one

[quote=438948:@Norman Palardy]yeah I expect multiple passes would have issue since dropbox may start syncing and then you’re locked out of the file

its one reason I’d avoid using drop box and copy projects in and out of it and not work on them IN drop box

and compilation fails for similar reasons to this one[/quote]

Same with Google Drive. Every time you do a write, the sync tries to happen. At some point, you will collide.

I would expect most cloud file systems like this will have that issue
hence why I would write to a temporary location then move the file to the cloud folder rather than try & write it there directly

Is there a way to tell if you are on a cloud file location like DropBox?

short answer is no.

for the O/S it is just another folder/directory on the filesystem but not a unique filesystem of itself. then the “app” (aka Dropbox in our example here) treats that folder like a filesystem and does its syncing magic on it. if you would stop dropbox app or even remove it, the folder/directory will still be there will all the data in synch up to the point you stopped the app/removed the app.

if things like Dropbox would create a new/unique filesystem for themselves, it would appear under /Volumes/Dropbox (or something similar) on MacOSX and as a drive letter on Windows (yes I know the newer Windows can do mount points but not all the versions of Windows that Dropbox does).

this works the same way for Dropbox, iCloud, OneDrive, OneDrive for Business (yes they are separate products), Box, and the rest.