os.walkDir supports yielding relative paths
This commit is contained in:
parent
ab6f8f6e5b
commit
c852143f3a
1 changed files with 8 additions and 4 deletions
|
|
@ -810,11 +810,12 @@ type
|
||||||
|
|
||||||
{.deprecated: [TPathComponent: PathComponent].}
|
{.deprecated: [TPathComponent: PathComponent].}
|
||||||
|
|
||||||
iterator walkDir*(dir: string): tuple[kind: PathComponent, path: string] {.
|
iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path: string] {.
|
||||||
tags: [ReadDirEffect].} =
|
tags: [ReadDirEffect].} =
|
||||||
## walks over the directory `dir` and yields for each directory or file in
|
## walks over the directory `dir` and yields for each directory or file in
|
||||||
## `dir`. The component type and full path for each item is returned.
|
## `dir`. The component type and full path for each item is returned.
|
||||||
## Walking is not recursive.
|
## Walking is not recursive. If ``relative`` is true the resulting path is
|
||||||
|
## shortened to be relative to ``dir``.
|
||||||
## Example: This directory structure::
|
## Example: This directory structure::
|
||||||
## dirA / dirB / fileB1.txt
|
## dirA / dirB / fileB1.txt
|
||||||
## / dirC
|
## / dirC
|
||||||
|
|
@ -843,7 +844,9 @@ iterator walkDir*(dir: string): tuple[kind: PathComponent, path: string] {.
|
||||||
k = pcDir
|
k = pcDir
|
||||||
if (f.dwFileAttributes and FILE_ATTRIBUTE_REPARSE_POINT) != 0'i32:
|
if (f.dwFileAttributes and FILE_ATTRIBUTE_REPARSE_POINT) != 0'i32:
|
||||||
k = succ(k)
|
k = succ(k)
|
||||||
yield (k, dir / extractFilename(getFilename(f)))
|
let xx = if relative: extractFilename(getFilename(f))
|
||||||
|
else: dir / extractFilename(getFilename(f))
|
||||||
|
yield (k, xx)
|
||||||
if findNextFile(h, f) == 0'i32: break
|
if findNextFile(h, f) == 0'i32: break
|
||||||
findClose(h)
|
findClose(h)
|
||||||
else:
|
else:
|
||||||
|
|
@ -855,7 +858,8 @@ iterator walkDir*(dir: string): tuple[kind: PathComponent, path: string] {.
|
||||||
var y = $x.d_name
|
var y = $x.d_name
|
||||||
if y != "." and y != "..":
|
if y != "." and y != "..":
|
||||||
var s: Stat
|
var s: Stat
|
||||||
y = dir / y
|
if not relative:
|
||||||
|
y = dir / y
|
||||||
var k = pcFile
|
var k = pcFile
|
||||||
|
|
||||||
when defined(linux) or defined(macosx) or defined(bsd):
|
when defined(linux) or defined(macosx) or defined(bsd):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue