%% Extract Paths Before File Names Using Patterns
% Since R2020b
% Create a string array of file names, including full paths.

str = ["C:\Temp\MyReport.docx";
       "C:\Data\Experiment1\Trial1\Sample1.csv";
       "C:\Temp\Slides.pptx"]

%
% To extract the paths, first create a pattern that matches the file name at the end of a path, and then extract the path that comes before that pattern.
% A full path can have several levels, each consisting of any text followed by a "\" character. So start by creating a pattern that matches any characters except the "\" character.

name = wildcardPattern("Except","\")

%
% Then, create a pattern that matches any name found between a "\" character and the end of a string. Use the textBoundary function to match the end of a string.

pat = "\" + name + textBoundary

%
% Finally, call extractBefore.

paths = extractBefore(str,pat)

%
% For a list of functions that create pattern objects, see pattern.
% Copyright 2020 The MathWorks, Inc.