Android Question File rename code snippet not working. [Solved]

Roger Daley

Well-Known Member
Licensed User
Hi All
I am still trying to rename a file grammatically, I am trying a sub found in Code Snippets that apparently works for other people. CODE SNIPPET

SCENARIO: User uploads file not named in lowercase.
I have this [file File.DirRootExternal&"/ABT/","IMPSITELIST.CSV"] and need to rename it to lowercase for those functions that are case sensitive.
I am calling the Sub RenameFile and all appears OK, except that the file is not renamed to lowercase.

I am probably missing something simple any clues much appreciated

Regards Roger


B4X:
Sub Globals
   Private OldName As String
   Private NewName As String
End Sub

Sub BtnTest_click
    'Looks for file and renames it to lowercase even if already in lowercase as file.exists is case insensitive.
    If File.Exists(File.DirRootExternal&"/ABT/","impsitelist.csv") = True Then            'Only run if file found "Name Case Insensitive"
        Table.Initialize
        Table = MF_File.ListFiles(File.DirRootExternal & "/ABT/", "*.*", True, False)                'Make list of all files
        If Table.Size > 0 Then                                                                        'Only run if there is at least 1 file
            For i = 0 To Table.Size -1                                                                
                OldName = Table.Get(i)
                NewName = OldName.ToLowerCase
                If NewName = "impsitelist.csv" Then
                    Private Success As Boolean
                    Success=RenameFile(File.DirRootExternal&"/ABT/"&OldName, File.DirRootExternal&"/ABT/"&NewName)
                    If Success Then Exit
                End If
            Next
        End If
    End If
End Sub

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
 

Geezer

Active Member
Licensed User
What does the log say ?

Without looking too much into it, I'd guess it's something to do with you not having PERMISSION to write to the device.
 

Roger Daley

Well-Known Member
Licensed User
What does the log say ?

Without looking too much into it, I'd guess it's something to do with you not having PERMISSION to write to the device.
Sorry,
The log doesn't show anything amiss. The permissions are OK. There is no problem reading from or writing to this directory.
It's only the RenameFile Sub that seems to be a problem. To be honest I don't have a clue how it works, but Donmanfred knows his stuff so it must be something I am doing wrong.

Regards Roger
 

noeleon

Member
Licensed User
Try renaming first to a different/temporary filename then rename temp to intended new name.

I don't know mv really is, whether it's copy or move. If it is copy then you will have to delete the old file first before renaming the temp file.
 
Last edited:

Roger Daley

Well-Known Member
Licensed User
noeleon,

You've hit it in one, renaming to a temp works perfectly.
Modified Sub below.

Many thanks
Regards Roger


B4X:
Sub BtnTest_click
    'Looks for file and renames it to lowercase even if already in lowercase as file.exists is case insensitive.
    If File.Exists(File.DirRootExternal&"/ABT/","impsitelist.csv") = True Then            'Only run if file found "Name Case Insensitive"
        Table.Initialize
        Table = MF_File.ListFiles(File.DirRootExternal & "/ABT/", "*.*", True, False)                'Make list of all files
        If Table.Size > 0 Then                                                                            'Only run if there is at least 1 file
            For i = 0 To Table.Size -1                                                                    'Loops to find file incorrectly Cased
                OldName = Table.Get(i)
                NewName = OldName.ToLowerCase
                If NewName = "impsitelist.csv" Then
                    Private Success As Boolean
                    Success=RenameFile(File.DirRootExternal&"/ABT/"&OldName, File.DirRootExternal&"/ABT/"&"TempName.csv")
                    Success=RenameFile(File.DirRootExternal&"/ABT/"&"TempName.csv", File.DirRootExternal&"/ABT/"&NewName)
                    If Success Then Exit
                End If
            Next
        End If
    End If
End Sub
 

udg

Expert
Licensed User
Nice to see that you solved your problem.
I've got here a different approach, but unfortunately it's not complete since I currently don't have my dev PC with me, sorry.
Having a look here, it comes to mind that your RenameFile sub could be simplified to something (meta-code, Java):
B4X:
File oldfile= new File(OriginalFileName);
File newfile = new File(NewFileName);
oldfile.renameTo(newfile);
Using JavaObjects you substantially creates instances to old and new file names, then use first instance (old name) to rename it to the new instance value.
 
Top