How to get the location or path of above folder of the current script?

So my structure is like this
-Test
Editor (in here i have a script lets say myscript.cs)

And i want from myscript.cs to grab the location(path) of the Test how would i do that?

Your script doesn’t know where its own script file is, and if you feel like it needs to, I’d suggest that you should step back and rethink what you’re trying to accomplish, because it sounds like whatever it is is probably a bad idea. Why do you need this?

If you’re just looking for the Assets folder in general, Application.dataPath when called in the Editor will do that.

1 Like

I found this So when i execute this it gives me this path
Test/Editor/myscript.cs
but what i want is the path for Test folder

Any idea how to do this please?

 string GetCurrentFileName([System.Runtime.CompilerServices.CallerFilePath] string fileName = null)
    {
        return fileName;
    }
1 Like

It’s a fairly simple string parsing problem at that point, but the fact that you’re doing something so bizarre that I’ve never seen nor heard of anyone doing it in 14 years of coding professionally, suggests to me that this is almost certainly the wrong way to go about doing whatever it is that you’re doing.

I know it may be frustrating that I’m not giving you the answer, but I honestly feel that that would be irresponsible at this point without knowing what your actual end goal with this is. If my instinct is incorrect and this is the correct way to go about doing what you want to do, I’ll happily provide the final steps.

So: what are you trying to do here?

1 Like

@StarManta I’m currently searching (which is not hard at 7.5k posts (gz for that btw)) and reading your answers just for fun. They are hilarious and your signature with the link to a broken page makes it even better :smile:
Sorry, I really enjoy your dry way of putting something while still trying to help as best as you can xD Indeed you seem to have some veteran experience :stuck_out_tongue:

@TheUninvited You need this:

using System.IO;
string wrongPath = "Test/Editor/myscript.cs";
string twoUp = "../../";
string youWantThis = Path.GetFullPath(Path.Combine(wrongPath, twoUp));

Have fun regretting it at some point xP

2 Likes

The gitbook link works for me…?

“Can’t reach server” Tested in Safari and Firefox from location Germany. Not sure whats wrong there :S
Edit: Can’t even reach gitbooks.io Error 502 – Bad Gateway

Nope i knew this but the test folder is not static that’s why i wanted to get the above folder.

It is working for me

This answer by @Leslie-Young can do that, assuming the script’s name is unique inside the project https://answers.unity.com/questions/306751/get-script-path.html

 string[] res = System.IO.Directory.GetFiles(Application.dataPath, "YourEitorScript.cs", SearchOption.AllDirectories);
if (res.Length == 0)
{
     Debug.LogError("error message ....");
     return null;
}
string path = res[0].Replace("YourEitorScript.cs", "").Replace("\\", "/");
Debug.Log(path);

This answer from Baste could also work

1 Like

Thank you @TheUninvited for asking such an unconventional question and providing an answer for it. @StarManta I understand your reluctance here but quite frankly I am a veteran as well and found use for such a code snippet.