From bef667d9fda9077d21fd6c92448e05704b91a6fa Mon Sep 17 00:00:00 2001 From: Ada Werefox Date: Thu, 10 Apr 2025 11:41:29 -0700 Subject: [PATCH] Initial commit. --- .gitignore | 1 + README.md | 3 +++ go-file-sort.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++ media/.gitkeep | 0 5 files changed, 76 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 go-file-sort.go create mode 100644 go.mod create mode 100644 media/.gitkeep diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e66ca0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +sorted_*/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..3085cff --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# File Sorter + +A small script to sort files into last modified year/month directories. \ No newline at end of file diff --git a/go-file-sort.go b/go-file-sort.go new file mode 100644 index 0000000..ea3bdad --- /dev/null +++ b/go-file-sort.go @@ -0,0 +1,69 @@ +package main + +import ( + "fmt" + "log" + "os" +) + +func main() { + // TODO: Replace with cli arg parsing. + // Directory to be sorted + media_dir := "media/" + // A destination directory of sorted files + sorted_media_dir := fmt.Sprintf("sorted_%s", media_dir) + + // Get a list of the files in the directory to be sorted + files, err := os.ReadDir(media_dir) + if err != nil { + log.Fatal(err) + } + + // Make the destination directory + err = os.Mkdir(sorted_media_dir, 0755) + if err != nil && !os.IsExist(err) { + log.Fatalf("Couldn't create sorted files directroy: %v", err) + } + + // Iterate through the list of files + for _, file := range files { + // Don't try to sort directories + if file.IsDir() { + continue + } + // The name string of the file we're looking at + current_file_name := file.Name() + + // Grab file info + current_file, err := os.Stat(fmt.Sprintf("%s%s", media_dir, current_file_name)) + if err != nil { + log.Fatalf("Couldn't stat current file: %v", err) + } + + // Save the last modified year and month + current_file_year := current_file.ModTime().Year() + current_file_month := current_file.ModTime().Month() + + // Make directories for the sorted files to go into + err = os.Mkdir(fmt.Sprintf("%s%d", sorted_media_dir, current_file_year), 0755) + if err != nil && !os.IsExist(err) { + log.Fatalf("Couldn't create new directory: %v", err) + } + + new_directory_path := fmt.Sprintf("%d/%02d/", current_file_year, current_file_month) + err = os.Mkdir(fmt.Sprintf("%s%s", sorted_media_dir, new_directory_path), 0755) + if err != nil && !os.IsExist(err) { + log.Fatalf("Couldn't create new directory: %v", err) + } + + // Debug output of last modified year/month, and the file name + log.Default().Println(fmt.Sprintf("%d/%02d | %s", current_file_year, current_file_month, current_file_name)) + + // Set the current file path and the destination file path + full_current_file_path := fmt.Sprintf("%s%s", media_dir, current_file_name) + full_new_file_path := fmt.Sprintf("%s%s%s", sorted_media_dir, new_directory_path, current_file_name) + + // Move the file + err = os.Rename(full_current_file_path, full_new_file_path) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e14d7e6 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module go-file-sort + +go 1.24.2 diff --git a/media/.gitkeep b/media/.gitkeep new file mode 100644 index 0000000..e69de29