Go Program to Rename a File

Introduction

Renaming a file is a common file operation that involves changing the name or path of an existing file. In Go, this can be easily achieved using the os package. This guide will demonstrate how to write a Go program that renames a specified file.

Problem Statement

Create a Go program that:

  • Takes the current filename and the new filename as input.
  • Renames the file if it exists.
  • Displays a message indicating whether the file was successfully renamed or if an error occurred.

Example:

  • Input: Current filename "oldname.txt", New filename "newname.txt"
  • Output:
    File renamed successfully.
    

    or

    Error renaming file: file does not exist.
    

Solution Steps

  1. Import the Necessary Packages: Use import "fmt" and import "os" for file operations and formatted I/O.
  2. Write a Function to Rename the File: Implement a function that renames the file using os.Rename.
  3. Write the Main Function: Define the main function, which is the entry point of every Go program.
  4. Prompt the User to Enter the Current Filename and New Filename: Use fmt.Scanln or fmt.Scanf to take the filenames as input.
  5. Call the Function to Rename the File: Use the function to rename the specified file.
  6. Display the Result: Use fmt.Println to display whether the file was successfully renamed or if an error occurred.

Go Program

package main

import (
    "fmt"
    "os"
)

// Step 2: Implement a function to rename the file
func renameFile(oldName, newName string) error {
    // Use os.Rename to rename the file
    err := os.Rename(oldName, newName)
    if err != nil {
        return err
    }
    return nil
}

/**
 * Go Program to Rename a File
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 4: Prompt the user to enter the current and new filenames
    var oldName, newName string
    fmt.Print("Enter the current filename: ")
    fmt.Scanln(&oldName)
    fmt.Print("Enter the new filename: ")
    fmt.Scanln(&newName)

    // Step 5: Call the function to rename the file
    err := renameFile(oldName, newName)
    if err != nil {
        fmt.Println("Error renaming file:", err)
    } else {
        fmt.Println("File renamed successfully.")
    }
}

Explanation

Step 2: Implement a Function to Rename the File

  • The renameFile function uses the os.Rename function to rename the file from oldName to newName.
    • os.Rename attempts to rename the file. If the file does not exist or cannot be renamed for some reason, it returns an error.
    • If the file is successfully renamed, os.Rename returns nil.

Step 3: Write the Main Function

  • The main function prompts the user for the current filename and the new filename, then calls renameFile to rename the file.

Step 4: Prompt the User to Enter the Current Filename and New Filename

  • The program prompts the user to enter the current filename and the new filename using fmt.Print and reads the input using fmt.Scanln.

Step 5: Call the Function to Rename the File

  • The program calls renameFile with the provided filenames to rename the file.

Step 6: Display the Result

  • The program prints a message indicating whether the file was successfully renamed or if an error occurred using fmt.Println.

Output Example

Example 1:

Enter the current filename: oldname.txt
Enter the new filename: newname.txt
File renamed successfully.

Example 2:

Enter the current filename: nonexistentfile.txt
Enter the new filename: newfile.txt
Error renaming file: rename nonexistentfile.txt newfile.txt: no such file or directory

Example 3 (Empty input):

Enter the current filename: 
Enter the new filename: 
Error renaming file: rename : no such file or directory

Conclusion

This Go program demonstrates how to rename a file using the os package. It covers basic file operations such as file renaming and error handling in Go. This example is useful for beginners learning Go programming and understanding how to perform file manipulation tasks effectively.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top