PB 5.11 - RenameFile reports success although it failed

Windows specific forum
nalor
Enthusiast
Enthusiast
Posts: 121
Joined: Thu Apr 02, 2009 9:48 pm

PB 5.11 - RenameFile reports success although it failed

Post by nalor »

In case you're using 'RenameFile' to move a file from one drive to another and the destination filename includes forbidden characters ( \ / : * ...) the 'RenameFile' command reports success although it failed - in the end the source file has been deleted and the destination file is not available.... perfect data loss...

I'd expect 'RenameFile' to fail as it does when src and dest-drive are equal.

Code: Select all

Define Filename.s="c:\TMP\test.txt"
Define NewFilename.s="d:\TMP\test:invalidfilename.txt"

If CreateFile(0, Filename)
	WriteString(0, "TEST")
	CloseFile(0)
	
	Debug "Filesize >"+Str(FileSize(Filename))+"<"
	
	If RenameFile(Filename, NewFilename)
		Debug "RenameFile successfull! New Filesize >"+Str(FileSize(NewFilename))+"<"
	Else
		Debug "Error!! RenameFile failed"
	EndIf
Else
	Debug "Createfile >"+Filename+"< failed!"
EndIf
I get this result:
[14:36:35] Filesize >4<
[14:36:35] RenameFile successfull! New Filesize >-1<
And in the destination dir there is a file named 'test' with a size of 0 Bytes.

I get the error independent of ASCII or UNICODE compiler setting.

Tried it with Purebasic 5.11 x86 on Windows 7x64
Fred
Administrator
Administrator
Posts: 18360
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: PB 5.11 - RenameFile reports success although it failed

Post by Fred »

The problem here is than the API function does the same thing, so it looks like your second filename is accepted by Windows, which is a bit strange:

Code: Select all

Define Filename.s="c:\AMD\test.txt"
Define NewFilename.s="E:\Downloads\test:invalidfilename2.txt"

If CreateFile(0, Filename)
   WriteString(0, "TEST")
   CloseFile(0)
   
   Debug "Filesize >"+Str(FileSize(Filename))+"<"
   
   ;If RenameFile(Filename, NewFilename)
   ;If MoveFileEx_(Filename, NewFilename, #MOVEFILE_COPY_ALLOWED)
   If MoveFile_(Filename, NewFilename)
      Debug "RenameFile successfull! New Filesize >"+Str(FileSize(NewFilename))+"<"
   Else
      Debug "Error!! RenameFile failed"
   EndIf
Else
   Debug "Createfile >"+Filename+"< failed!"
EndIf
Here it creates an empty 'test' file in the destination.
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: PB 5.11 - RenameFile reports success although it failed

Post by breeze4me »

It's because of NTFS Alternate Data Streams feature.
http://support.microsoft.com/kb/105763/en-us
Fred
Administrator
Administrator
Posts: 18360
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: PB 5.11 - RenameFile reports success although it failed

Post by Fred »

Thanks for the information, moved to windows specific forum.
Post Reply