digitalmars.D.learn - Remove filename from path
- Suliman (2/2) Sep 24 2014 What is the best way to remove file name from full path?
- monarch_dodra (4/6) Sep 24 2014 Seems like "dirName" in std.path is a good candidate ;)
- Suliman (1/5) Sep 24 2014 Thanks! But if I want to strip it, how I can cut it?
- Suliman (4/4) Sep 24 2014 I can't understand how to use strip? For example I would like to
- ketmar via Digitalmars-d-learn (4/6) Sep 24 2014 On Wed, 24 Sep 2014 10:35:28 +0000
- monarch_dodra (6/10) Sep 24 2014 "stripExtension" would be your friend here.
- monarch_dodra (14/18) Sep 24 2014 strip doens't work that way. It simply removes leading/trailing
- ketmar via Digitalmars-d-learn (4/6) Sep 24 2014 On Wed, 24 Sep 2014 12:21:40 +0000
- monarch_dodra (4/10) Sep 24 2014 I missread that documentation. I thought it removed all
- ketmar via Digitalmars-d-learn (5/7) Sep 24 2014 On Wed, 24 Sep 2014 12:39:01 +0000
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (23/41) Sep 24 2014 find() and friends can be used:
- monarch_dodra (24/46) Sep 25 2014 I had thought of that, but then you might get in trouble with
- Jay Norwood (4/11) Sep 25 2014 dirName gives the directory, baseName the filename,
- Jay Norwood (4/16) Sep 25 2014 easier than that. Looks like stripExtension handles the whole
What is the best way to remove file name from full path? string path = thisExePath()
Sep 24 2014
On Wednesday, 24 September 2014 at 10:11:04 UTC, Suliman wrote:What is the best way to remove file name from full path? string path = thisExePath()Seems like "dirName" in std.path is a good candidate ;) You'll find many other path manipulation functions there.
Sep 24 2014
Thanks! But if I want to strip it, how I can cut it?string path = thisExePath()Seems like "dirName" in std.path is a good candidate ;) You'll find many other path manipulation functions there.
Sep 24 2014
I can't understand how to use strip? For example I would like to cut just extension. path = path.stripRight("exe"); Error: no overload matches for stripRight(C)(C[] str) if
Sep 24 2014
On Wed, 24 Sep 2014 10:35:28 +0000 Suliman via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:I can't understand how to use strip? For example I would like to=20 cut just extension.std.path.setExtension is your friend.
Sep 24 2014
On Wednesday, 24 September 2014 at 10:35:29 UTC, Suliman wrote:I can't understand how to use strip? For example I would like to cut just extension. path = path.stripRight("exe"); Error: no overload matches for stripRight(C)(C[] str) if"stripExtension" would be your friend here. If you want the extension, then "extension". As a general rule, everything you need to manipulate paths and filenames is in std.path: http://dlang.org/phobos/std_path.html
Sep 24 2014
On Wednesday, 24 September 2014 at 10:35:29 UTC, Suliman wrote:I can't understand how to use strip? For example I would like to cut just extension. path = path.stripRight("exe"); Error: no overload matches for stripRight(C)(C[] str) ifstrip doens't work that way. It simply removes leading/trailing white. There's a version in std.algorithm which is more generic, but it accepts either a predicate, or an element, but not a range. Unfortunately, there is no generic function that allows striping of a specific ending range (though there are ways to either detect it, or strip it from the end). If you want something generic, then: string path = "myFile.doc"; string extension = ".doc"; if (path.endsWith(extension)) path = path[0 .. $ - extension.length]; Would work.
Sep 24 2014
On Wed, 24 Sep 2014 12:21:40 +0000 monarch_dodra via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Unfortunately, there is no generic function that allows striping=20 of a specific ending rangebut for strings we have std.string.chomp.
Sep 24 2014
On Wednesday, 24 September 2014 at 12:29:09 UTC, ketmar via Digitalmars-d-learn wrote:On Wed, 24 Sep 2014 12:21:40 +0000 monarch_dodra via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:I missread that documentation. I thought it removed all characters that can also be found in delim. Power to me.Unfortunately, there is no generic function that allows striping of a specific ending rangebut for strings we have std.string.chomp.
Sep 24 2014
On Wed, 24 Sep 2014 12:39:01 +0000 monarch_dodra via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:I missread that documentation. I thought it removed all=20 characters that can also be found in delim. Power to me.ah, i just found this function (really, less than hour ago), that's why i still remember where it is and what it's doing. ;-)
Sep 24 2014
On 09/24/2014 05:21 AM, monarch_dodra wrote:On Wednesday, 24 September 2014 at 10:35:29 UTC, Suliman wrote:find() and friends can be used: import std.algorithm; void main() { string path = "myFile.doc"; string extension = ".doc"; path = findSplitBefore(path, extension)[0]; assert(path == "myFile"); } And three retro()s make one modern(): :p import std.algorithm; import std.range; void main() { string path = "myFile.doc"; string extension = ".doc"; path = findSplitAfter(path.retro, extension.retro) [1].retro; assert(path == "myFile"); } AliI can't understand how to use strip? For example I would like to cut just extension. path = path.stripRight("exe"); Error: no overload matches for stripRight(C)(C[] str) ifstrip doens't work that way. It simply removes leading/trailing white. There's a version in std.algorithm which is more generic, but it accepts either a predicate, or an element, but not a range. Unfortunately, there is no generic function that allows striping of a specific ending range (though there are ways to either detect it, or strip it from the end). If you want something generic, then: string path = "myFile.doc"; string extension = ".doc"; if (path.endsWith(extension)) path = path[0 .. $ - extension.length]; Would work.
Sep 24 2014
On Wednesday, 24 September 2014 at 17:15:39 UTC, Ali Çehreli wrote:find() and friends can be used: import std.algorithm; void main() { string path = "myFile.doc"; string extension = ".doc"; path = findSplitBefore(path, extension)[0]; assert(path == "myFile"); }I had thought of that, but then you might get in trouble with something like: string path = "myPath.doc.old"And three retro()s make one modern(): :p import std.algorithm; import std.range; void main() { string path = "myFile.doc"; string extension = ".doc"; path = findSplitAfter(path.retro, extension.retro) [1].retro; assert(path == "myFile"); } AliThat was the next one I had. Except here: - You still run into issues if the extension is *not* .doc (and there happens to be a .doc somewhere in there). - You are paying for a search, when you are only interested in testing a prefix. I had thought of this though: void main() { string path = "myFile.doc"; string extension = ".doc"; auto rpath = path.retro(); skipOver(rpath, extension.retro); path = rpath.retro(); assert(path == "myFile"); } The "issue" though is that skipOver modifies an rvalue, so it's not as "functional-style" as I would have liked it. Anyways, the conclusion here (IMO), is that to manipulate paths, use std.path.
Sep 25 2014
On Wednesday, 24 September 2014 at 10:28:05 UTC, Suliman wrote:dirName gives the directory, baseName the filename, stripExtension strips it, so seems like what you want is dirName ~ stripExtension( baseName )Thanks! But if I want to strip it, how I can cut it?string path = thisExePath()Seems like "dirName" in std.path is a good candidate ;) You'll find many other path manipulation functions there.
Sep 25 2014
On Friday, 26 September 2014 at 03:32:46 UTC, Jay Norwood wrote:On Wednesday, 24 September 2014 at 10:28:05 UTC, Suliman wrote:easier than that. Looks like stripExtension handles the whole path. assert (stripExtension("dir/file.ext") == "dir/file");dirName gives the directory, baseName the filename, stripExtension strips it, so seems like what you want is dirName ~ stripExtension( baseName )Thanks! But if I want to strip it, how I can cut it?string path = thisExePath()Seems like "dirName" in std.path is a good candidate ;) You'll find many other path manipulation functions there.
Sep 25 2014