How do you rename a file?

Discussion in 'Android Questions' started by NeoTechni, Jul 2, 2012.

  1. NeoTechni

    NeoTechni Well-Known Member Licensed User

    WITHOUT using file.copy/file.delete cause that is wasteful.
     
  2. margret

    margret Well-Known Member Licensed User

    This is a very needed feature in what I do as well. I would love to have a way to rename a file without having to copy it! Maybe someone can show us a way!
     
  3. Mahares

    Mahares Well Known Member Licensed 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.
     
  4. NeoTechni

    NeoTechni Well-Known Member Licensed User

    I know that. Thats why I said WITHOUT USING FILE.COPY/FILE.DELETE
     
  5. margret

    margret Well-Known Member Licensed 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: Jul 2, 2012
  6. NJDude

    NJDude Expert Licensed User

    Using the Phone lib and Shell (e.g)
    Code:
    Dim p As Phone

    p.Shell(...
    You could use the MV command, but not sure if it would work as expected, I haven't tried it myself yet.
     
  7. Mahares

    Mahares Well Known Member Licensed 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.
     
  8. margret

    margret Well-Known Member Licensed User

    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.
     
  9. NeoTechni

    NeoTechni Well-Known Member Licensed 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
     
  10. Mahares

    Mahares Well Known Member Licensed 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.
     
  11. stevel05

    stevel05 Expert Licensed User

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

    Code:
    Sub FileRename(Old As String,New As StringAs 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.
     
  12. Mahares

    Mahares Well Known Member Licensed 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
     
  13. raytronixsystems

    raytronixsystems Active Member Licensed 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
     
  14. margret

    margret Well-Known Member Licensed 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:

    Code:
    RenameFile("/mnt/sdcard/Movies/Monsters_HQ.mp4""/mnt/sdcard/Movies/M_HQ.mp4")
    Code:
    Sub RenameFile(OriginalFileName As String, NewFileName As StringAs 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: Jul 3, 2012
    Mashiane, tdocs2 and merlin2049er like this.
  15. NeoTechni

    NeoTechni Well-Known Member Licensed User

    Thank you!

    Can we encapsulate the filenames in ' in case they have spaces?
     
  16. margret

    margret Well-Known Member Licensed 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: Jul 3, 2012
    MarcRB likes this.
  17. stevel05

    stevel05 Expert Licensed 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.
     
    Last edited: Jul 3, 2012
  18. NeoTechni

    NeoTechni Well-Known Member Licensed User

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

    NeoTechni Well-Known Member Licensed User

    Here's the code I used with error handling

    Code:
    Sub RenameFile(SrcDir As String, SrcFilename As String, DestDir As String, DestFilename As StringAs 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
     
    Syd Wright and ilan like this.
  20. aidymp

    aidymp Active Member Licensed User

    Hi, I can see this is an old thread, But is there now a command to rename files? Its 2014 now :)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice