folders of path - platform independent (repost)

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

folders of path - platform independent (repost)

Ben quant
Hello, (sorry re-posting due to typo)

I'm attempting to get the folders of a path in a robust way (platform
independent, format independent). It has to run on Windows and Linux and
tolerate different formats.

For these: (The paths don't actually exist in Linux but you get the idea.)

Windows:
file_full_path = "C://Program Files//R//R-2.13.1//NEWS.pdf"
file_full_path = "C:\Program Files\R\R-2.13.1\NEWS.pdf"
Linux:
file_full_path = "~/Program FilesR/R-2.13.1/NEWS.pdf"
file_full_path = "/home/username/Program FilesR/R-2.13.1/NEWS.pdf"

I would get for Windows: "C", "Program Files", "R", "R-2.13.1","NEWS.pdf"
I would get for Linux: "home","username", "Program Files", "R",
"R-2.13.1","NEWS.pdf"
(The drive and/or home/username aren't necessary, but would be nice to
have. Also, that file name isn't necessary, but would be nice.)

Thank you for your help,

Ben

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: folders of path - platform independent (repost)

Duncan Murdoch-2
On 11-12-28 4:30 PM, Ben quant wrote:

> Hello, (sorry re-posting due to typo)
>
> I'm attempting to get the folders of a path in a robust way (platform
> independent, format independent). It has to run on Windows and Linux and
> tolerate different formats.
>
> For these: (The paths don't actually exist in Linux but you get the idea.)
>
> Windows:
> file_full_path = "C://Program Files//R//R-2.13.1//NEWS.pdf"
> file_full_path = "C:\Program Files\R\R-2.13.1\NEWS.pdf"
> Linux:
> file_full_path = "~/Program FilesR/R-2.13.1/NEWS.pdf"
> file_full_path = "/home/username/Program FilesR/R-2.13.1/NEWS.pdf"
>
> I would get for Windows: "C", "Program Files", "R", "R-2.13.1","NEWS.pdf"
> I would get for Linux: "home","username", "Program Files", "R",
> "R-2.13.1","NEWS.pdf"
> (The drive and/or home/username aren't necessary, but would be nice to
> have. Also, that file name isn't necessary, but would be nice.)
>
> Thank you for your help,
>

If you use the normalizePath() function with winslash="/", then all
current platforms will return a path using "/" as the separator, so you
could do something like

strsplit(normalizePath(filename, winslash="/"), "/")[[1]]

You need to be careful with normalizePath:  at least on Windows, it will
not necessarily do what you wanted if the filename doesn't exist.

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: folders of path - platform independent (repost)

Ben quant
Excellent!

Thanks,

ben

On Wed, Dec 28, 2011 at 2:37 PM, Duncan Murdoch <[hidden email]>wrote:

> On 11-12-28 4:30 PM, Ben quant wrote:
>
>> Hello, (sorry re-posting due to typo)
>>
>> I'm attempting to get the folders of a path in a robust way (platform
>> independent, format independent). It has to run on Windows and Linux and
>> tolerate different formats.
>>
>> For these: (The paths don't actually exist in Linux but you get the idea.)
>>
>> Windows:
>> file_full_path = "C://Program Files//R//R-2.13.1//NEWS.pdf"
>> file_full_path = "C:\Program Files\R\R-2.13.1\NEWS.pdf"
>> Linux:
>> file_full_path = "~/Program FilesR/R-2.13.1/NEWS.pdf"
>> file_full_path = "/home/username/Program FilesR/R-2.13.1/NEWS.pdf"
>>
>> I would get for Windows: "C", "Program Files", "R", "R-2.13.1","NEWS.pdf"
>> I would get for Linux: "home","username", "Program Files", "R",
>> "R-2.13.1","NEWS.pdf"
>> (The drive and/or home/username aren't necessary, but would be nice to
>> have. Also, that file name isn't necessary, but would be nice.)
>>
>> Thank you for your help,
>>
>>
> If you use the normalizePath() function with winslash="/", then all
> current platforms will return a path using "/" as the separator, so you
> could do something like
>
> strsplit(normalizePath(**filename, winslash="/"), "/")[[1]]
>
> You need to be careful with normalizePath:  at least on Windows, it will
> not necessarily do what you wanted if the filename doesn't exist.
>
> Duncan Murdoch
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: folders of path - platform independent (repost)

Ben quant
One quick follow-up on reversing your example. Is there an easy way to get
the file.path separator for the platform?  file.path("","") seems the be
the only way to do it.

So if filename is a valid file path, this will return the folders, drive,
and file name in vector form regardless of the platform:
folders = strsplit(normalizePath(filename, winslash="/"), "/")[[1]]
This will undo the above regardless of the platform:
paste(folders,collapse=file.path('"",""))

Thanks again for your help Duncan!

Ben


> On Wed, Dec 28, 2011 at 2:37 PM, Duncan Murdoch <[hidden email]>wrote:
>
>> On 11-12-28 4:30 PM, Ben quant wrote:
>>
>>> Hello, (sorry re-posting due to typo)
>>>
>>> I'm attempting to get the folders of a path in a robust way (platform
>>> independent, format independent). It has to run on Windows and Linux and
>>> tolerate different formats.
>>>
>>> For these: (The paths don't actually exist in Linux but you get the
>>> idea.)
>>>
>>> Windows:
>>> file_full_path = "C://Program Files//R//R-2.13.1//NEWS.pdf"
>>> file_full_path = "C:\Program Files\R\R-2.13.1\NEWS.pdf"
>>> Linux:
>>> file_full_path = "~/Program FilesR/R-2.13.1/NEWS.pdf"
>>> file_full_path = "/home/username/Program FilesR/R-2.13.1/NEWS.pdf"
>>>
>>> I would get for Windows: "C", "Program Files", "R", "R-2.13.1","NEWS.pdf"
>>> I would get for Linux: "home","username", "Program Files", "R",
>>> "R-2.13.1","NEWS.pdf"
>>> (The drive and/or home/username aren't necessary, but would be nice to
>>> have. Also, that file name isn't necessary, but would be nice.)
>>>
>>> Thank you for your help,
>>>
>>>
>> If you use the normalizePath() function with winslash="/", then all
>> current platforms will return a path using "/" as the separator, so you
>> could do something like
>>
>> strsplit(normalizePath(**filename, winslash="/"), "/")[[1]]
>>
>> You need to be careful with normalizePath:  at least on Windows, it will
>> not necessarily do what you wanted if the filename doesn't exist.
>>
>> Duncan Murdoch
>>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: folders of path - platform independent (repost)

David Winsemius

On Dec 28, 2011, at 5:57 PM, Ben quant wrote:

> One quick follow-up on reversing your example. Is there an easy way  
> to get
> the file.path separator for the platform?  file.path("","") seems  
> the be
> the only way to do it.

I don't get it. Did you look at ?file.path  ? It's default call shows  
fsep=

 > .Platform$file.sep
[1] "/"

?.Platform

--
David.
>
> So if filename is a valid file path, this will return the folders,  
> drive,
> and file name in vector form regardless of the platform:
> folders = strsplit(normalizePath(filename, winslash="/"), "/")[[1]]
> This will undo the above regardless of the platform:
> paste(folders,collapse=file.path('"",""))


>
> Thanks again for your help Duncan!
>
> Ben
>
>
>> On Wed, Dec 28, 2011 at 2:37 PM, Duncan Murdoch <[hidden email]
>> >wrote:
>>
>>> On 11-12-28 4:30 PM, Ben quant wrote:
>>>
>>>> Hello, (sorry re-posting due to typo)
>>>>
>>>> I'm attempting to get the folders of a path in a robust way  
>>>> (platform
>>>> independent, format independent). It has to run on Windows and  
>>>> Linux and
>>>> tolerate different formats.
>>>>
>>>> For these: (The paths don't actually exist in Linux but you get the
>>>> idea.)
>>>>
>>>> Windows:
>>>> file_full_path = "C://Program Files//R//R-2.13.1//NEWS.pdf"
>>>> file_full_path = "C:\Program Files\R\R-2.13.1\NEWS.pdf"
>>>> Linux:
>>>> file_full_path = "~/Program FilesR/R-2.13.1/NEWS.pdf"
>>>> file_full_path = "/home/username/Program FilesR/R-2.13.1/NEWS.pdf"
>>>>
>>>> I would get for Windows: "C", "Program Files", "R",  
>>>> "R-2.13.1","NEWS.pdf"
>>>> I would get for Linux: "home","username", "Program Files", "R",
>>>> "R-2.13.1","NEWS.pdf"
>>>> (The drive and/or home/username aren't necessary, but would be  
>>>> nice to
>>>> have. Also, that file name isn't necessary, but would be nice.)
>>>>
>>>> Thank you for your help,
>>>>
>>>>
>>> If you use the normalizePath() function with winslash="/", then all
>>> current platforms will return a path using "/" as the separator,  
>>> so you
>>> could do something like
>>>
>>> strsplit(normalizePath(**filename, winslash="/"), "/")[[1]]
>>>
>>> You need to be careful with normalizePath:  at least on Windows,  
>>> it will
>>> not necessarily do what you wanted if the filename doesn't exist.
>>>
>>> Duncan Murdoch
>>>
>>
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: folders of path - platform independent (repost)

Ben quant
Oops. I guess I stopped reading about the fsep param when I saw PATH and
R_LIB because I'm not interested in those. I didn't get to the part I was
interested in. Thanks!

Ben

On Wed, Dec 28, 2011 at 5:33 PM, David Winsemius <[hidden email]>wrote:

>
> On Dec 28, 2011, at 5:57 PM, Ben quant wrote:
>
>  One quick follow-up on reversing your example. Is there an easy way to get
>> the file.path separator for the platform?  file.path("","") seems the be
>> the only way to do it.
>>
>
> I don't get it. Did you look at ?file.path  ? It's default call shows fsep=
>
> > .Platform$file.sep
> [1] "/"
>
> ?.Platform
>
> --
> David.
>
>
>> So if filename is a valid file path, this will return the folders, drive,
>> and file name in vector form regardless of the platform:
>> folders = strsplit(normalizePath(**filename, winslash="/"), "/")[[1]]
>> This will undo the above regardless of the platform:
>> paste(folders,collapse=file.**path('"",""))
>>
>
>
>
>> Thanks again for your help Duncan!
>>
>> Ben
>>
>>
>>  On Wed, Dec 28, 2011 at 2:37 PM, Duncan Murdoch <
>>> [hidden email]>**wrote:
>>>
>>>  On 11-12-28 4:30 PM, Ben quant wrote:
>>>>
>>>>  Hello, (sorry re-posting due to typo)
>>>>>
>>>>> I'm attempting to get the folders of a path in a robust way (platform
>>>>> independent, format independent). It has to run on Windows and Linux
>>>>> and
>>>>> tolerate different formats.
>>>>>
>>>>> For these: (The paths don't actually exist in Linux but you get the
>>>>> idea.)
>>>>>
>>>>> Windows:
>>>>> file_full_path = "C://Program Files//R//R-2.13.1//NEWS.pdf"
>>>>> file_full_path = "C:\Program Files\R\R-2.13.1\NEWS.pdf"
>>>>> Linux:
>>>>> file_full_path = "~/Program FilesR/R-2.13.1/NEWS.pdf"
>>>>> file_full_path = "/home/username/Program FilesR/R-2.13.1/NEWS.pdf"
>>>>>
>>>>> I would get for Windows: "C", "Program Files", "R",
>>>>> "R-2.13.1","NEWS.pdf"
>>>>> I would get for Linux: "home","username", "Program Files", "R",
>>>>> "R-2.13.1","NEWS.pdf"
>>>>> (The drive and/or home/username aren't necessary, but would be nice to
>>>>> have. Also, that file name isn't necessary, but would be nice.)
>>>>>
>>>>> Thank you for your help,
>>>>>
>>>>>
>>>>>  If you use the normalizePath() function with winslash="/", then all
>>>> current platforms will return a path using "/" as the separator, so you
>>>> could do something like
>>>>
>>>> strsplit(normalizePath(****filename, winslash="/"), "/")[[1]]
>>>>
>>>>
>>>> You need to be careful with normalizePath:  at least on Windows, it will
>>>> not necessarily do what you wanted if the filename doesn't exist.
>>>>
>>>> Duncan Murdoch
>>>>
>>>>
>>>
>>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________**________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/**
>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> David Winsemius, MD
> West Hartford, CT
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.