How do you rename a file?

Mahares

Expert
Licensed User
Longtime User
According to the documentation for B4A:
Files - Basic4android Wiki
There does not exist a RenameFile function. To rename a file, first copy it with File.Copy and then delete the old file with File.Delete.
You already know the answer.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
@ Mahares - That's not a very good answer. Say I have a movie file 1gig in size. My memory card only has 900meg free. Now I want to rename my movie file, well you see you can't if you have to copy the file first. Not to mention it taking five minutes or more to rename it.

This is not a big deal with small app/settings files but in a situation as listed above it kills your ability to offer a rename function in your app. This could also be the case with a larger SQL file too.
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@margret: As a workaround, whenever I needed to rename a large file, I just connect the device to a PC and rename it using Windows Explorer. What are the drawbacks of doing that?
@neotechni: Do not use too many capital letters in your posts. It sends the message, you are shouting. I hope you are not. I am only agonizing with your pain.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
@margret: As a workaround, whenever I needed to rename a large file, I just connect the device to a PC and rename it using Windows Explorer. What are the drawbacks of doing that?

The drawbacks are when you have hundreds of users, using an app and they are not computer smart, it creates a log jam of support calls/emails. Most of them download from the App Store and don't connect to their PCs. It is not any problem for me to rename the files but it is for many users. This is not a major request that has been ask. We do need and have valid reasons for having the ability to rename files without copying them.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I had to shout cause you gave the exact answer I said not to. So clearly I didn't do a good enough job pointing it out the first time
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I responded the way I did to confirm your findings. As you know new documentation, libraries , funtions are circuating all the time. I happened to search it recently without a plausible answer, despite searching the most up to date information I could find.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The android java.io.File object has a renameTo method that can be accessed via reflection as:

B4X:
Sub FileRename(Old As String,New As String) As Boolean

   Dim R As Reflector
   Dim NewObj As Object
   NewObj=R.CreateObject2("java.io.File",Array As Object(New),Array As String("java.lang.String"))
   R.Target=R.CreateObject2("java.io.File",Array As Object(Old),Array As String("java.lang.String"))
   Return R.RunMethod4("renameTo",Array As Object(NewObj),Array As String("java.io.File"))
End Sub

Further documentation here

Please test this thoroughly if you mean to use it in a distributed app, as I have only made sure it works for very small files.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@steve: Since I have been equally looking for a solution, your code works very well. I tested it on renaming a file 24 MB in size and it passed the test. I do not have files bigger than that.
Thanks
 
Upvote 0

raytronixsystems

Active Member
Licensed User
Longtime User
I'm sure someone on here could put together a simple library that we can all use to rename files.:sign0060:

The java code is simple as found on the Internet:

import java.io.File;

public class RenameFile {

public static void main(String[] args) {

File file = new File("C:\\MyFile.txt");
File newFile = new File("C:\\MyFileRenamed.txt");
file.renameTo(newFile);
}
}

Regards,
Ray
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
The desolatesoul was kind enough to send me the core code of which I created this sub. I have tested it with files up to 1.5gig. You must pass the full path and file name for the original and the new file. It returns True if the file was renamed and False if it was not.

Uses the Phone library

Call with:

B4X:
RenameFile("/mnt/sdcard/Movies/Monsters_HQ.mp4", "/mnt/sdcard/Movies/M_HQ.mp4")

B4X:
Sub RenameFile(OriginalFileName As String, NewFileName As String) As Boolean
   Dim Result As Int
   Dim StdOut, StdErr As StringBuilder
   StdOut.Initialize
   StdErr.Initialize
   Dim Ph As Phone
   Result = Ph.Shell("mv " & OriginalFileName & " " & NewFileName, Null,  StdOut, StdErr)
   If Result = 0 Then
      Return True
   Else
      Return False
   End If
End Sub

Margret:D
 
Last edited:
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
You will have to play with it and see if the ' works for files names with spaces.

The method that stevel05 posted above works fine with spaces in the name.

The one I posted however, does have a problem if the file name has spaces.

The Reflection method may be the better option of the two. They will both need testing to find their weaknesses. But at least now, there are options!!!:D
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I'm sure someone on here could put together a simple library that we can all use to rename files.:sign0060:

For such a small, (presumably) infrequently used routine, I am not sure that a single function library would be much, if any more efficient than using reflection. You could isolate it in a code module or class if need be and build in file/path checking to suit your app.

I'd be interested to know if this is not the case.
 
Last edited:
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
For such a small, (presumably) infrequently used routine, I am not sure that a single function library would be much, if any more efficient than using reflection. You could isolate it in a code module or class if need be and build in file/path checking to suit your app.

I'd be interested to know if this is not the case.

I agree. The reflection works fine. I'd rather use that till some point where Erel integrates it into the file object.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Here's the code I used with error handling

B4X:
Sub RenameFile(SrcDir As String, SrcFilename As String, DestDir As String, DestFilename As String) As Boolean
    Dim R As Reflector, NewObj As Object, New As String , Old As String 
   If SrcFilename=Null OR DestFilename=Null OR SrcDir=Null OR DestDir=Null Then Return False
   If File.Exists(SrcDir,SrcFilename) AND Not(File.Exists(DestDir,DestFilename)) Then    
      New=File.Combine(DestDir,DestFilename)
      Old=File.Combine(SrcDir,SrcFilename)
      If Not(New = Old) Then
          NewObj=R.CreateObject2("java.io.File",Array As Object(New),Array As String("java.lang.String"))
          R.Target=R.CreateObject2("java.io.File",Array As Object(Old),Array As String("java.lang.String"))
          Return R.RunMethod4("renameTo",Array As Object(NewObj),Array As String("java.io.File"))
      End If
   End If
   Return False
End Sub
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Hi, I can see this is an old thread, But is there now a command to rename files? Its 2014 now :)
 
Upvote 0
Top