remame directory?

Just starting out? Need help? Post your questions and find answers here.
Damion12
User
User
Posts: 81
Joined: Tue Oct 30, 2012 1:39 am

remame directory?

Post by Damion12 »

Is there a proper way to rename a directory? I tried using renamefile() but it failed to rename...
(if it matters, I'm on windows, and I suppose I could use movefile_() but I'm trying to keep it cross platform)
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: remame directory?

Post by Keya »

the PB helpfile for RenameFile says "This function can also be used to rename/move directories" so it should be fine, so retest your code (feel free to post example!)
- make sure you didn't have any Explorer or command prompt or other programs with an open handle to that directory (use Process Explorer to do a Handle Search to confirm the directory isn't open)
- make sure the directory you're renaming to doesn't already exist
- make sure it doesn't contain invalid cars like /\:*?"<>|
- make sure permissions and attributes are ok
You can also try getting the error and checking MSDN:

Code: Select all

SetLastError_(0)
RenameFile("doesntexist","doesntexistb")
rc = GetLastError_()
Last edited by Keya on Fri Jan 27, 2017 8:48 am, edited 8 times in total.
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: remame directory?

Post by Bisonte »

That works !

If not, there is a problem with the permissions ... or the name is already in that directory...
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Damion12
User
User
Posts: 81
Joined: Tue Oct 30, 2012 1:39 am

Re: remame directory?

Post by Damion12 »

Code: Select all

#folder = "f:\test\test .me" ; f is a fat32 formatted disk, so permissions are NOT the issue.
#target = "f:\test\test.me"

If CreateDirectory(#folder)
	Debug "Folder created"
Else
	Debug "Failed to create folder: "+FileSize(#folder)
	End
EndIf

Debug "Target folder? "+FileSize(#target)

If RenameFile(#folder,#target)
	Debug "Folder renamed"
Else
	Debug "Failed folder rename"
EndIf
If I check the error code after the rename fails, I get "0" (success)
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: remame directory?

Post by Shardik »

Sorry, but I tested your code on Windows XP SP3 x86 and Windows 8.1 x64 with PB 5.44 x86 and it worked without any problem. The only change I made was to change the folder path (file system NTFS):

Code: Select all

#folder = "C:\Temp\test .me"
#target = "C:\Temp\test.me"
On my Windows XP SP3 machine even your unmodified example worked without any problem (PB 5.44 x86 in ASCII and Unicode mode).

By the way, on which Windows version did you experience the problem and which PB version did you use? You should always state these infos!
Last edited by Shardik on Fri Jan 27, 2017 12:47 pm, edited 1 time in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4955
Joined: Sun Apr 12, 2009 6:27 am

Re: remame directory?

Post by RASHAD »

Hi
Tested PB 5.51 x86 - 5.44 x86 Windows 10 x64

Code: Select all

If FileSize("f:\test") = -2
  CreateDirectory("f:\test\test .me")
Else
  CreateDirectory("f:\test")
  CreateDirectory("f:\test\test .me")
EndIf

If FileSize("f:\test\test .me") = -2
  SetCurrentDirectory("f:\test")
  RenameFile("test .me","test.me")
EndIf

Egypt my love
Post Reply