Hello. I need help. In my code, the specified .txt file (with text) is copied in the specified path to the directory, but the code needs to be modified so that only those tapes that have a certain sequence of characters are copied to the new file. This can be done, I think, with Graph, but I don’t know how
I will be grateful for your help
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
func main() {
read := make([]byte, 0, 64)
var name, name2 string
var path string
fmt.Println("Set way to copy file: ")
fmt.Scan(&path)
fmt.Println("Set file name to copy: ")
fmt.Scan(&name)
wd, err := os.Getwd()
if err != nil {
fmt.Println("Error:", err)
return
}
os.Chdir(path)
file, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
fmt.Println(err)
}
read, err = ioutil.ReadFile(name)
defer file.Close()
os.Chdir(wd)
fmt.Println("Working Directory: ", wd)
fmt.Println("Application: ", filepath.Join(wd, os.Args[0]))
d := filepath.Join(wd, "Emperor")
if err := os.Mkdir(d, 0755); err != nil {
fmt.Println("Error:", err)
}
fmt.Println("Created ", d)
if err := os.Chdir(d); err != nil {
fmt.Println("Error:", err)
}
fmt.Println("Set name")
fmt.Scan(&name2)
file2, err := os.OpenFile(name2, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
fmt.Println("failed to copy the file")
fmt.Println(err)
}
file2.WriteString(string(read))
fmt.Println("New Working Directory: ", d)
}