Path

version 2.2.0 (17.9 KB) by Martin Koch
Class for handling file system paths with short and readable code

84 Downloads

Updated 10 Sep 2022

From GitHub

View License on GitHub

Path

Class for representing filesystem paths in MATLAB and solving path-related problems with short and readable code.

Features
Examples
Installation
Reference

View Path on File Exchange

Features

  • Get and set path name, parent, root, stem and extension
  • Filter paths by extension, name, etc. using wildcards
  • List files recursively
  • Handle lists of paths
  • Clean and resolve paths
  • Build absolute and relative paths
  • Create, copy, move, delete files and directories
  • Get directory of currently executing MATLAB file

Examples

Path properties

>> file = Path("C:\data") \ "model.dat"
   Path("C:\data\model.dat")
>> file.parent
   Path("C:\data")
>> file.stem
   "model"
>> file.extension
   ".dat"

Arrays of paths

>> personalFolders = Path("Astronauts") / ["Arthur", "Trillian", "Zaphod"]
    Path("Astronauts\Arthur")
    Path("Astronauts\Trillian")
    Path("Astronauts\Zaphod")
>> personalFolders.join("DONT_PANIC.txt").createEmptyFile;

Filtering and chaining

>> files = Path("Sketchy Folder").listDeepFiles
    Path("Sketchy Folder\DeleteStuffVirus.exe")
    Path("Sketchy Folder\System32\nastyWorm.dll")
    Path("Sketchy Folder\dark_corner\half_a_sandwich.dat")
    Path("Sketchy Folder\WormholeResearch.pdf")
>> files.where("Stem", ["*Virus*", "*Worm*"], "ExtensionNot", ".pdf").moveToDir("D:\Quarantine");

Path and directory of executing file

scriptFile = Path.this
    Path("C:/projects/SpaceCatapult/simulate.m")
scriptDir = Path.here
    Path("C:/projects/SpaceCatapult")
scriptDir.cd;

Installation

Download or clone this repository and add it to your MATLAB search path. Requires R2019b or newer.

Reference

Constructor

Create Path objects by calling Path(...) with one or multiple arguments of type string vector, char vector, cell of string or char vectors.

Type conversions

Method Return type Description
string string Convert to string
char char Convert to char array
cellstr cell Convert to cell of char arrays

Properties

Method Return type Description
name Path File or folder name without directory
parent Path Parent directory
root Path First directory element of absolute paths
stem string File name without extension
extension string File extension
parts string Split path into list comprising root, folders and name
strlength double Number of characters in the path string
absolute Path Absolute path assuming the current working directory as reference
relative Path Path relative to reference directory
is logical Whether properties match patterns
isAbsolute logical Whether path is absolute
isRelative logical Whether path is relative

Modify

Method Return type Description
/, \, join Path Join paths
+, addSuffix Path Add string to the end of the path
setName Path Set file or folder name without directory
setParent Path Set parent directory
setRoot Path Set first directory element
setStem Path Set file name without extension
setExtension Path Set file extension
addStemSuffix Path Add string to the end of the file stem
regexprep Path Wrapper for built-in regexprep
tempFileName Path Append random unique file name

Compare and filter

Method Return type Description
==, eq logical Whether path strings are equal
~=, ne logical Whether path strings are unequal
where Path Select paths where properties match patterns

File system interaction

Method Return type Description
exists logical Whether path exists in filesystem
isFile logical Whether path is an existing file
isDir logical Whether path is an existing directory
mustExist - Raise error if path does not exist
mustBeFile - Raise error if path is not an existing file
mustBeDir - Raise error if path is not an existing directory
modifiedDate datetime Date and time of last modification
bytes - File size in bytes
mkdir - Create directory if it does not already exist
cd Path Wrapper for built-in cd
createEmptyFile - Create an empty file
delete - Delete files and directories. Remove directories recursively with optional argument 's'.
fopen [double, char] Wrapper for built-in fopen
open [double, onCleanup] Open file and return file ID and onCleanup object, which closes the file on destruction. Create parent directory if necessary. Raise error on failure.
readText string Read text file
writeText - Write text file
copy - Copy to new path
copyToDir - Copy into target directory preserving the original name
move - Move to new path (rename)
moveToDir - Move into target directory preserving the original name
listFiles Path List file paths in directory
listDeepFiles Path List files paths in directory and all its subdirectories
listFolders Path List directories in directory
listDeepFolders Path List directories and subdirectories in directory

Array

Method Return type Description
isEmpty logical Check if array is empty
count double Number of elements
sort [Path, double] Sort by path string
unique_ [Path, double, double] Wrapper for built-in unique
deal [Path, Path, ...] Distribute array objects among output arguments

Factories

Method Return type Description
Path.current Path Current working directory; wrapper for built-in pwd
Path.home Path User home directory
Path.tempDir Path Temporary directory; wrapper for built-in tempdir
Path.tempFile Path Random unique file in temporary directory; wrapper for built-in tempname
Path.matlab Path MATLAB install directory; wrapper for built-in matlabroot
Path.searchPath Path Folders on MATLAB search path; wrapper for built-in path
Path.userPath Path MATLAB user directory; wrapper for built-in userpath
Path.ofMatlabFile Path Path of MATLAB file on the MATLAB search path
Path.this Path Path of MATLAB file executing this method
Path.here Path Directory of MATLAB file executing this method
Path.empty Path Empty object array

Other

Method Return type Description
disp - Display in console
help - Open documentation web page

Cite As

Martin Koch (2022). Path (https://github.com/MartinKoch123/Path/releases/tag/v2.2.0), GitHub. Retrieved .

MATLAB Release Compatibility
Created with R2022a
Compatible with R2019b and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
To view or report issues in this GitHub add-on, visit the GitHub Repository.
To view or report issues in this GitHub add-on, visit the GitHub Repository.