Skip to content

Instantly share code, notes, and snippets.

@Varriount
Created January 2, 2014 20:19
Show Gist options
  • Save Varriount/8226008 to your computer and use it in GitHub Desktop.
Save Varriount/8226008 to your computer and use it in GitHub Desktop.
RemoveDir Implementations
## Original Implementation
proc removeDir*(dir: string) =
for kind, path in walkDir(dir):
case kind
of pcFile, pcLinkToFile, pcLinkToDir: removeFile(path)
of pcDir: removeDir(path)
rawRemoveDir(dir)
## New Implementation
proc removeDirTwo*(dir: string) =
## Method Two
var stack = @[(pcDir, dir)]
while stack.len > 0:
var empty = true
var (currentKind, currentDir) = stack[stack.len-1]
for childKind, childPath in walkDir(currentDir):
if childKind in {pcFile, pcLinkToFile}:
removeFile(childPath)
if childKind in {pcDir, pcLinkToDir}:
stack.add((childKind, childPath))
empty = false
if empty:
rawRemoveDir(stack.pop()[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment