Golang program to rename a file


In golang we can use internal function of OS package to rename a file in various ways. In this article we are going to demonstrate how to change a file name using os. create, os.rename and os.open functions.

Method1: Using os.Rename Function

In this example, the file oldname.txt is renamed to newname.txt using the Rename function. If the renaming does not occur, such as if the source file doesn't exist or if the destination file already exists, the function produces an error by panicking. The error will be nil if the renaming procedure is successful.

Syntax

os.Rename

os.Rename is the function of OS package and is used for renaming a particular file. It has only one parameter and that is the file name that is needed to be renamed.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package), os package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that function use os.Rename with two inputs the old file and the new file.

  • Step 3 − If no error appears it implies that the file is renamed successfully.

  • Step 4 − If error appears panic is created with an err.

Example

In this example we will use os package function like os.Rename to execute the program.

package main
import (
   "os"
)

func main() {
   err := os.Rename("oldname.txt", "newname.txt")  //rename the file
   if err != nil {
      panic(err)  //return the error with a panic
   }
}

Output

If the renaming is successful:
Nothing will be printed

If the renaming is not successful:
panic: open oldname.txt: no such file or directory

goroutine 1 [running]:
main.main()
   /tmp/sandbox3294159103/prog.go:13 +0x17b

Method 2: Using os.Open and os.Create Function

In this method we initially try to rename the file using os.Rename. As an alternative technique we use os.Open to open the original file and os.Create to create the new file. The original file's content is subsequently copied using Readdirnames to the new file. Finally, the new file and the original file are both closed using the Close command.

Syntax

Os.Create 

Create is a part of os package , this function creates a new file, it contains a single parameter i.e. the filename which is to be created.

os.Open

This function opens the given file. It contains a single parameter i.e the filename that needs to be accessed.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package), os package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a main function using the old filename and the desired new filename as parameters, call the os.Rename function.

  • Step 3 − Use the os.Open function to try opening the old file if any appears with the first technique.

  • Step 4 − If any error appears while opening the file using the second technique create a panic with an error and close the file using the defer keyword and close function.

  • Step 5 − Create a new file using the os.Create function and return an error with it comes while creating the new file.

  • Step 6 − Close the new file using defer keyword and close function.

  • Step 7 − In this Step, copy the contents of old file to new file using Readdirnames and check if an error comes return it by panicking.

Example

In this example, we will os package os.Open and os.Create functions to execute the program.

package main
import (
   "os"
)

func main() {
   err := os.Rename("oldname.txt", "newname.txt") //rename a file
   if err != nil {
      // Rename failed, try alternative method
      oldname, err := os.Open("oldname.txt") //use this function to open the file
      if err != nil {
         panic(err)
      }
      defer oldname.Close()

      newname, err := os.Create("newname.txt") //create a new file
      if err != nil {
         panic(err)
      }
      defer newname.Close()
      _, err = oldname.Readdirnames(0)
      if err != nil {
         panic(err)
      }
   }
}

Output

If the renaming is successful:
Nothing will be printed

If the renaming is not successful:
panic: open oldname.txt: no such file or directory

goroutine 1 [running]:
main.main()
/tmp/sandbox3294159103/prog.go:13 +0x17b

Conclusion

We executed the program of renaming a file using two methods. In both methods we used os package but with different functions. In the first method os.Rename function is used whereas in the second method os.Open and os.Create are used along with Rename to execute the program.

Updated on: 22-Feb-2023

297 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements