Re: how to put a single backslash into a string, to make file paths usable on windows

Rui Barradas via R-help <[email protected]> Fri, 8 May 2026 07:23:01 +0100
Newsgroups gmane.comp.lang.r.general
Message-ID <[email protected]>
Hello,

Inline.

Às 03:53 de 08/05/2026, Spencer Graves escreveu:
> See also:
> 
> 
> file.path(..., fsep = .Platform$file.sep)

On Windows, .Platform$file.sep outputs "/".

> 
> 
> https://web.mit.edu/~r/current/lib/R/library/base/html/file.path.html


# on Windows 11 this outputs "/"
.Platform$file.sep
# [1] "/"

file.path(R.home(), "path", "to", "file", fsep = .Platform$file.sep)
# [1] "C:/PROGRA~1/R/R-4.6.0/path/to/file"

# warning, file cannot be found (it doesn't exist)
# but now fsep is "\\"
file.path(R.home(), "path", "to", "file", fsep = .Platform$file.sep) |> 
normalizePath()
# [1] "C:\\PROGRA~1\\R\\R-4.6.0\\path\\to\\file"

# no warning, fsep is "\\", ?shortPathName is meant for DOS/Windows 
8-dot-3 file names
file.path(R.home(), "path", "to", "file", fsep = .Platform$file.sep) |> 
shortPathName()
# [1] "C:\\PROGRA~1\\R\\R-4.6.0\\path\\to\\file"

sessionInfo()
# R version 4.6.0 (2026-04-24 ucrt)
# Platform: x86_64-w64-mingw32/x64
# Running under: Windows 11 x64 (build 22631)
#
# Matrix products: default
#   LAPACK version 3.12.1
#
# locale:
# [1] LC_COLLATE=Portuguese_Portugal.utf8 
LC_CTYPE=Portuguese_Portugal.utf8
# [3] LC_MONETARY=Portuguese_Portugal.utf8 LC_NUMERIC=C 

# [5] LC_TIME=Portuguese_Portugal.utf8
#
# time zone: Europe/Lisbon
# tzcode source: internal
#
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base
#
# loaded via a namespace (and not attached):
# [1] compiler_4.6.0
#


> 
> 
> I'm not sure, but help(".Platform") seems to support what I get from 
> Richard O'Keefe, that "you can safely use / in file name" perhaps no 
> matter which platform. At one point that was not true, but it may be the 
> case now. 

Yes, that's true for a (long) while, now.

[I am not sure about DOS 2, though. I once programmed a GW-BASIC uniroot 
type of equations solver on DOS 2.(*) and called it EQ.POLY.BAS. The 
result was to see DIR list it as EQ<TAB>POL but GW-BASIC only accepted 
its real name. (*) I don't remember the sub-version number.]


Hope this helps,

Rui Barradas


Sorry: I cannot easily check. Spencer Graves
> 
> 
> On 5/7/26 21:30, Richard O'Keefe wrote:
>> This isn't an R thing.  The Windows system calls that accept file names
>> accept both kinds of slash indifferently.  I've seen it said that this 
>> goes
>> back to MS-DOS 2, but certainly it's all NT versions.  If you need to 
>> link
>> C and/or Fortran with R, you can safely use / in file name strings in 
>> those
>> languages too.
>>
>> On Fri, 8 May 2026 at 01:18, Chris Ryan <[email protected]> 
>> wrote:
>>
>>> It had been about a year since I had worked in Windows, and apparently I
>>> had gotten rusty. Forward slashes in path names, as in
>>>
>>> read.csv("e:/path/to/datafileOnWindows.csv")
>>>
>>> work just fine in R on Windows.
>>>
>>> Sorry for the bother.
>>>
>>> --Chris Ryan
>>>
>>>
>>>
>>> Chris Ryan wrote:
>>>> I'm trying to generalize one of my scripts so that it will run on my
>>>> Linux Mint computer at home and my client's Windows 11 computer at 
>>>> work.
>>>>   I'm encountering trouble when setting file paths, conditional on 
>>>> which
>>>> computer the script is running on. It's the Windows backslash issue
>>>>
>>>
>>> ______________________________________________
>>> [email protected] mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> https://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>     [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [email protected] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide https://www.R-project.org/posting- 
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting- 
> guide.html
> and provide commented, minimal, self-contained, reproducible code.