Create a Golang program to remove temporary files
In this tutorial, we will use Go language to write a program to remove temporary files from your PC.
We will create a Golang program that traverse through all files and directories in the specified root directory and lists all empty file's path as output. Let's begin with importing the required packages for this project.
Import required package from the Go standard library
fmt
- Implement formatted input output in Go.
io/fs
- This is used to read file system (multi platform support).
io/ioutil
- impliments package input output utility functions.
log
- This package is used to add simple logging feature to your code.
os
- To read HTML files from standard input (stdin).
package main
import (
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
)
Declare a function which takes a path as input and returns list of files and directories in that path:
// listDirContents reads the contents of a directory and returns a list of new paths and files
func listDirContents(path string) ([]string, []fs.FileInfo) {
}
Read the content of the path specified.
// listDirContents reads the contents of a directory and returns a list of new paths and files
func listDirContents(path string) ([]string, []fs.FileInfo) {
// Reads the contents (files) of a directory.
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
}
Initialize an array and a for loop to append all files in that array.
// listDirContents reads the contents of a directory and returns a list of new paths and files
func listDirContents(path string) ([]string, []fs.FileInfo) {
// Reads the contents (files) of a directory.
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
// Creates a new path []string and initializes it.
newPaths := make([]string, 0)
// Generates a list of paths for all files.
for _, f := range files {
}
}
Append the files to the array and return the name and files.
// listDirContents reads the contents of a directory and returns a list of new paths and files
func listDirContents(path string) ([]string, []fs.FileInfo) {
// Reads the contents (files) of a directory.
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
// Creates a new path []string and initializes it.
newPaths := make([]string, 0)
// Generates a list of paths for all files.
for _, f := range files {
var newPath string
newPath = fmt.Sprintf("%s%s", path, f.Name())
newPaths = append(newPaths, newPath)
}
return newPaths, files
}
Declare the main function and handle path specified as argument or return a message if a path is not provided in the arguments.
func main() {
// Read directories from argument
args := os.Args[1:]
if len(args) == 0 {
fmt.Println("Specify a directory as argument.")
return //stop further execution of script
}
}
Call the function to list all files in the path and append empty files to an array.
func main() {
// Read directories from argument
args := os.Args[1:]
if len(args) == 0 {
fmt.Println("Specify a directory as argument.")
return //stop further execution of script
}
names, files := listDirContents(args[0])
// count total space used by temp file on the system
var ss int
for _, file := range files {
if file.Size() == 0 {
ss += len(file.Name()) + 1
}
}
}
Print the result to standard output.
func main() {
// Read directories from argument
args := os.Args[1:]
if len(args) == 0 {
fmt.Println("Specify a directory as argument.")
return //stop further execution of script
}
names, files := listDirContents(args[0])
// count total space used by temp file on the system
var ss int
for _, file := range files {
if file.Size() == 0 {
ss += len(file.Name()) + 1
}
}
// Prints the storage space that can be freed.
fmt.Printf("Storage space that can be freed: %d bytes.\n\n", ss)
fmt.Printf("%s", names)
}
Finally, we can place all pieces together to get a working script which lists all empty files in specified path.
package main
import (
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
)
// listDirContents reads the contents of a directory and returns a list of new paths and files
func listDirContents(path string) ([]string, []fs.FileInfo) {
// Reads the contents (files) of a directory.
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
// Creates a new path []string and initializes it.
newPaths := make([]string, 0)
// Generates a list of paths for all files.
for _, f := range files {
var newPath string
newPath = fmt.Sprintf("%s%s", path, f.Name())
newPaths = append(newPaths, newPath)
}
return newPaths, files
}
func main() {
// Read directories from argument
args := os.Args[1:]
if len(args) == 0 {
fmt.Println("Specify a directory as argument.")
return //stop further execution of script
}
names, files := listDirContents(args[0])
// count total space used by temp file on the system
var ss int
for _, file := range files {
if file.Size() == 0 {
ss += len(file.Name()) + 1
}
}
// Prints the storage space that can be freed.
fmt.Printf("Storage space that can be freed: %d bytes.\n\n", ss)
fmt.Printf("%s", names)
}
Conclusion
In this tutorial, we used standard library packages available in Golang to create a program which scrapes all emty files and prints them on terminal.