Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get path to dropbox folder on different computer

Posted on 2013-11-12
12
Medium Priority
?
2,219 Views
Last Modified: 2013-11-14
I am looking for a way to programmatically find the path of the "Dropbox" folder within a PC.

I ran accross this code for C+, but don't know how to convert it to VBA, if that is possible at all.

private static string GetDropBoxPath()
    {
        var appDataPath = Environment.GetFolderPath(
                                           Environment.SpecialFolder.ApplicationData);
        var dbPath = Path.Combine(appDataPath, "Dropbox\\host.db");

        if (!File.Exists(dbPath))
            return null;

        var lines = File.ReadAllLines(dbPath);
        var dbBase64Text = Convert.FromBase64String(lines[1]);
        var folderPath = Encoding.ASCII.GetString(dbBase64Text);

        return folderPath;
    }
0
Comment
Question by:jb702
  • 5
  • 5
  • 2
12 Comments
 
LVL 85
ID: 39644668
That code looks for the path to the "ApplicationData" folder on the machine, and then combines that with "Dropbox\\host.db", which apparently points to the Dropbox folder.

You can use the Environ command to get the AppData path:

Dim sAppData As String
sAppData = Environ("AppData") & "\DropBox\host.db"

"host.db" is an SQLite database, so you'd have to be able to read that file. You'd need the SQLite drivers installed on the remote machine. Also, if the user left the DropBox folder at the "default" location, you couldn't read from host.db - it would be in <users>\My Documents\DropBox.
0
 
LVL 20

Accepted Solution

by:
ltlbearand3 earned 2000 total points
ID: 39644902
I don't use drop box, but your code appears to be reading the db file just by decoding the data using base64 decoding (and I see that elsewhere on the web).  I have taken the concepts above and converted them to VBA.  I used the FileSystemObject but there are other ways to access the data, if you like.  I also used XML to decode the base 64.  To use in VBA, you will need to add references to
Microsoft XML, v6.0
Microsoft Scripting RunTime

As LMSConsulting stated, you are assuming a path to the file that does not appear to be the default.  You may have to update that in the code.

' ExpertExchange Question ID28292626
' http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_28292626.html
' Expert: ltlbearand3 [http://www.experts-exchange.com/M_2469312.html]
'
' Requires the following references:
'   MICROSOFT SCRIPTING RUNTIME
'   MICROSOFT XML, V6.0

Public Sub Test()
    Debug.Print GetDropBoxPath
End Sub

Private Function GetDropBoxPath() As String
    
    On Error GoTo GetDropBoxPath_Error:
    
    ' Need a reference to Microsoft Scripting RunTime
    Dim objFSO As Scripting.FileSystemObject
    Dim objDropBox As Scripting.TextStream
    Dim strDropBoxFolder As String
    Dim strData() As String
    
    Set objFSO = New Scripting.FileSystemObject
    
    strDropBoxFolder = Environ("AppData") & "\DropBox\host.db"
    
    If objFSO.FileExists(strDropBoxFolder) Then
        Set objDropBox = objFSO.OpenTextFile(strDropBoxFolder, ForReading)
        objDropBox.SkipLine
        GetDropBoxPath = Base64Decode(objDropBox.ReadLine)
    End If
    
Release:
    On Error Resume Next
    Set objDropBox = Nothing
    Set objFSO = Nothing
    Exit Function

GetDropBoxPath_Error:

    Resume Release
    MsgBox "Procedure = GetDropBoxPath" & vbNewLine & _
        "Error Number = " & Err.Number & vbNewLine & _
        "Error Message = " & Err.Description & vbNewLine, _
        vbCritical Or vbSystemModal, "VBA Error"

End Function


Private Function Base64Decode(ByVal EncodedValue As String) As String

    On Error GoTo Base64Decode_Error:

    ' Uses XML to decode the Base 64 string
    ' Requires Reference to Microsoft XML, v6.0 or higher
    Dim objXMLDoc As MSXML2.DOMDocument60
    Dim objNode As MSXML2.IXMLDOMElement

    Set objXMLDoc = New MSXML2.DOMDocument60
    Set objNode = objXMLDoc.createElement("Base64")
    objNode.DataType = "bin.base64"
    objNode.Text = EncodedValue
    
    ' Convert byte array to string and return the string
    Base64Decode = StrConv(objNode.nodeTypedValue, vbUnicode)

Release:
    Set objNode = Nothing
    Set objXMLDoc = Nothing
    
    Exit Function

Base64Decode_Error:
    MsgBox "Procedure = Base64Decode" & vbNewLine & _
        "Error Number = " & Err.Number & vbNewLine & _
        "Error Message = " & Err.Description & vbNewLine, _
        vbCritical Or vbSystemModal, "VBA Error"
        
    Resume Release
End Function

Open in new window

-Bear
0
 

Author Closing Comment

by:jb702
ID: 39645088
Wow, that is fricken awesome. Thank you. i could imagine i could use this function for others means as well. thank you so much.
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 85
ID: 39645208
I tried the code on my machine (with DropBox installed, and with a non-default DropBox folder defined) and it errored out. Did it work for you? Just curious really ...
0
 

Author Comment

by:jb702
ID: 39645237
Yeah, i think mine is in the default location.

"C:\Users\Jbryan\Desktop\Dropbox\"

Was this where yours was at?

I have a users  machine where it is located at

"C:\Users\Joe\Dropbox\"

Hmmm.
0
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 39646654
Glad it worked for you.

@LMSConsulting - Since I don't have dropbox, I can't test this too well.  I know you are just curious and if you want to pursue getting this work, we can look at it - to make the code better.  If we pursue, I am wondering where it errors at.  I know the Environ("AppData") does not always pull in values on some windows boxes, but it works most of the time and it simpler code.  I wonder if that is returning a blank value and therefore an invalid path.
0
 
LVL 85
ID: 39646703
"C:\Users\Jbryan\Desktop\Dropbox\"

I believe that is the default.

I am wondering where it errors at

Right here:

Base64Decode = StrConv(objNode.nodeTypedValue, vbUnicode)

When that line is run, the value of objNode.nodeTypedValue results in "The parameter is incorrect"
0
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 39646755
Hmm.  I am able to test that part of the code and it works.  What version of Microsoft XML is referenced in the project?  I was running in Excel 2010 to use that VBA editor.  What are you testing the code in?  

If you add this line of code in sub test, does it work?  [Should put ThisIsATest in the immediate window.
    Debug.Print Base64Decode("VGhpc0lzQVRlc3Q=")

Open in new window


-Bear
0
 
LVL 85
ID: 39647516
XML 6.0, and I'm running this in Access 2010.

If I run that line in the Immediate window, I get "ThisIsATest", so it would seem your Base64Decode routine functions as expected.
0
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 39647859
It makes me wonder if your host.db is not base64 encoded.  Everything I read is that file is base64 encoded.  I wonder if you have a different db version and if maybe it has changed at some point.
0
 
LVL 85
ID: 39648298
I just installed it a few days ago (I don't really use DropBox), so perhaps it is outdated?
0
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 39650065
I don't know - or maybe they updated it.  Since I don't use it, I don't really know.  I am fairly certain it has something to do with host.db either being corrupt or encoded in a different manner.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question