1

Hey if have been working on this copy tool as an exercise since I am totally new to Golang and programming. So far it does work in the way i want but I am trying to do fine cuts on it. The Tool asks for a source path and my Code adds an "\" for comfort, but I want the program to ignore whenever I put an "\" at the end of the path or forget to do so. The program should add it by itself in case i forget and ignore in case if there already is one.

The code:

func main() {

    fmt.Println("Willkommen beim Rocon Copy-Tool." +
        "Wählen Sie bitte aus:")
    fmt.Println("Drücken Sie die 1 um einen Dateipfad auszuwählen")
    fmt.Println("Drücken Sie die 2 um das Programm zu beenden")

    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    input := scanner.Text()

    if input == "1" {


        fmt.Print("Geben Sie den gewünschten Dateipfad an: ")

        scanner = bufio.NewScanner(os.Stdin)
        scanner.Scan()
        srcPath := scanner.Text()

        fmt.Print("Geben Sie den gewünschten Dateinamen an: ")
        scanner.Scan()
        filename := scanner.Text()

        // Open the source file
        srcFile, err := os.Open(srcPath+ "\\" + filename)
        if err != nil {
            log.Fatal(err)
        }
        defer srcFile.Close()

        // Create the destination file
        fmt.Print("Geben Sie den gewünschten Speicherort an: ")
        scanner.Scan()
        scanner.Text()
        dstPath := scanner.Text()

        dstFile, err := os.Create(dstPath + filename)
        if err != nil {
            log.Fatal(err)
        }
        defer dstFile.Close()

        // Copy the file
        _, err = io.Copy(dstFile, srcFile)
    }
}

I hope the German query don't bother while reading it.

Hector Correa
  • 26,290
  • 8
  • 57
  • 73
reX
  • 15
  • 2
  • 2
    To avoid discarding data buffered in a scanner, create and reuse one bufio.Scanner for stdin. – Charlie Tumahai Mar 11 '21 at 13:19
  • 2
    Please never use bare string manipulation (like `+`) on pathnames; use the `path/filepath` package. – kostix Mar 11 '21 at 13:22
  • 1
    I don't think I'm able to parse the question (your English is fine): you tell about the "" added at the end (of a pathname), but that's an empty string, and adding an empty string to an existing string is a no-op: the source string is unchanged. If you _literally_ meant adding two "double quote" characters, I fail to see from the source code how that can ever happen. Care to elaborate please? – kostix Mar 11 '21 at 13:24
  • Im sorry my bad, I meant to write a \ into the quotas. Its just for example when giving a source path (e.g. C:\Users\me\Desktop -> C:\Users\me\Desktop\ ) to add a backslash at the end of it so I can use the filename i want to copy in the next step without writing a backslash in front of it (\file.txt -> file.txt) – reX Mar 11 '21 at 13:35

1 Answers1

0

As kostix indicated I think filepath.Join is your best bet for this.

This line:

srcFile, err := os.Open(srcPath+ "\\" + filename)

Would become:

srcFile, err := os.Open(filepath.Join(srcPath, filename))

(you'll need to import "path/filepath")

You can do the same for the the destination file.

Hector Correa
  • 26,290
  • 8
  • 57
  • 73