Skip to content

Commit

Permalink
Now can sort files by name content too!
Browse files Browse the repository at this point in the history
  • Loading branch information
unquietwiki committed Nov 5, 2022
1 parent 5e4da49 commit 23881ed
Showing 1 changed file with 53 additions and 20 deletions.
73 changes: 53 additions & 20 deletions src/sortplz.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Michael Adams, unquietwiki.com, 2022-11-04
]#

# Libraries
import std/[os, parseopt, sequtils, terminal]
import std/[os, parseopt, sequtils, strutils, terminal]

# Config import (it's just variables)
include config
Expand All @@ -21,42 +21,69 @@ var
fromdir = getCurrentDir()
todir = getCurrentDir()
exts: seq[string]
names: seq[string]
silent: bool = false
recurse: bool = false

# Function to process & sort directory
proc processDir(ext: string) =

# === Function to handle file moves ===
proc moveThisFile(f: string, newdir: string) =
if not silent:
stdout.styledWriteLine(fgCyan, "Processing extension: ", ext)
stdout.styledWriteLine(fgGreen, "Moving file: ", f.unixToNativePath())
os.moveFile(f, newdir & os.DirSep & os.lastPathPart(f))

# === Function to handle directory moves ===
proc dirProc(value: string): string =
# Does the source directory exist?
if not os.dirExists(fromdir):
stdout.styledWriteLine(fgRed, "Failed to find source directory: ", ext)
stdout.styledWriteLine(fgRed, "Failed to find source directory: ", fromdir)
quit(1)

# Does the destination directory exist?
var newdir: string = os.joinPath(todir, ext)
var newdir: string = os.joinPath(todir, value)
if not silent:
stdout.styledWriteLine(fgYellow, "Creating directory: ", newdir)
os.createDir(newdir)

# Process the list of files in the source directory
# Return newdir as a value for the calling procs to use
if not silent:
stdout.styledWriteLine(fgCyan, "Moving files to ", newdir)
return newdir

# === Function to process & sort directory by name-strings ===
proc processDirNameString(ns: string) =

# prepare the destination directory
if not silent:
stdout.styledWriteLine(fgCyan, "Processing name-string: ", ns)
var newdir = dirProc(ns)

# Process the list of files in the source directory
if not recurse:
for f in toSeq(os.walkFiles(os.joinPath(fromdir))):
moveThisFile(f, newdir)
else:
for f in toSeq(os.walkDirRec(os.joinPath(fromdir))):
if f.contains(ns):
moveThisFile(f, newdir)

# === Function to process & sort directory by extension ===
proc processDirExt(ext: string) =

# prepare the destination directory
if not silent:
stdout.styledWriteLine(fgCyan, "Processing extension: ", ext)
var newdir = dirProc(ext)

# Process the list of files in the source directory
if not recurse:
for f in toSeq(os.walkFiles(os.joinPath(fromdir,"*." & ext))):
if not silent:
stdout.styledWriteLine(fgGreen, "Moving file: ", os.lastPathPart(f))
os.moveFile(f, newdir & os.DirSep & os.lastPathPart(f))
moveThisFile(f, newdir)
else:
for f in toSeq(os.walkDirRec(os.joinPath(fromdir))):
if f.splitFile.ext == "." & ext:
if not silent:
stdout.styledWriteLine(fgGreen, "Moving file: ", f.unixToNativePath())
os.moveFile(f, newdir & os.DirSep & os.lastPathPart(f))
moveThisFile(f, newdir)

# Functions to display command line information
# === Functions to display command line information ===
proc writeVersion() =
stdout.styledWriteLine(fg8Bit, "==============================================================")
stdout.styledWriteLine(fgBlue, name, " ", version)
Expand All @@ -66,11 +93,11 @@ proc writeVersion() =

proc writeHelp() =
writeVersion()
stdout.styledWriteLine(fgGreen, "Usage: sortplz -f:[fromdir] -t:[todir] -e:[ext]")
stdout.styledWriteLine(fgGreen, "Usage: sortplz -f:[fromdir] -t:[todir] -e:[ext] -n:[name]")
stdout.styledWriteLine(fgYellow, "Other flags: --recurse (-r) --help (-h), --version (-v), --silent (-s)")
stdout.styledWriteLine(fg8Bit, "==============================================================")

# Parse command line
# === Parse command line ===
for kind, key, val in getopt():
case kind
of cmdLongOption, cmdShortOption:
Expand All @@ -90,6 +117,10 @@ for kind, key, val in getopt():
if not silent:
stdout.styledWriteLine(fgBlue, "Loading extension: ", val)
exts.add(val)
of "name", "n":
if not silent:
stdout.styledWriteLine(fgBlue, "Considering name-string: ", val)
names.add(val)
of "fromdir", "f":
if val.len > 0: fromdir = val
if not silent:
Expand All @@ -105,10 +136,12 @@ for kind, key, val in getopt():
quit(0)

# Act on provided extensions
if exts.len < 1:
stdout.styledWriteLine(fgRed, "No extensions provided!")
if exts.len < 1 and names.len < 1:
stdout.styledWriteLine(fgRed, "No extensions or name-strings provided!")
writeHelp()
quit(0)
else:
for n in names:
processDirNameString(n)
for e in exts:
processDir(e)
processDirExt(e)

0 comments on commit 23881ed

Please sign in to comment.