'Move a file to a different drive with Go

I'm trying to move a file from my C-drive to my H-drive using os.Replace().

The code looks as follows:

func MoveFile(source string, destination string) {
    err := os.Rename(source, destination)
    if err != nil {
        fmt.Println(err)
    }
}

However, when I run the code I get the following error:

rename C:\old\path\to\file.txt H:\new\path\to\file.txt: The system cannot move the file to a different disk drive.

I found this issue on GitHub that specifies the problem but it appears that they will not change this function to allow it to move file on different disk drives.

I already searched for other possibilities to move files, but found nothing in the standard documentation or the internet.

So, what should I do now to be able to move files on different disk drives?

go


Solution 1:[1]

As the comment said, you'll need to create a new file on the other disk, copy the contents, and then remove the original. It's straightforward using os.Create, io.Copy, and os.Remove:

import (
    "fmt"
    "io"
    "os"
)

func MoveFile(sourcePath, destPath string) error {
    inputFile, err := os.Open(sourcePath)
    if err != nil {
        return fmt.Errorf("Couldn't open source file: %s", err)
    }
    outputFile, err := os.Create(destPath)
    if err != nil {
        inputFile.Close()
        return fmt.Errorf("Couldn't open dest file: %s", err)
    }
    defer outputFile.Close()
    _, err = io.Copy(outputFile, inputFile)
    inputFile.Close()
    if err != nil {
        return fmt.Errorf("Writing to output file failed: %s", err)
    }
    // The copy was successful, so now delete the original file
    err = os.Remove(sourcePath)
    if err != nil {
        return fmt.Errorf("Failed removing original file: %s", err)
    }
    return nil
}

Solution 2:[2]

You need to make sure that you handle all cases on both Linux and Windows. For example, for any size file,

package main

import (
    "flag"
    "fmt"
    "io"
    "os"
)

func MoveFile(source, destination string) (err error) {
    src, err := os.Open(source)
    if err != nil {
        return err
    }
    defer src.Close()
    fi, err := src.Stat()
    if err != nil {
        return err
    }
    flag := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
    perm := fi.Mode() & os.ModePerm
    dst, err := os.OpenFile(destination, flag, perm)
    if err != nil {
        return err
    }
    defer dst.Close()
    _, err = io.Copy(dst, src)
    if err != nil {
        dst.Close()
        os.Remove(destination)
        return err
    }
    err = dst.Close()
    if err != nil {
        return err
    }
    err = src.Close()
    if err != nil {
        return err
    }
    err = os.Remove(source)
    if err != nil {
        return err
    }
    return nil
}

func main() {
    var src, dst string
    flag.StringVar(&src, "src", "", "source file")
    flag.StringVar(&dst, "dst", "", "destination file")
    flag.Parse()
    if src == "" || dst == "" {
        flag.Usage()
        os.Exit(1)
    }

    err := MoveFile(src, dst)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
    fmt.Printf("moved %q to %q\n", src, dst)
}

Output (Linux):

$ cp move.file src.file && go build movefile.go && ./movefile -src=src.file -dst=dst.file
moved "src.file" to "dst.file"
$

Output (Windows):

>copy /Y move.file src.file && go build movefile.go && movefile -src=src.file -dst=dst.file
moved "src.file" to "dst.file"
>

Solution 3:[3]

This solution Moves the file and preserves permissions:

func MoveFile(src, dst string) error {
    in, err := os.Open(src)
    if err != nil {
        return fmt.Errorf("Couldn't open source file: %s", err)
    }

    out, err := os.Create(dst)
    if err != nil {
        in.Close()
        return fmt.Errorf("Couldn't open dest file: %s", err)
    }
    defer out.Close()

    _, err = io.Copy(out, in)
    in.Close()
    if err != nil {
        return fmt.Errorf("Writing to output file failed: %s", err)
    }

    err = out.Sync()
    if err != nil {
        return fmt.Errorf("Sync error: %s", err)
    }

    si, err := os.Stat(src)
    if err != nil {
        return fmt.Errorf("Stat error: %s", err)
    }
    err = os.Chmod(dst, si.Mode())
    if err != nil {
        return fmt.Errorf("Chmod error: %s", err)
    }

    err = os.Remove(src)
    if err != nil {
        return fmt.Errorf("Failed removing original file: %s", err)
    }
    return nil
}

If only want to Copy the file without remove the original:

func CopyFile(src, dst string) error {
    in, err := os.Open(src)
    if err != nil {
        return fmt.Errorf("Couldn't open source file: %s", err)
    }

    out, err := os.Create(dst)
    if err != nil {
        in.Close()
        return fmt.Errorf("Couldn't open dest file: %s", err)
    }
    defer out.Close()

    _, err = io.Copy(out, in)
    in.Close()
    if err != nil {
        return fmt.Errorf("Writing to output file failed: %s", err)
    }

    err = out.Sync()
    if err != nil {
        return fmt.Errorf("Sync error: %s", err)
    }

    si, err := os.Stat(src)
    if err != nil {
        return fmt.Errorf("Stat error: %s", err)
    }
    err = os.Chmod(dst, si.Mode())
    if err != nil {
        return fmt.Errorf("Chmod error: %s", err)
    }

    return nil
}

Solution 4:[4]

Maybe you can use a magic approach, just using the syscall.MoveFile as follows.

func main() {
    oldpath := "D:\\black.txt"
    newpath := "E:\\black-new.txt"

    from, _ := syscall.UTF16PtrFromString(oldpath)
    to, _ := syscall.UTF16PtrFromString(newpath)
    fmt.Println(*from, *to)
    err := syscall.MoveFile(from, to)
    if err != nil {
        panic(err)
    }
}

the program works.

if you want a cross-platform compatibility program, you can implement your own MoveFile.

func MoveFile(src string, dst string) error {
    if runtime.GOOS == "windows" {
        from, _ := syscall.UTF16PtrFromString(src)
        to, _ := syscall.UTF16PtrFromString(dst)
        return syscall.MoveFile(from, to)
    } else {
        return os.Rename(src, dst)
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Adeoy
Solution 4 a2htray yuen