2 summary:: file and directory path utilities
3 related:: Classes/File, Classes/String
7 PathName is a utility class for manipulating file names and paths. It expects a path to a file, and lets you access parts of that file path.
16 a link::Classes/String:: which likely contains one or more / as typical for folder separation. ~ will be converted to your fully addressed home directory, as per link::Classes/String#-standardizePath::.
18 PathName.new("MyDisk/SC 2.2.8 f/Sounds/FunkyChicken");
22 Get or set the global temp directory as a link::Classes/String::. This is used by link::Classes/Buffer::, etc. By default this is "/tmp/" for Linux and OSX, and "/WINDOWS/TEMP/" for Windows.
27 returns just the name of the file itself; i.e. everything after the last slash in the full path.
31 myPath = PathName.new("MyDisk/SC 2.2.8 f/Sounds/FunkyChicken");
32 myPath.fileName.postln;
36 method:: fileNameWithoutExtension
37 returns the name of the file itself without the file extension.
40 returns the file extension, i.e. everything after the last full-stop in the link::#-fileName::.
43 returns the full path up to the file name itself; i.e. everything up to and including the last slash. This is handy e.g. for storing several files in the same folder.
48 myPath = PathName.new("MyDisk/SC 2.2.8 f/Sounds/FunkyChicken");
49 myPath.pathOnly.postln;
53 method::isAbsolutePath, asAbsolutePath, isRelativePath, asRelativePath
54 you MUST have correctly initialized the scroot classvar for this to know what it is relative to !
57 returns only the name of the folder that the file is in; i.e. everything in between the last but one and the last slash.
61 myPath = PathName.new("MyDisk/SC 2.2.8 f/Sounds/FunkyChicken");
62 myPath.folderName.postln;
67 returns the full path name that PathName contains.
71 myPath = PathName.new("MyDisk/SC 2.2.8 f/Sounds/FunkyChicken");
72 myPath.fullPath.postln;
77 returns a list of all the files+folders inside the folder represented by this path.
81 myPath = PathName.new("./");
82 myPath.entries.postln;
87 returns a list of all the files in the folder represented by this path.
91 myPath = PathName.new("./");
97 returns a list of all the subfolders of the folder represented by this path.
101 myPath = PathName.new("./");
102 myPath.folders.postln;
107 returns a link::Classes/Boolean:: indicating whether or not the path represents a file (not a folder).
111 myPath = PathName.new("./");
112 myPath.isFile.postln;
117 returns a link::Classes/Boolean:: indicating whether or not the path represents a folder (not a file).
121 myPath = PathName.new("./");
122 myPath.isFolder.postln;
127 Iterates over all files found in the pathname, including ones in subfolders.
131 myPath = PathName.new("./");
132 myPath.filesDo{|afile| afile.postln};
137 returns a list of all the folder names contained in the pathname itself.
141 myPath = PathName.new("MyDisk/SC 2.2.8 f/Sounds/FunkyChicken");
142 myPath.allFolders.postln;
147 if path is an absolute path, returns the disk name; else a blank string.
151 myPath = PathName.new("MyDisk/SC 2.2.8 f/Sounds/FunkyChicken");
152 myPath.diskName.postln;
155 ( // note the / at the start
157 myPath = PathName.new("/MyDisk/SC 2.2.8 f/Sounds/FunkyChicken");
158 myPath.diskName.postln;
163 Path concatenation operator - useful for avoiding doubling-up slashes unnecessarily.
165 (PathName("/somewhere") +/+ PathName("over/the/rainbow")).postln;
166 (PathName("/somewhere") +/+ PathName("/over/the/rainbow")).postln;
170 returns a number at the end of PathName. Returns zero if there is no number.
172 PathName("floating1").endNumber.postln;
173 PathName("floating").endNumber.postln;
177 returns link::#-fullPath:: without any numbers at the end.
179 PathName("floating1").noEndNumbers.postln;
180 PathName("floating").noEndNumbers.postln;
184 generates a sensible next name for a file by incrementing a number at the end of PathName, or by adding one if there is none. This is useful for recording files with consecutive names, and e.g. to generate a new filename when you don't want to overwrite an existing file with the current name.
186 PathName("floating34").nextName.postln;
187 PathName("floating").nextName.postln;
188 PathName("floating12_3A4X_56.7").nextName.postln;
193 Here is an example that uses many instance methods. Just pick any file to see all the parts of its path.
202 myPathName = PathName.new(path);
204 "New PathName object: ".postc;
207 "fileName only: ".postc;
208 myPathName.fileName.postln;
210 "path up to file only: ".postc;
211 myPathName.pathOnly.postln;
213 "folder Name: ".postc;
214 myPathName.folderName.postln;
222 Choose a soundfile to put into the library, using its foldername and filename.
228 var myPathName, myFile;
231 myPathName = PathName.new(path);
233 // read your file from disk, e.g. a soundFile/
235 myFile = SoundFile.new;
236 if (myFile.openRead(path),
239 [ myPathName.folderName.asSymbol, myPathName.fileName.asSymbol ],
241 ("Check Library.global" + myPathName.folderName + "please.").postln;
243 { ("Could not read soundfile" + path ++ ".").postln; }
253 Save three tables in the same folder. Note: The file name chosen in the dialog is ignored! The files are always named table1, table2, table3.
257 var table1, table2, table3;
259 table1 = Wavetable.sineFill(1024, [1,2,3]);
260 table2 = Signal.newClear.asWavetable;
261 table3 = Wavetable.sineFill(1024, Array.rand(64, 0.0, 1.0));
265 var myPathName, myPathOnly;
268 myPathName = PathName.new(path);
269 myPathOnly = myPathName.pathOnly;
270 ("writing files tables1-3 to"+myPathOnly).postln;
271 table1.write(myPathOnly ++ "table1");
272 table2.write(myPathOnly ++ "table2");
273 table3.write(myPathOnly ++ "table3");