Skip to content

Instantly share code, notes, and snippets.

@Parkham
Created January 20, 2017 20:57
Show Gist options
  • Save Parkham/17563420337e5191fc06725181edc694 to your computer and use it in GitHub Desktop.
Save Parkham/17563420337e5191fc06725181edc694 to your computer and use it in GitHub Desktop.
Golang 1.8 - Windows - Find, replace output modified files in directory *Learning Project, in progress*
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
)
func main() {
t := time.Now()
if len(os.Args) > 5 {
fmt.Println("\n")
fmt.Println("Too many arguments\n")
fmt.Println("-------------------------------------------------------------------------------\n")
fmt.Println(" FAR :: Find and Replace for Windows\n")
fmt.Println("-------------------------------------------------------------------------------\n")
fmt.Println(" Started : ", t.Local())
fmt.Println("\n")
fmt.Printf(" Required Usage :: FAR file_extension output_prefix ")
fmt.Printf("%#v", "search text")
fmt.Printf(" ")
fmt.Printf("%#v\n", "replace text")
fmt.Println("\n")
fmt.Println(" file_extension :: the file extension of files to search for. Example: .txt")
fmt.Println(" output_prefix :: the prefix appended to all modified files. Example: mod_")
fmt.Printf(" ")
fmt.Printf("%#v", "search text")
fmt.Printf(" ")
fmt.Println(":: The 'find' text. Use quotes for phrases.")
fmt.Printf(" ")
fmt.Printf("%#v", "replace text")
fmt.Printf(" ")
fmt.Println(":: The 'replace' with text. Use quotes for phrases.")
fmt.Println("-------------------------------------------------------------------------------\n")
os.Exit(1)
}
if len(os.Args) < 5 {
fmt.Println("\n")
fmt.Println("Too few arguments\n")
fmt.Println("-------------------------------------------------------------------------------\n")
fmt.Println(" FAR :: Find and Replace for Windows\n")
fmt.Println("-------------------------------------------------------------------------------\n")
fmt.Println(" Started : ", t.Local())
fmt.Println("\n")
fmt.Printf(" Required Usage :: FAR file_extension output_prefix ")
fmt.Printf("%#v", "search text")
fmt.Printf(" ")
fmt.Printf("%#v\n", "replace text")
fmt.Println("\n")
fmt.Println(" file_extension :: the file extension of files to search for. Example: .txt")
fmt.Println(" output_prefix :: the prefix appended to all modified files. Example: mod_")
fmt.Printf(" ")
fmt.Printf("%#v", "search text")
fmt.Printf(" ")
fmt.Println(":: The 'find' text. Use quotes for phrases.")
fmt.Printf(" ")
fmt.Printf("%#v", "replace text")
fmt.Printf(" ")
fmt.Println(":: The 'replace' with text. Use quotes for phrases.")
fmt.Println("-------------------------------------------------------------------------------\n")
os.Exit(1)
}
fileExtension := os.Args[1]
fileOutputMod := os.Args[2]
textInput := os.Args[3]
textOutput := os.Args[4]
i := -1
dirname := "." + string(filepath.Separator)
d, err := os.Open(dirname)
if err != nil {
fmt.Println(err)
}
defer d.Close()
files, err := d.Readdir(-1)
if err != nil {
fmt.Println(err)
}
fileList := make([]string, 0)
for _, file := range files {
if file.Mode().IsRegular() {
if filepath.Ext(file.Name()) == fileExtension {
i++
fileList = append(fileList, file.Name())
}
}
}
fmt.Println("\n")
fmt.Println(i+1, "files detected")
for j := 0; j < i+1; j++ {
eachFile := (fileList[j])
input, err := ioutil.ReadFile(eachFile)
if err != nil {
fmt.Println(err)
}
output := bytes.Replace(input, []byte(textInput), []byte(textOutput), -1)
eachFileOut := eachFile
modFileOut := fileOutputMod + eachFileOut
if err = ioutil.WriteFile(modFileOut, output, 0666); err != nil {
fmt.Println(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment