files

package module
v0.2.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2026 License: MIT Imports: 17 Imported by: 0

README

Files - Go File Operations Library

Go Version Test Status Lint Status License Go Report Card

A comprehensive Go library for file operations, filtering, and file type detection.

Features

  • File Operations: Copy, move, delete, and concatenate files
  • Directory Operations: Copy directories recursively, calculate directory statistics
  • File Filtering: Flexible filters by name, extension, size, date, and regex
  • Directory Filtering: Filter directories by name, size, and file count
  • File Type Detection: Comprehensive file description with MIME types for 280+ extensions
  • Archive Support: Extract and create ZIP and TAR archives
  • Hash Functions: Calculate MD5, SHA1, SHA256, and SHA512 file hashes
  • Cross-platform: Windows and Unix compatible

Installation

go get github.com/alex-cos/files

Quick Start

package main

import (
    "fmt"
    "github.com/alex-cos/files"
)

func main() {
    // Get file information
    desc, exists := files.GetFileDesc("pdf")
    if exists {
        fmt.Printf("Extension: %s, MIME: %s\n", desc.Name, desc.MimeType)
    }

    // Copy a file
    err := files.CopyFile("source.txt", "destination.txt")

    // List files with filters
    files, err := files.ListFiles(".", func(f *files.FileInfo) bool {
        return files.FilterFileByExt("go")(f)
    })
}

File Operations

Copy Files
// Copy a single file
err := files.CopyFile("source.txt", "destination.txt")

// Copy to a directory (keeps filename)
err := files.CopyFile("file.txt", "/path/to/directory")

// Copy entire directory
err := files.CopyDir("/source/dir", "/destination/dir")
Move Files
// Move a single file
err := files.MoveFile("source.txt", "destination.txt")

// Move to a directory (keeps filename)
err := files.MoveFile("file.txt", "/path/to/directory")

// Move entire directory
err := files.MoveDir("/source/dir", "/destination/dir")
Delete Files
// Delete a single file
err := files.DeleteFile("file.txt")

// Delete a directory and all contents
err := files.DeleteDir("/path/to/directory")
Concatenate Files
// Concatenate multiple files into one
err := files.ConcatFiles([]string{"file1.txt", "file2.txt"}, "combined.txt", 0644)

// Concatenate all files of same type in a directory
err := files.ConcatDir("/source/dir", "all.txt", files.FilterFileByExt("log"), 0644)
Hash Files
// Calculate MD5 hash
hash, err := files.FileHash("file.txt", files.MD5)

// Calculate SHA256 hash
hash, err := files.FileHash("file.txt", files.SHA256)
Directory Statistics
stats, err := files.GetDirStats("/path/to/dir")
fmt.Printf("Total files: %d, Total size: %s\n", stats.TotalFiles, stats.FormatSize())

Filtering

File Filters
// Filter by extension
filter := files.FilterFileByExt("go")

// Filter by name
filter := files.FilterFileByName("README.md")

// Filter by size (greater than)
filter := files.FilterFileBySizeGreater(1024 * 1024)

// Filter by date (updated after)
filter := files.FilterFileByUpdatedAfter(time.Now().AddDate(0, 0, -7))

// Filter by regex
filter := files.FilterFileByRegEx(*regexp.MustCompile(`^test.*\.txt$`))

// Filter by category
filter := files.FilterFileByCategory(files.CategoryCode)

// Filter by MIME type
filter := files.FilterFileByMimeType(files.MimeJSON)

// Combine filters
combined := func(f *files.FileInfo) bool {
    return files.FilterFileByExt("go")(f) && files.FilterFileBySizeGreater(100)(f)
}
Directory Filters
// Filter by name
filter := files.FilterDirByName("node_modules")

// Filter by size
filter := files.FilterDirBySizeGreater(1024 * 1024 * 100)

// Filter by file count
filter := files.FilterDirByNbFilesGreater(10)

Listing Files

// List all files in a directory
files, err := files.ListFiles("/path/to/dir", nil)

// List files with filter
txtFiles, err := files.ListFiles("/path/to/dir", files.FilterFileByExt("txt"))

// List directories
dirs, err := files.ListDirs("/path/to/parent", nil)

Archive Operations

ZIP Archives
// Create a ZIP archive
err := files.CreateZip("/output/archive.zip", "/source/dir")

// Extract a ZIP archive
err := files.ExtractZip("/archive.zip", "/output/dir")
TAR Archives
// Create a TAR archive
err := files.CreateTar("/output/archive.tar", "/source/dir")

// Create a gzip-compressed TAR archive
err := files.CreateTarGz("/output/archive.tar.gz", "/source/dir")

// Extract a TAR archive
err := files.ExtractTar("/archive.tar", "/output/dir")

// Extract a gzip-compressed TAR archive
err := files.ExtractTarGz("/archive.tar.gz", "/output/dir")

File Descriptions

Get detailed information about file types:

// Get file description
desc, exists := files.GetFileDesc("pdf")
if exists {
    fmt.Printf("Name: %s\n", desc.Name)
    fmt.Printf("Category: %s\n", desc.Category)
    fmt.Printf("MIME Type: %s\n", desc.MimeType)
    fmt.Printf("Binary: %v\n", desc.IsBinary)
    fmt.Printf("Compressed: %v\n", desc.IsCompressed)
}

// Get file name by extension
name := files.GetFileDescName("jpg") // "JPEG Image"

// Get file category
cat := files.GetFileDescCat("go") // "Code"

// Get MIME type
mime := files.GetMimeType("json") // "application/json"

// Check file properties
isBinary := files.IsFileBinary("pdf")   // *bool
isCompressed := files.IsFileCompressed("zip") // *bool

Constants

Hash Algorithms
files.MD5
files.SHA1
files.SHA256
files.SHA512
File Categories
files.CategoryDocument
files.CategorySpreadsheet
files.CategoryPresentation
files.CategoryImage
files.CategoryAudio
files.CategoryVideo
files.CategoryArchive
files.CategoryExecutable
files.CategoryCode
files.CategoryConfig
files.CategoryData
files.CategoryDatabase
files.CategorySecurity
files.CategoryFont
files.Category3D
files.CategoryEbook
files.CategoryScientist
files.CategoryDiskImage
files.CategoryLog
files.CategoryTemp
files.CategoryOther
files.Unknown

Data Types

DirInfo
type DirInfo struct {
    Path    string
    Name    string
    Nbfiles int64
    Size    int64
}
FileInfo
type FileInfo struct {
    Path    string
    Name    string
    Ext     string
    Size    int64
    Created time.Time
    Updated time.Time
}
DirStats
type DirStats struct {
    TotalFiles  int64
    TotalDirs   int64
    TotalSize   int64
    OldestFile  time.Time
    NewestFile  time.Time
    AverageSize int64
}
FileDesc
type FileDesc struct {
    Name         string
    Category     string
    MimeType     string
    IsBinary     bool
    IsCompressed bool
    IsExecutable bool
}

Documentation

Index

Constants

View Source
const (
	CategoryDocument     = "Document"
	CategorySpreadsheet  = "Spreadsheet"
	CategoryPresentation = "Presentation"
	CategoryImage        = "Image"
	CategoryAudio        = "Audio"
	CategoryVideo        = "Video"
	CategoryArchive      = "Archive"
	CategoryExecutable   = "Executable"
	CategoryCode         = "Code"
	CategoryConfig       = "Config"
	CategoryData         = "Data"
	CategoryDatabase     = "Database"
	CategorySecurity     = "Security"
	CategoryFont         = "Font"
	Category3D           = "3D"
	CategoryEbook        = "eBook"
	CategoryCloud        = "Cloud"
	CategoryAutomation   = "Automation"
	CategoryScientist    = "Scientist Data"
	CategoryDiskImage    = "Disk Image"
	CategoryLog          = "Log"
	CategoryTemp         = "Temporary"
	CategoryOther        = "Other"
)
View Source
const (
	// Document.
	DOC  string = "doc"
	DOCX string = "docx"
	DOT  string = "dot"
	DOTX string = "dotx"
	DOTM string = "dotm"
	ODT  string = "odt"
	OTT  string = "ott"
	PDF  string = "pdf"
	TXT  string = "txt"
	RTF  string = "rtf"
	MD   string = "md"
	OXPS string = "oxps"
	XPS  string = "xps"
	WP   string = "wp"
	WPD  string = "wpd"

	// Spreadsheets.
	XLS  string = "xls"
	XLSX string = "xlsx"
	XLSM string = "xlsm"
	XLT  string = "xlt"
	XLTX string = "xltx"
	ODS  string = "ods"
	SXC  string = "sxc"
	CSV  string = "csv"
	TSV  string = "tsv"

	// Presentation.
	PPT  string = "ppt"
	PPTX string = "pptx"
	PPTM string = "pptm"
	POT  string = "pot"
	POTX string = "potx"
	PPS  string = "pps"
	PPSX string = "ppsx"
	ODP  string = "odp"

	// Image.
	JPG    string = "jpg"
	JPEG   string = "jpeg"
	PNG    string = "png"
	GIF    string = "gif"
	BMP    string = "bmp"
	TIFF   string = "tiff"
	TIF    string = "tif"
	SVG    string = "svg"
	WEBP   string = "webp"
	ICO    string = "ico"
	HEIC   string = "heic"
	HEIF   string = "heif"
	RAW    string = "raw"
	CR2    string = "cr2"
	NEF    string = "nef"
	ARW    string = "arw"
	PSD    string = "psd"
	AI     string = "ai"
	EPS    string = "eps"
	INDD   string = "indd"
	SKETCH string = "sketch"
	FIGMA  string = "fig"

	// Audio.
	MP3  string = "mp3"
	WAV  string = "wav"
	FLAC string = "flac"
	AAC  string = "aac"
	OGG  string = "ogg"
	WMA  string = "wma"
	M4A  string = "m4a"
	AIFF string = "aiff"
	ALAC string = "alac"
	OPUS string = "opus"
	MID  string = "mid"
	MIDI string = "midi"

	// Video.
	MP4      string = "mp4"
	AVI      string = "avi"
	MKV      string = "mkv"
	MOV      string = "mov"
	WMV      string = "wmv"
	FLV      string = "flv"
	WEBM     string = "webm"
	M4V      string = "m4v"
	MPEG     string = "mpeg"
	MPG      string = "mpg"
	THREEGP  string = "3gp"
	THREEG2  string = "3g2"
	M2TS     string = "m2ts"
	MTS      string = "mts"
	VOB      string = "vob"
	TS_VIDEO string = "ts_video"

	// Archive/Compressed.
	ZIP     string = "zip"
	RAR     string = "rar"
	TAR     string = "tar"
	GZ      string = "gz"
	GZIP    string = "gzip"
	SEVENZ  string = "7z"
	BZ2     string = "bz2"
	XZ      string = "xz"
	TGZ     string = "tgz"
	TAR_GZ  string = "tar.gz"
	TAR_BZ2 string = "tar.bz2"
	TAR_XZ  string = "tar.xz"
	ZST     string = "zst"
	LZ4     string = "lz4"
	LZH     string = "lzh"
	CAB     string = "cab"
	ISO     string = "iso"

	// Executable/System.
	EXE         string = "exe"
	MSI         string = "msi"
	SHELLSCRIPT string = "sh"
	BASH        string = "bash"
	ZSH         string = "zsh"
	FISH        string = "fish"
	BAT         string = "bat"
	CMD         string = "cmd"
	PS1         string = "ps1"
	APP         string = "app"
	DMG         string = "dmg"
	DEB         string = "deb"
	RPM         string = "rpm"
	APPIMAGE    string = "appimage"

	// Code - Web.
	HTML   string = "html"
	HTM    string = "htm"
	CSS    string = "css"
	SCSS   string = "scss"
	SASS   string = "sass"
	LESS   string = "less"
	JS     string = "js"
	JSX    string = "jsx"
	TS     string = "ts"
	TSX    string = "tsx"
	VUE    string = "vue"
	SVELTE string = "svelte"
	SLIM   string = "slim"
	ERB    string = "erb"

	// Code - Languages.
	PY        string = "py"
	PYW       string = "pyw"
	GO        string = "go"
	CPP       string = "cpp"
	CC        string = "cc"
	C         string = "c"
	H         string = "h"
	HH        string = "hh"
	HPP       string = "hpp"
	JAVA      string = "java"
	CLASS     string = "class"
	KT        string = "kt"
	KTS       string = "kts"
	SCALA     string = "scala"
	SWIFT     string = "swift"
	RB        string = "rb"
	RUBY      string = "ruby"
	PHP       string = "php"
	RUST      string = "rust"
	DART      string = "dart"
	LUA       string = "lua"
	PERL      string = "pl"
	PM        string = "pm"
	RMARKDOWN string = "rmd"

	// Code - Config/Build.
	JSON       string = "json"
	JSON5      string = "json5"
	JSONC      string = "jsonc"
	XML        string = "xml"
	YAML       string = "yaml"
	YML        string = "yml"
	TOML       string = "toml"
	INI        string = "ini"
	CONF       string = "conf"
	CONFIG     string = "config"
	ENV        string = "env"
	PROPERTIES string = "properties"
	PROPS      string = "props"
	GRADLE     string = "gradle"
	GRADLE_KTS string = "gradle.kts"
	MAKEFILE   string = "makefile"
	CMAKE      string = "cmake"
	LOCK       string = "lock"
	PACKAGE    string = "package"
	HTACCESS   string = "htaccess"

	// Code - Data format.
	SQL     string = "sql"
	GRAPHQL string = "graphql"
	GQL     string = "gql"
	MDX     string = "mdx"

	// Database.
	DB      string = "db"
	SQLITE  string = "sqlite"
	SQLITE3 string = "sqlite3"
	DB3     string = "db3"

	// Certificate/Security.
	PEM string = "pem"
	CRT string = "crt"
	CER string = "cer"
	DER string = "der"
	KEY string = "key"
	CSR string = "csr"
	P12 string = "p12"
	PFX string = "pfx"
	P7B string = "p7b"
	P7C string = "p7c"
	JKS string = "jks"
	GPG string = "gpg"
	PGP string = "pgp"
	SRL string = "srl"
	CRL string = "crl"

	// Font.
	TTF   string = "ttf"
	OTF   string = "otf"
	WOFF  string = "woff"
	WOFF2 string = "woff2"
	EOT   string = "eot"

	// 3D/CAD.
	OBJ      string = "obj"
	FBX      string = "fbx"
	STL      string = "stl"
	DWG      string = "dwg"
	DXF      string = "dxf"
	BLEND    string = "blend"
	MAX3D    string = "max"
	THREEDS  string = "3ds"
	IGES     string = "iges"
	IGS      string = "igs"
	STEP     string = "step"
	STP      string = "stp"
	IPT      string = "ipt"
	IAM      string = "iam"
	SLDDRPRT string = "sldprt"
	SLDDRASM string = "slddrw"

	// eBook.
	EPUB string = "epub"
	MOBI string = "mobi"
	AZW  string = "azw"
	AZW3 string = "azw3"
	LIT  string = "lit"
	PRC  string = "prc"
	FB2  string = "fb2"

	// DevOps/Cloud.
	TF         string = "tf"
	TERRAFORM  string = "terraform"
	ANSIBLE    string = "ansible"
	DOCKERFILE string = "dockerfile"
	DOCKER     string = "docker"
	KUBERNETES string = "k8s"
	BZL        string = "bzl"

	// Script/Automation.
	BAKE     string = "rake"
	FABRIC   string = "fabric"
	FASTLANE string = "fastlane"

	// Scientist Data.
	NC     string = "nc"
	NETCDF string = "netcdf"
	HDF5   string = "h5"
	HDF    string = "hdf"
	FITS   string = "fits"

	// Virtual Machine/Disk Image.
	IMG  string = "img"
	VDI  string = "vdi"
	VMDK string = "vmdk"
	VHD  string = "vhd"
	VHDX string = "vhdx"

	// Log/temp.
	LOG      string = "log"
	LOGS     string = "logs"
	LOCKFILE string = "lockfile"
	PID      string = "pid"
	DUMP     string = "dump"
	TMP      string = "tmp"
	TEMP     string = "temp"
	BAK      string = "bak"
	OLD      string = "old"
	CACHE    string = "cache"

	// Other.
	TORRENT string = "torrent"
	PART    string = "part"
	DESKTOP string = "desktop"
	LNK     string = "lnk"
)
View Source
const (
	MD5    string = "md5"
	SHA1   string = "sha1"
	SHA256 string = "sha256"
	SHA512 string = "sha512"
)

Hash algorithms.

View Source
const (
	MimePlain       = "text/plain"
	MimePowerPoint  = "application/vnd.ms-powerpoint"
	MimeGzip        = "application/gzip"
	MimePython      = "text/x-python"
	MimeJSON        = "application/json"
	MimeX509Cert    = "application/x-x509-ca-cert"
	MimeOctetStream = "application/octet-stream"
)
View Source
const Unknown = "Unknown"

Variables

View Source
var (
	// ErrFileNotFound is returned when a file does not exist.
	ErrFileNotFound = errors.New("file not found")

	// ErrDirNotFound is returned when a directory does not exist.
	ErrDirNotFound = errors.New("directory not found")

	// ErrFileIsDir is returned when a file path points to a directory.
	ErrFileIsDir = errors.New("path is a directory")

	// ErrDirIsFile is returned when a directory path points to a file.
	ErrDirIsFile = errors.New("path is a file")

	// ErrFileAlreadyExist is returned when a file already exist.
	ErrFileAlreadyExist = errors.New("file already exist")

	// ErrEmptySource is returned when no source files are provided.
	ErrEmptySource = errors.New("no source files provided")

	// ErrPermissionDenied is returned when file access is denied.
	ErrPermissionDenied = errors.New("permission denied")

	// ErrPathIsTainted is returned when a path is tainted.
	ErrPathIsTainted = errors.New("filepath is tainted")
)
View Source
var (

	// FilterDirByName returns a filter that matches directories by exact name.
	FilterDirByName = func(name string) FilterDir {
		return func(f *DirInfo) bool {
			return name == f.Name
		}
	}

	// FilterDirByRegEx returns a filter that matches directories by regex pattern.
	FilterDirByRegEx = func(regex regexp.Regexp) FilterDir {
		return func(f *DirInfo) bool {
			return regex.MatchString(f.Name)
		}
	}

	// FilterDirBySizeGreater returns a filter that matches directories larger than the given size.
	FilterDirBySizeGreater = func(size int64) FilterDir {
		return func(f *DirInfo) bool {
			return f.Size > size
		}
	}

	// FilterDirBySizeLower returns a filter that matches directories smaller than the given size.
	FilterDirBySizeLower = func(size int64) FilterDir {
		return func(f *DirInfo) bool {
			return f.Size < size
		}
	}

	// FilterDirByNbFilesGreater returns a filter that matches directories with more files than the given count.
	FilterDirByNbFilesGreater = func(nb int64) FilterDir {
		return func(f *DirInfo) bool {
			return f.Nbfiles > nb
		}
	}

	// FilterDirByNbFilesLower returns a filter that matches directories with fewer files than the given count.
	FilterDirByNbFilesLower = func(nb int64) FilterDir {
		return func(f *DirInfo) bool {
			return f.Nbfiles < nb
		}
	}

	// FilterDirByNbFilesEqual returns a filter that matches directories with exactly the given number of files.
	FilterDirByNbFilesEqual = func(nb int64) FilterDir {
		return func(f *DirInfo) bool {
			return f.Nbfiles == nb
		}
	}

	// FilterFileByName returns a filter that matches files by exact name.
	FilterFileByName = func(name string) FilterFile {
		return func(f *FileInfo) bool {
			return name == f.Name
		}
	}

	// FilterFileByRegEx returns a filter that matches files by regex pattern.
	FilterFileByRegEx = func(regex regexp.Regexp) FilterFile {
		return func(f *FileInfo) bool {
			return regex.MatchString(f.Name)
		}
	}

	// FilterFileByExt returns a filter that matches files by extension.
	FilterFileByExt = func(ext string) FilterFile {
		return func(f *FileInfo) bool {
			return strings.ToLower(ext) == f.GetExt()
		}
	}

	// FilterFileByCategory returns a filter that matches files by category.
	FilterFileByCategory = func(category string) FilterFile {
		return func(f *FileInfo) bool {
			ext := f.GetExt()
			if cat := GetFileDescCat(ext); cat != Unknown {
				return strings.EqualFold(cat, category)
			}
			return false
		}
	}

	// FilterFileByMimeType returns a filter that matches files by MIME type.
	FilterFileByMimeType = func(mimeType string) FilterFile {
		return func(f *FileInfo) bool {
			ext := f.GetExt()
			return GetMimeType(ext) == mimeType
		}
	}

	// FilterFileBySizeGreater returns a filter that matches files larger than the given size.
	FilterFileBySizeGreater = func(size int64) FilterFile {
		return func(f *FileInfo) bool {
			return f.Size > size
		}
	}

	// FilterFileBySizeLower returns a filter that matches files smaller than the given size.
	FilterFileBySizeLower = func(size int64) FilterFile {
		return func(f *FileInfo) bool {
			return f.Size < size
		}
	}

	// FilterFileByCreatedAfter returns a filter that matches files created after the given time.
	FilterFileByCreatedAfter = func(datetime time.Time) FilterFile {
		return func(f *FileInfo) bool {
			return f.Created.UnixNano() > datetime.UnixNano()
		}
	}

	// FilterFileByCreatedBefore returns a filter that matches files created before the given time.
	FilterFileByCreatedBefore = func(datetime time.Time) FilterFile {
		return func(f *FileInfo) bool {
			return f.Created.UnixNano() < datetime.UnixNano()
		}
	}

	// FilterFileByUpdatedAfter returns a filter that matches files updated after the given time.
	FilterFileByUpdatedAfter = func(datetime time.Time) FilterFile {
		return func(f *FileInfo) bool {
			return f.Updated.UnixNano() > datetime.UnixNano()
		}
	}

	// FilterFileByUpdatedBefore returns a filter that matches files updated before the given time.
	FilterFileByUpdatedBefore = func(datetime time.Time) FilterFile {
		return func(f *FileInfo) bool {
			return f.Updated.UnixNano() < datetime.UnixNano()
		}
	}
)

Functions

func ConcatDir

func ConcatDir(src, dst string, filter FilterFile, perm os.FileMode) error

ConcatDir concatenates all files matching the given filter in a source directory.

func ConcatFiles

func ConcatFiles(sources []string, dst string, perm os.FileMode) error

ConcatFiles concatenates all given files into one.

func CopyDir

func CopyDir(src, dst string) error

CopyDir copies the entire source directory and sub-directories to a destination directory.

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies a source file to a destination file or directory.

func DeleteDir

func DeleteDir(path string) error

DeleteDir removes a directory and all its contents recursively.

func DeleteFile

func DeleteFile(path string) error

DeleteFile removes a single file.

func FileHash

func FileHash(path, algo string) (string, error)

FileHash calculates the hash of a file using the specified algorithm (md5, sha1, sha256, sha512).

func FormatSize

func FormatSize(bytes int64) string

FormatSize formats a byte count into a human-readable string (e.g., "1.5 MB").

func GetAllExtensions

func GetAllExtensions() []string

func GetFileDescCat

func GetFileDescCat(ext string) string

GetFileDescCat returns the category for a given extension.

func GetFileDescName

func GetFileDescName(ext string) string

GetFileDescName returns the human-readable name for a given extension.

func GetMimeType

func GetMimeType(ext string) string

GetMimeType returns the MIME type for a given extension.

func IsFileBinary

func IsFileBinary(ext string) *bool

IsFileBinary returns whether a file with the given extension is binary.

func IsFileCompressed

func IsFileCompressed(ext string) *bool

IsFileCompressed returns whether a file with the given extension is typically compressed.

func IsFileExecutable

func IsFileExecutable(ext string) *bool

IsFileExecutable returns whether a file with the given extension is executable.

func MoveDir added in v0.2.5

func MoveDir(src, dst string) error

MoveDir moves a directory and all its contents to a destination directory.

func MoveFile added in v0.2.5

func MoveFile(src, dst string) error

MoveFile moves a file to a destination file or directory.

func TarAll

func TarAll(baseFolder, tarfile string) error

TarAll creates a TAR archive from a source folder.

func TarList

func TarList(tarfile string) ([]string, error)

TarList lists all files contained in a TAR archive.

func UnTar

func UnTar(tarfile, dest string) error

UnTar extracts a TAR archive to the destination directory.

func UnZip

func UnZip(zipfile, dest string) error

UnZip extracts a ZIP archive to the destination directory.

func ZipAll

func ZipAll(baseFolder, zipfile string) error

ZipAll creates a ZIP archive from a source folder.

func ZipList

func ZipList(zipfile string) ([]string, error)

ZipList lists all files contained in a ZIP archive.

Types

type DirInfo

type DirInfo struct {
	Path    string
	Name    string
	Nbfiles int64
	Size    int64
}

DirInfo represents information about a directory.

func ListDirs added in v0.2.2

func ListDirs(directory string, filter FilterDir) ([]*DirInfo, error)

ListDirs lists all directories recursively within the given directory. It returns a slice of DirInfo for each directory found. If a filter is provided, only directories matching the filter are returned.

func (*DirInfo) FormatSize

func (item *DirInfo) FormatSize() string

FormatSize returns a human-readable string representation of the directory size.

func (*DirInfo) String

func (item *DirInfo) String() string

String returns a string representation of the DirInfo.

type DirStats

type DirStats struct {
	TotalFiles  int64
	TotalDirs   int64
	TotalSize   int64
	OldestFile  time.Time
	NewestFile  time.Time
	AverageSize int64
}

DirStats represents statistics about a directory.

func GetDirStats

func GetDirStats(dir string) (*DirStats, error)

GetDirStats calculates statistics for a directory (total files, size, oldest/newest file).

func (*DirStats) FormatSize

func (s *DirStats) FormatSize() string

FormatSize returns a human-readable string representation of the total size.

type FileDesc

type FileDesc struct {
	Name         string
	Category     string
	MimeType     string
	IsBinary     bool
	IsCompressed bool
	IsExecutable bool
}

FileDesc describes a file type with its properties.

func GetFileDesc

func GetFileDesc(ext string) (FileDesc, bool)

GetFileDesc returns the file description for a given extension.

func (*FileDesc) String added in v0.2.2

func (item *FileDesc) String() string

String returns a string representation of the FileDesc.

type FileInfo

type FileInfo struct {
	Path    string
	Name    string
	Ext     string
	Size    int64
	Created time.Time
	Updated time.Time
}

FileInfo represents information about a file.

func ListFiles

func ListFiles(directory string, filter FilterFile) ([]*FileInfo, error)

ListFiles lists all files within the given directory. It returns a slice of FileInfo for each file found. If a filter is provided, only files matching the filter are returned.

func WalkFiles

func WalkFiles(directory string, filter FilterFile) ([]*FileInfo, error)

WalkFiles recursively walks through a directory and lists all files. It returns a slice of FileInfo for each file found. If a filter is provided, only files matching the filter are returned.

func (*FileInfo) FormatSize

func (item *FileInfo) FormatSize() string

FormatSize returns a human-readable string representation of the file size.

func (*FileInfo) GetExt

func (item *FileInfo) GetExt() string

GetExt returns the file extension in lowercase without the leading dot.

func (*FileInfo) String

func (item *FileInfo) String() string

String returns a string representation of the FileInfo.

type FilterDir

type FilterDir func(f *DirInfo) bool

FilterDir is a function type for filtering directories.

type FilterFile

type FilterFile func(f *FileInfo) bool

FilterFile is a function type for filtering files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL