Separate path string into drive and folders
332 views (last 30 days)
Show older comments
Hi,
using fileparts brings the path, the name and the exentension of a fullfile (e.g., C:\user\files\myFile.txt). Is there a function that separates the path into drive and folders, so that drive = 'C:' folder1 = 'user' folder2 = 'files' ?
Thx, Philipp
0 Comments
Accepted Answer
Azzi Abdelmalek
on 24 Jul 2013
s='C:\user\files\myFile.txt'
out=regexp(s,'\','split')
2 Comments
Manuel Lera Ramirez
on 5 Jun 2018
Edited: Manuel Lera Ramirez
on 5 Jun 2018
For a general solution (would work on any OS) I would suggest
endout=regexp(s,filesep,'split')
Best
More Answers (2)
Christian Schwermer
on 9 Sep 2018
The shortest solution is to split the string using filesep as delimeter. filesep returns the platform-specific file separator "\" or "/".
pathparts = strsplit(s,filesep);
0 Comments
Dominique
on 19 Jul 2023
path = uigetdir()
foldername = strsplit(path,"\");
foldername = foldername(end);
0 Comments
See Also
Categories
Find more on File Name Construction in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!