<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <atom:link href="http://kitchingroup.cheme.cmu.edu/blog/feed/index.xml" rel="self" type="application/rss+xml" />
    <title>The Kitchin Research Group</title>
    <link>https://kitchingroup.cheme.cmu.edu/blog</link>
    <description>Chemical Engineering at Carnegie Mellon University</description>
    <pubDate>Sat, 01 Nov 2025 13:47:46 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    
    <item>
      <title>Finding the box root directory on a local machine</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2013/12/22/Finding-the-box-root-directory-on-a-local-machine</link>
      <pubDate>Sun, 22 Dec 2013 10:26:24 EST</pubDate>
      <category><![CDATA[box]]></category>
      <guid isPermaLink="false">zweGmQmKn8aXANapPQS8IHruMfA=</guid>
      <description>Finding the box root directory on a local machine</description>
      <content:encoded><![CDATA[


&lt;p&gt;
I am working to automate some aspects of box.com, specifically to create collaborations in folders and tasks on files on my local computer at the command-line. I use Box Sync to mirror folders and files on my local computer, and I would like to open a prompt in one of these folders and type something like:
&lt;/p&gt;
&lt;pre class="example"&gt;
box collaborate --role editor someone@gmail.com
&lt;/pre&gt;
&lt;p&gt;
to add that person as an editor to my box folder. 
&lt;/p&gt;

&lt;p&gt;
The challenge is that I need to know the &lt;code&gt;id&lt;/code&gt; of that folder on box. Box stores the files on their server by id, not by name, and the root box folder has an id of &lt;code&gt;0&lt;/code&gt;. On my local computer, the box root folder is where &lt;code&gt;Box Sync&lt;/code&gt; puts my synchronized files. In my box&lt;sub&gt;course&lt;/sub&gt; python module I wrote a function that will return the &lt;code&gt;id&lt;/code&gt; of an item in box given the &lt;i&gt;box path&lt;/i&gt; which is relative to the box root directory. For example, here we can get the &lt;code&gt;id&lt;/code&gt; for a folder called group-course.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;from&lt;/span&gt; box_course &lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; box

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; box.get_item(&lt;span style="color: #228b22;"&gt;'/group-course'&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
{u'sequence_id': u'1', u'etag': u'1', u'type': u'folder', u'id': u'1328895168', u'name': u'group-course'}
&lt;/pre&gt;

&lt;p&gt;
On my local computer, group course is located at C:\Users\jkitchin\Box Sync\group-course, and C:\Users\jkitchin\Box Sync is like the box root directory. So, the challenge is, if I am on the local computer in some directory, how do I determine the box path to that directory? 
&lt;/p&gt;

&lt;p&gt;
What I worked out is to start in the current directory, and check directories above this for a file that indicates you are in the box root directory. With Box Sync 3, that file was "Box Sync ReadMe.pdf", but Box Sync 4 does not include that file anymore. I just put a folder of that name in the Box Sync 4 root directory &lt;sup&gt;&lt;a id="fnr.1" name="fnr.1" class="footref" href="#fn.1"&gt;1&lt;/a&gt;&lt;/sup&gt;.
&lt;/p&gt;

&lt;p&gt;
Here is a way to start in a box directory, and walk up the path to look for the file. We get the path, and then split each directory off the end, checking for the existence of the file, until the path is gone.
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; os
&lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;change into a box directory&lt;/span&gt;
os.chdir(&lt;span style="color: #228b22;"&gt;'C:\Users\jkitchin\Box Sync\group-course'&lt;/span&gt;)

&lt;span style="color: #8b008b;"&gt;wd&lt;/span&gt;, &lt;span style="color: #8b008b;"&gt;last&lt;/span&gt; = os.getcwd(), &lt;span style="color: #cd0000;"&gt;True&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;while&lt;/span&gt; last:
    &lt;span style="color: #8b008b;"&gt;wd&lt;/span&gt;, &lt;span style="color: #8b008b;"&gt;last&lt;/span&gt; = os.path.split(wd)
    
    &lt;span style="color: #8b008b;"&gt;cfile&lt;/span&gt; = os.path.join(wd, &lt;span style="color: #228b22;"&gt;'Box Sync ReadMe.pdf'&lt;/span&gt;)
    &lt;span style="color: #8b0000;"&gt;if&lt;/span&gt; os.path.exists(cfile):
        &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;we found box root&lt;/span&gt;
        &lt;span style="color: #8b0000;"&gt;break&lt;/span&gt;

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; wd
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
C:\Users\jkitchin\Box Sync
&lt;/pre&gt;

&lt;p&gt;
That gets us the box root directory. Now, we need to strip this off of the current working directory. We also need to replace all the backslashes that Windows uses with forward slashes so that we can get the id. 
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; os
os.chdir(&lt;span style="color: #228b22;"&gt;'C:\Users\jkitchin\Box Sync\group-course'&lt;/span&gt;)

&lt;span style="color: #8b008b;"&gt;cwd&lt;/span&gt; = os.getcwd()

&lt;span style="color: #8b008b;"&gt;wd&lt;/span&gt;, &lt;span style="color: #8b008b;"&gt;last&lt;/span&gt; = os.getcwd(), &lt;span style="color: #cd0000;"&gt;True&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;while&lt;/span&gt; last:
    &lt;span style="color: #8b008b;"&gt;wd&lt;/span&gt;, &lt;span style="color: #8b008b;"&gt;last&lt;/span&gt; = os.path.split(wd)
    
    &lt;span style="color: #8b008b;"&gt;cfile&lt;/span&gt; = os.path.join(wd, &lt;span style="color: #228b22;"&gt;'Box Sync ReadMe.pdf'&lt;/span&gt;)
    &lt;span style="color: #8b0000;"&gt;if&lt;/span&gt; os.path.exists(cfile):
        &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;we found box root&lt;/span&gt;
        &lt;span style="color: #8b0000;"&gt;break&lt;/span&gt;

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; wd
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; cwd
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; cwd.replace(wd, &lt;span style="color: #228b22;"&gt;''&lt;/span&gt;).replace(&lt;span style="color: #228b22;"&gt;'\\'&lt;/span&gt;,&lt;span style="color: #228b22;"&gt;'/'&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
C:\Users\jkitchin\Box Sync
C:\Users\jkitchin\Box Sync\group-course
/group-course
&lt;/pre&gt;

&lt;p&gt;
This seems to work pretty well, but on some Windows machines, the drive letter is lower case, and then this does not work. In that case, we use &lt;code&gt;os.path.normcase&lt;/code&gt; to make everything consistent.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; os
os.chdir(&lt;span style="color: #228b22;"&gt;'C:\Users\jkitchin\Box Sync\group-course'&lt;/span&gt;)

&lt;span style="color: #8b0000;"&gt;from&lt;/span&gt; box_course &lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; box

&lt;span style="color: #8b008b;"&gt;cwd&lt;/span&gt; = os.getcwd()

&lt;span style="color: #8b008b;"&gt;wd&lt;/span&gt;, &lt;span style="color: #8b008b;"&gt;last&lt;/span&gt; = os.getcwd(), &lt;span style="color: #cd0000;"&gt;True&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;while&lt;/span&gt; last:
    &lt;span style="color: #8b008b;"&gt;wd&lt;/span&gt;, &lt;span style="color: #8b008b;"&gt;last&lt;/span&gt; = os.path.split(wd)
    
    &lt;span style="color: #8b008b;"&gt;cfile&lt;/span&gt; = os.path.join(wd, &lt;span style="color: #228b22;"&gt;'Box Sync ReadMe.pdf'&lt;/span&gt;)
    &lt;span style="color: #8b0000;"&gt;if&lt;/span&gt; os.path.exists(cfile):
        &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;we found box root&lt;/span&gt;
        &lt;span style="color: #8b0000;"&gt;break&lt;/span&gt;

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; wd
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; cwd
&lt;span style="color: #8b008b;"&gt;bpath&lt;/span&gt; = os.path.normcase(cwd).replace(os.path.normcase(wd), &lt;span style="color: #228b22;"&gt;''&lt;/span&gt;).replace(&lt;span style="color: #228b22;"&gt;'\\'&lt;/span&gt;,&lt;span style="color: #228b22;"&gt;'/'&lt;/span&gt;)

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; bpath
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; box.get_item(bpath)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
C:\Users\jkitchin\Box Sync
C:\Users\jkitchin\Box Sync\group-course
/group-course
{u'sequence_id': u'1', u'etag': u'1', u'type': u'folder', u'id': u'1328895168', u'name': u'group-course'}
&lt;/pre&gt;

&lt;p&gt;
This seems to work so far. Something similar this is probably done in git repositories, to find the .git file. This is also a useful way to find a config file higher up the path.
&lt;/p&gt;
&lt;div id="footnotes"&gt;
&lt;h2 class="footnotes"&gt;Footnotes: &lt;/h2&gt;
&lt;div id="text-footnotes"&gt;

&lt;div class="footdef"&gt;&lt;sup&gt;&lt;a id="fn.1" name="fn.1" class="footnum" href="#fnr.1"&gt;1&lt;/a&gt;&lt;/sup&gt; &lt;p&gt;
Box Sync 4 renames your sync directory from "~/Documents/My Box Files" to "~/Box Sync".
&lt;/p&gt;&lt;/div&gt;


&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Copyright (C) 2013 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;&lt;p&gt;&lt;a href="/org/2013/12/22/Finding-the-box-root-directory-on-a-local-machine.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
