41 lines
979 B
Go
41 lines
979 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
// MoveFile moves a file across file system boundaries
|
|
// taken from https://stackoverflow.com/questions/50740902/move-a-file-to-a-different-drive-with-go
|
|
func MoveFile(sourcePath, destPath string) error {
|
|
inputFile, err := os.Open(sourcePath)
|
|
if err != nil {
|
|
return fmt.Errorf("Couldn't open source file: %w", err)
|
|
}
|
|
defer inputFile.Close()
|
|
|
|
outputFile, err := os.Create(destPath)
|
|
if err != nil {
|
|
return fmt.Errorf("Couldn't open dest file: %w", err)
|
|
}
|
|
defer outputFile.Close()
|
|
|
|
_, err = io.Copy(outputFile, inputFile)
|
|
if err != nil {
|
|
return fmt.Errorf("Couldn't copy to dest from source: %w", err)
|
|
}
|
|
|
|
// for Windows, close before trying to remove: https://stackoverflow.com/a/64943554/246801
|
|
err = inputFile.Close()
|
|
if err != nil {
|
|
return fmt.Errorf("Couldn't close input: %w", err)
|
|
}
|
|
|
|
err = os.Remove(sourcePath)
|
|
if err != nil {
|
|
return fmt.Errorf("Couldn't remove source file: %w", err)
|
|
}
|
|
return nil
|
|
}
|