Find the depth of the current path
A simple approach in fish:
count (string split / $PWD)
PHP: How do I get the directory depth of the current url?
Here's a function I wrote a few years ago, I use it on my website.
It should help:
<?
function absolute_include($file)
{
/*
$file is the file url relative to the root of your site.
Yourdomain.com/folder/file.inc would be passed as
"folder/file.inc"
*/
$folder_depth = substr_count($_SERVER["PHP_SELF"] , "/");
if($folder_depth == false)
$folder_depth = 1;
include(str_repeat("../", $folder_depth - 1) . $file);
}
?>
How to determine the depth of file path?
You could do it quite easily using pathlib
:
from pathlib import Path
path = Path("directory/folder1/fodler2/folder3/file1.txt")
print(len(path.parents), list(path.parents))
Which gives:
5 [Path('directory/folder1/fodler2/folder3'), Path('directory/folder1/fodler2'), Path('directory/folder1'), Path('directory'), Path('.')]
As can be seen, the results is 5 because "."
is also in the list as "directory/folder1/fodler2/folder3/file1.txt"
is implicitly equal to "./directory/folder1/fodler2/folder3/file1.txt"
so you can always just subtract 1 from the result.
Compared to path.count('/')
, this is platform-independent...
How to measure the depth of a file system path?
Define a measure_depth function:
measure_depth() { echo "${*#/}" | awk -F/ '{print NF}'; }
Then, use it as follows:
$ measure_depth /a/b/c/d/e/f
6
$ measure_depth /a
1
Get directories from the current to the n-th depth
You can clear the directories returned by os.walk
to prevent it from traversing deeper when it has reached your desired depth:
for root, dirs, _ in os.walk(path, topdown=True):
if root == path:
continue
parents.append(root)
children.extend(dirs)
dirs.clear()
Find highest depth when given a file path
You can do a "depth first search"
def get_depth(path, depth=0):
if not os.path.isdir(path): return depth
maxdepth = depth
for entry in os.listdir(path):
fullpath = os.path.join(path, entry)
maxdepth = max(maxdepth, get_depth(fullpath, depth + 1))
return maxdepth
which is the general approach to your solution but I think that you forgot to count that regular files have depth one greater than the directory that they are in.
React - Nested routes - Get depth of current URL
Just pass the depth variable into the next component.
function Node({depth = 0}) {
const { id } = useParams();
const { url, path } = useRouteMatch();
const { loading, data } = useApi(NODES);
const { nodes } = data || {};
return (
<>
<h2>Level 1</h2>
{
nodes?.map(x =>
<p key={x.id}>
<Link to={`${url}/${x.id}`}>
{x.name}
</Link>
</p>
)
}
<Switch>
<Route path={`${path}/:id`}>
<Node depth={depth + 1}/>
</Route>
</Switch>
</>
);
}
C# Best way to get folder depth for a given path?
Off the top of my head:
Directory.GetFullPath().Split("\\").Length;
How to exclude this / current / dot folder from find type d
POSIX 7 solution:
find . ! -path . -type d
For this particular case (.
), golfs better than the mindepth
solution (24 vs 26 chars), although this is probably slightly harder to type because of the !
.
To exclude other directories, this will golf less well and requires a variable for DRYness:
D="long_name"
find "$D" ! -path "$D" -type d
My decision tree between !
and -mindepth
:
- script? Use
!
for portability. - interactive session on GNU?
- exclude
.
? Throw a coin. - exclude
long_name
? Use-mindepth
.
- exclude
Related Topics
Graphing The Dag Generated by Make
Git: Can't Push (Strange Config Issue)
Apache Cgi in User Directory "End of Script Output Before Headers"
How to Take Advantage of The Vdso Object with Your Own Programming Language
How to Execute Shell Builtin from Scala
How to Add an Icon to The Bash Prompt
Receiving Multicast on a Server with Multiple Interfaces (Linux)
List Directory Entries in The Svn Repository
Getting Android Sdk Tools to Work on Raspberry Pi
Linux: Disable Using Loopback and Send Data via Wire Between 2 Eth Cards of One Comp
Vs Code Ssh Remote Connection Issues
Understanding /Boot/Config File
Reusing Custom Makefile for Static Library with Cmake
Linux Desktop Shortcut and Icon from Install
Read Serial Data Without High CPU Use