I'm using spacemacs and currently I'm using the following in my user-config to switch to the scratch buffer when emacs starts:

(setq inhibit-splash-screen t)
(switch-to-buffer "*scratch*")

However, I want to adapt that code to open an org file instead. The issue is that I run the same config on 3 different computers and the location of the file is different for each:

  • ~/Dropbox/myfile.org
  • C:\Dropbox\myfile.org
  • D:\Dropbox\myfile.org

How do I add some condition checking so that:

  1. If I'm on OSX or Linux, load the org file from ~/Dropbox/myfile.org
  2. If I'm on Windows and the file exists, load it from D:\Dropbox\myfile.org
  3. If I'm on Windows and #2 doesn't exist, load it from C:\Dropbox\myfile.org

My issue is differentiating between case 2 and 3. Case 1 can be handled by checking system-type, but on my desktop I have multiple hard drives so I keep Dropbox on D:\, but on my laptop I only have 1 SSD so Dropbox is on C:\, and need a way to figure out the correct path to the file on both Windows systems

  • Try file-exists-p.
    – Dan
    Apr 20 '17 at 2:15
  • 1
    Alternatively, create a symbolic link to the Dropbox directory on Windows. Apr 20 '17 at 6:42
  • Instead of comparing system-type you could use system-name. Apr 20 '17 at 9:52

You can check if a file exists with file-exists-p. From its documentation:

Return t if file FILENAME exists (whether or not you can read it.)

Note that this won't throw an error if you're, say, on Windows and trying to read ~/myfile.org. So we can just loop over all the possible files and take the first one that exists:

(loop for filename in '("~/Dropbox/myfile.org" "C:\\Dropbox\\myfile.org" "D:\\Dropbox\\myfile.org")
      when (file-exists-p filename) collect filename into valid-files
      finally return (car valid-files))

This will return the first file that exists, which you can assign to a variable or open directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.