Separate path string into drive and folders

248 次查看(过去 30 天)
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

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-7-24
s='C:\user\files\myFile.txt'
out=regexp(s,'\','split')
  2 个评论
Manuel Lera Ramirez
编辑:Manuel Lera Ramirez 2018-6-5
For a general solution (would work on any OS) I would suggest
endout=regexp(s,filesep,'split')
Best

请先登录,再进行评论。

更多回答(2 个)

Christian Schwermer
The shortest solution is to split the string using filesep as delimeter. filesep returns the platform-specific file separator "\" or "/".
pathparts = strsplit(s,filesep);

Dominique
Dominique 2023-7-19
path = uigetdir()
foldername = strsplit(path,"\");
foldername = foldername(end);

类别

Help CenterFile Exchange 中查找有关 File Name Construction 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by