I use the same config file between my windows-linux-mac machines. But I'm having some trouble setting some org folders because of the different definition of paths per OS.

For instance setting my default notes folder for windows:

  ((string-equal system-type "windows-nt")
    (setq org-default-notes-file (concat org-directory "/notes.org"))

is giving me "D:\\my_data/notes.org"

(setq feeds-file (expand-file-name "notes.org" org-directory ))

is giving me "D:/my_data/notes.org"

As you can see, none is a working path in windows (or even in linux). How can I define a variable pointing to a path and concatenate any file I need taking Operative System into account?


The setting

(setq feeds-file (expand-file-name "notes.org" org-directory ))

is perfect for emacs internal usage. It gives "D:/my_data/notes.org" with forward-slashes.

The relevant section of the emacs manual is G.3 File Names on MS-Windows. It says:

MS-Windows and MS-DOS normally use a backslash, ‘\’, to separate name units within a file name, instead of the slash used on other systems. Emacs on MS-DOS/MS-Windows permits use of either slash or backslash, and also knows about drive letters in file names.

There is another section on file names in the elisp manual:

On MS-DOS and MS-Windows, these functions (like the function that actually operate on files) accept MS-DOS or MS-Windows file-name syntax, where backslashes separate the components, as well as POSIX syntax; but they always return POSIX syntax. This enables Lisp programs to specify file names in POSIX syntax and work properly on all systems without change.

If you really need to convert the standard file name into the operating system dependent format you can use convert-standard-filename on file names in standard format.

If you need the windows separator ?\\ instead of / you can replace it by

(subst-char-in-string ?/ ?\\ standard-path)

where standard-path is the file name with / as path component separator.

Also be aware of the footnote in the Section "File Names" of the elisp manual that mentions the two functions cygwin-convert-file-name-from-windows and cygwin-convert-file-name-to-windows for converting file names under cygwin.

[1] In MS-Windows versions of Emacs compiled for the Cygwin environment, you can use the functions cygwin-convert-file-name-to-windows and cygwin-convert-file-name-from-windows to convert between the two file-name syntaxes.

  • Maybe for emacs internals is ok but the init part for windows is not working. The path is not recognized and any operation gives a message: wrong type argument. Will try with convert-standard-filename. Thanks – Forge Aug 17 '18 at 6:19
  • @Forge I added the citate of another section of the elisp manual that says that all emacs functions returning paths use slash as path component separator in the return value. I think you have to replace the slash for a backslash yourself if you need the backslash for stuff like call-process. I've added an example call of subst-char-in-string for that purpose in the answer. – Tobias Aug 17 '18 at 7:45

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.