SoFunction
Updated on 2025-04-08

Develop a command line file management tool using Go language

Introduction

Are you still worried about the cumbersome file operation? Today I will teach you to develop a command line file management tool in Go language, which supports batch renaming, deleting, creating, and moving files, freeing your hands and improving efficiency! The complete source code is attached at the end of the article, and it is recommended to collect it!

1. At a glance of tool functions

  • Batch renaming: Support regular expression matching file names
  • Batch Delete: Delete the file according to regular expression
  • Create a file/directory: Create a file or directory with one click
  • Batch mobile: Move the matching file to the specified directory

2. Core code analysis

1. Main program structure

func main() {
    // Define subcommands    renameCmd := ("rename", )
    deleteCmd := ("delete", )
    createCmd := ("create", )
    moveCmd := ("move", )

    // parse command line parameters    switch [1] {
    case "rename":
        ([2:])
        renameFiles(*renamePattern, *renameReplacement)
    case "delete":
        ([2:])
        deleteFiles(*deletePattern)
    case "create":
        ([2:])
        createFileOrDir(*createPath, *createIsDir)
    case "move":
        ([2:])
        moveFiles(*moveSource, *moveTarget)
    default:
        ("Expected subcommands: rename, delete, create, or move")
        (1)
    }
}

2. Batch renaming

func renameFiles(pattern, replacement string) {
    regex, err := (pattern)
    if err != nil {
        ("Invalid regex pattern: %v", err)
    }

    (".", func(path string, info , err error) error {
        if (()) {
            newName := ((), replacement)
            newPath := ((path), newName)
            ("Renaming %s to %s\n", path, newPath)
            return (path, newPath)
        }
        return nil
    })
}

Example of usage

# Rename all .txt files to .mdgo run  rename -pattern="\.txt$" -replace=".md"

3. Batch deletion

func deleteFiles(pattern string) {
    regex, err := (pattern)
    if err != nil {
        ("Invalid regex pattern: %v", err)
    }

    (".", func(path string, info , err error) error {
        if (()) {
            ("Deleting %s\n", path)
            return (path)
        }
        return nil
    })
}

Example of usage

# Delete all .log filesgo run  delete -pattern="\.log$"

4. Create a file/directory

func createFileOrDir(path string, isDir bool) {
    if isDir {
        err := (path, 0755)
        if err != nil {
            ("Error creating directory: %v", err)
        }
        ("Created directory: %s\n", path)
    } else {
        file, err := (path)
        if err != nil {
            ("Error creating file: %v", err)
        }
        defer ()
        ("Created file: %s\n", path)
    }
}

Example of usage

# Create a directorygo run  create -path="new_dir" -dir

# Create a filego run  create -path="new_file.txt"

5. Batch Move

func moveFiles(sourcePattern, targetDir string) {
    regex, err := (sourcePattern)
    if err != nil {
        ("Invalid regex pattern: %v", err)
    }

    (targetDir, 0755)

    (".", func(path string, info , err error) error {
        if (()) {
            newPath := (targetDir, ())
            ("Moving %s to %s\n", path, newPath)
            return (path, newPath)
        }
        return nil
    })
}

Example of usage

# Move all .jpg files to images directorygo run  move -source="\.jpg$" -target="images"

3. How to install and use

Install Go environment(Refer to the previous tutorial)

Compile and run

go build -o file-manager
./file-manager [command] [flags]

The above is the detailed content of using Go language to develop a command line file management tool. For more information about Go file management tools, please pay attention to my other related articles!