# File::createLegalPathName()

**URL:** <https://forum.juce.com/t/file-createlegalpathname/12554>\
**Category:** General JUCE discussion\
**Created:** [March 14, 2014, 4:56pm UTC](https://forum.juce.com/t/file-createlegalpathname/12554 "2014-03-14T16:56:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![haydxn](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@haydxn](https://forum.juce.com/u/haydxn)\
**Post date:** [March 14, 2014, 4:56pm UTC](https://forum.juce.com/t/file-createlegalpathname/12554/1 "2014-03-14T16:56:54Z")

</div>

This function can cause an assert&nbsp;if the input&nbsp;is empty...

```
String File::createLegalPathName (const String& original)
{
    String s (original);
    String start;

    if (s[1] == ':') // <- here
    {
       ...
```

I expect the result would technically not be a legal path, but should it handle this?

On a related note, I've found that (on windows) isAbsolutePath() returns true for "/" (which isn't valid). There doesn't appear to be a 100% reliable function to determine whether or not&nbsp;a given (user provided) String will successfully make it through the File constructor. Unless I'm missing something!

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 14, 2014, 6:37pm UTC](https://forum.juce.com/t/file-createlegalpathname/12554/2 "2014-03-14T18:37:06Z")

</div>

Ta, will avoid that assertion.

Can't see how '/' could be an absolute path on windows though... Do you mean '\' ?

---

<div class="post-metadata">

**Author:** ![Peter\_Emanuel\_Roos](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/peter_emanuel_roos/32/189_2.png) [@Peter\_Emanuel\_Roos](https://forum.juce.com/u/Peter_Emanuel_Roos)\
**Post date:** [March 14, 2014, 6:40pm UTC](https://forum.juce.com/t/file-createlegalpathname/12554/3 "2014-03-14T18:40:00Z")

</div>

Mmm, when I am working with Windows cmd.exe and Linux consoles at the same time, I always use / in Windows as well, no problem

---

<div class="post-metadata">

**Author:** ![haydxn](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@haydxn](https://forum.juce.com/u/haydxn)\
**Post date:** [March 14, 2014, 6:58pm UTC](https://forum.juce.com/t/file-createlegalpathname/12554/4 "2014-03-14T18:58:53Z")

</div>

Sorry, in the code it just uses the&nbsp;'separator' symbol, which my brain always classes as '/' :)

My point was that the following bit of code...

```
bool File::isAbsolutePath (StringRef path)
{
    return path.text[0] == separator
           #if JUCE_WINDOWS
            || (path.isNotEmpty() && path.text[1] == ':');
           #else
...
```

... sees the single slash character in the string and so returns that it is an absolute path - but obviously it isn't, and the File constructor rejects it.

Basically, I'm just trying to find a way to know for sure whether or not a given string will&nbsp;be safe to use in the&nbsp;File constructor :)&nbsp;

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 14, 2014, 7:01pm UTC](https://forum.juce.com/t/file-createlegalpathname/12554/5 "2014-03-14T19:01:33Z")

</div>

Well.. &nbsp;a path that starts with a backslash is _kind of_ an absolute path, don't you think? Or do we reckon that on Windows only something that starts with a drive letter can count as an absolute path?

---

<div class="post-metadata">

**Author:** ![haydxn](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@haydxn](https://forum.juce.com/u/haydxn)\
**Post date:** [March 14, 2014, 8:49pm UTC](https://forum.juce.com/t/file-createlegalpathname/12554/6 "2014-03-14T20:49:05Z")

</div>

It's funny you should say that, as I've been going backwards and forwards about it for a few days - I only even decided to mention it because of the other assert :) obviously a slash at the start means it's not a relative path, therefore it is effectively absolute... But not necessarily fully formed.

It always comes back to needing to be able to satisfy the File class itself. Perhaps moving the constructor checks out into a public static 'isValidPath' function is what is really needed.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 15, 2014, 12:15pm UTC](https://forum.juce.com/t/file-createlegalpathname/12554/7 "2014-03-15T12:15:30Z")

</div>

But what would 'isValidPath' really mean.. Isn't a partial path also valid if you're planning on using it in getChildFile()?

---

<div class="post-metadata">

**Author:** ![haydxn](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@haydxn](https://forum.juce.com/u/haydxn)\
**Post date:** [March 15, 2014, 2:06pm UTC](https://forum.juce.com/t/file-createlegalpathname/12554/8 "2014-03-15T14:06:28Z")

</div>

Well, maybe not isValidPath :) - perhaps isValidFullPath or&nbsp;isFullyFormedAbsolutePath... whatever you would call the checks that the File class itself does to the provided path upon construction.

I.e. the File class&nbsp;has a specific&nbsp;idea about what makes a valid path, (since it asserts if a path doesn't qualify!). That is what I mean by valid path :D

That's really all it comes down to - I just need to be able to guarantee that it is safe to construct a File&nbsp;for&nbsp;a given String (since isAbsolutePath isn't sufficient&nbsp;for that), without having to test manually.

If I'm missing an existing function that does the trick for this, let me know :D

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 15, 2014, 2:13pm UTC](https://forum.juce.com/t/file-createlegalpathname/12554/9 "2014-03-15T14:13:00Z")

</div>

I'm just wondering why I've never needed to do a similar check..&nbsp;I think that when I've got a user-supplied string that may be dodgy,&nbsp;I generally use File::getCurrentWorkingDirectory().getChildFile (dodgyString), so that it's actually ok to get a partial path.

---

<div class="post-metadata">

**Author:** ![haydxn](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@haydxn](https://forum.juce.com/u/haydxn)\
**Post date:** [March 15, 2014, 2:39pm UTC](https://forum.juce.com/t/file-createlegalpathname/12554/10 "2014-03-15T14:39:00Z")

</div>

That makes sense, and I might be able to work with it - but I think we're thinking about different problems.

Using that approach is a means of **coping** &nbsp;with input - it doesn't actually allow you to **validate** &nbsp;it, since it will give you a File regardless (and - depending on the input -&nbsp;that File's path could be different at runtime across different machines). I'm in particular trying to validate a specific path, rather than merely cope with it and potentially end up pointing at something different.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 16, 2014, 12:33pm UTC](https://forum.juce.com/t/file-createlegalpathname/12554/11 "2014-03-16T12:33:58Z")

</div>

Yup. Maybe sketch out the function that you're really looking for, and I can add it if it makes sense!
