help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
go-cli-tool
/
internal
/
fileops
RSK World
go-cli-tool
Go CLI Tool v3.0 - Command-Line Interface with File Operations + Text Processing + System Monitoring + Cryptographic Tools + Network Utilities + HTTP Client + Image Processing + Cross-Platform Support + Modern Go Development
fileops
  • files.go4.3 KB
files.go
internal/fileops/files.go
Raw Download
Find: Go to:
/*
 * Project: Go CLI Tool
 * Category: Go Projects
 * Description: Command-line interface tool built with Go. Features file operations, text processing, command parsing, and system utilities.
 * Author: RSK World
 * Email: support@rskworld.in
 * Website: https://rskworld.in/contact.php
 * Year: 2026
 * Copyright (c) 2026 RSK World. All rights reserved.
 */

package fileops

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"go-cli-tool/internal/ui"
)

// ReadFile reads the content of a file and prints it
func ReadFile(filename string) error {
	content, err := os.ReadFile(filename)
	if err != nil {
		return fmt.Errorf("failed to read file: %v", err)
	}
	fmt.Printf("Content of %s:\n%s\n", filename, content)
	return nil
}

// CreateFile creates a new file with the given content
func CreateFile(filename, content string) error {
	err := os.WriteFile(filename, []byte(content), 0644)
	if err != nil {
		return fmt.Errorf("failed to create file: %v", err)
	}
	fmt.Printf("Successfully created file: %s\n", filename)
	return nil
}

// DeleteFile deletes the specified file
func DeleteFile(filename string) error {
	err := os.Remove(filename)
	if err != nil {
		return fmt.Errorf("failed to delete file: %v", err)
	}
	fmt.Printf("Successfully deleted file: %s\n", filename)
	return nil
}

// ListFiles lists all files in the directory
func ListFiles(dir string) error {
	files, err := os.ReadDir(dir)
	if err != nil {
		return fmt.Errorf("failed to list files: %v", err)
	}

	fmt.Printf("Files in %s:\n", dir)
	for _, file := range files {
		info, err := file.Info()
		if err != nil {
			continue
		}
		fmt.Printf("- %s (Size: %d bytes)\n", file.Name(), info.Size())
	}
	return nil
}

// CopyFile copies a file from src to dst.
// It reads the source file and writes it to the destination file.
func CopyFile(src, dst string) error {
	input, err := os.ReadFile(src)
	if err != nil {
		return fmt.Errorf("failed to read source file: %v", err)
	}

	err = os.WriteFile(dst, input, 0644)
	if err != nil {
		return fmt.Errorf("failed to write destination file: %v", err)
	}

	fmt.Printf("Successfully copied %s to %s\n", src, dst)
	return nil
}

// MoveFile moves a file from src to dst.
// It achieves this by renaming the file (which is an atomic operation on most systems).
func MoveFile(src, dst string) error {
	err := os.Rename(src, dst)
	if err != nil {
		return fmt.Errorf("failed to move file: %v", err)
	}

	fmt.Printf("Successfully moved %s to %s\n", src, dst)
	return nil
}

// SearchFiles recursively searches for files containing the given text in a directory.
func SearchFiles(dir, searchText string) error {
	if searchText == "" {
		return fmt.Errorf("search text cannot be empty")
	}

	ui.Info(fmt.Sprintf("Searching for '%s' in %s", searchText, dir))
	found := false

	err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
		if err != nil {
			return nil // Skip inaccessible files
		}

		if d.IsDir() {
			return nil // Skip directories
		}

		// Read file content
		content, err := os.ReadFile(path)
		if err != nil {
			return nil // Skip files that can't be read
		}

		// Check if file contains search text (case-insensitive)
		if strings.Contains(strings.ToLower(string(content)), strings.ToLower(searchText)) {
			if !found {
				ui.Header("SEARCH RESULTS")
				found = true
			}
			fmt.Printf("Found in: %s\n", path)
		}

		return nil
	})

	if err != nil {
		return fmt.Errorf("failed to search files: %v", err)
	}

	if !found {
		ui.Info("No files found containing the search text")
	} else {
		ui.Success("Search completed")
	}

	return nil
}

// GetFileStats shows detailed statistics about a file.
func GetFileStats(filename string) error {
	info, err := os.Stat(filename)
	if err != nil {
		return fmt.Errorf("failed to get file stats: %v", err)
	}

	ui.Header("FILE STATISTICS")
	fmt.Printf("Name: %s\n", info.Name())
	fmt.Printf("Size: %d bytes (%.2f KB, %.2f MB)\n",
		info.Size(), float64(info.Size())/1024, float64(info.Size())/1024/1024)
	fmt.Printf("Permissions: %s\n", info.Mode().String())
	fmt.Printf("Modified: %s\n", info.ModTime().Format("2006-01-02 15:04:05"))
	fmt.Printf("Is Directory: %t\n", info.IsDir())

	return nil
}
166 lines•4.3 KB
go

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer