The system cannot find the file specified

A system file path (windows) is

D:\Export\test.txt

but I get the error

2019/09/30 16:49:35 rename D:\Export\test.txt D:\Export\test1.txt: The system cannot find the file specified.
exit status 1

package main

import (
	"log"
	"os"
)

func main() {
	originalPath := "D:\\Export\\test.txt"
	newPath := "D:\\Export\\test1.txt"

	err := os.Rename(originalPath, newPath)
	if err != nil {
		log.Fatal(err)
	}
}

What is wrong with it?

2 Likes

I tested in my system (Windows 10) and works as it is expected . I mean, a file in a folder and then rename works fine but when the oriiginal file is not there I got the error message you mentioned.
Check if the original file exists…(I am unsing go 1.13)

3 Likes

you were right, but if I change destination disk to

c:\

then I get

The system cannot move the file to a different disk drive.

What is wrong with this method?

yes, that doesn’t work with differnt drives

2 Likes

and here is a solution

2 Likes

Andre, so good you find the solution but I think it is problem of the go’s version your are using.
I tested with go 1.13 and works as it is documented…

1 Like

Yamil, that’s strange because I also use 1.13 and I’ve got the error

JPEG_example_down.jpg
originalPath = e:/торренты/JPEG_example_down.jpg
newPath = c:/go/JPEG_example_down.jpg
2019/10/01 19:09:59 rename e:/торренты/JPEG_example_down.jpg c:/go/JPEG_example_down.jpg: The system cannot move the file to a different disk drive.
exit status 1
1 Like

Andre, we have some different uses cases here

  1. Rename filename - Just change the name of a file and keeps it in the same folder
  2. Rename filename - As you report in your last message, using extended characters and keeps it in the same folder
  3. Move a file - Moves a file just changing its location and filename
  4. Move a file - Moves a file just changing its location and filename using extended characters

Let’s test the simplest one (1).
Please just change JPEG_example_down.jpg to myJPG.jpg to check if go function works as itdocumented…

1 Like

Yamil, that shoud not be be a problem because go uses unicode internaly so renaming file name will not give us anything, but to be shure I did it and result is the same (it was moving the file in different disk drive (there are no issues with other cases)). Did you realy tried to do the same?

1 Like

I tried (1) and (3) and works fine (go 1.13 and Windows 10)
Let me try the others cases

1 Like

Use case (2) works fine.
However use case (4) show the error

2019/10/01 13:33:55 rename C:\push\торzz.txt C:\temp\zz01.txt: The system cannot find the file specified.
exit status 1

1 Like

Yamil, did you notice your use cases doesn’t reflect the different disk drive case when you move the file to different disk drive, for example from c:\ to e:\ ? I think you will get an error there too.

1 Like

I have only one particion.
However I created a virtual drive and the error comes.
So we have two sources of this error (1) With extended charact6er and copy to another foder in the same drive and 2) move to another drive (No matter if filename has extended characters or not)

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.