Android Question Check/Change Case of filename ERROR [Solved]

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All

This is a spin off question from another post.

The code below is my attempt answer my own question but is throwing up [an error] while compiling. I suspect it may have something to do with the Java bits [Sub RenameTo] which I have taken from another post.

Many Thanks in advance

Regards Roger

The Error Message:
B4A Version: 9.50
Java Version: 8
Parsing code. (0.09s)
Building folders structure. (0.04s)
Compiling code. (1.04s)
Compiling layouts code. (0.11s)
Organizing libraries. (0.00s)
(AndroidX SDK)
Generating R file. (0.00s)
Compiling debugger engine code. Error
B4A line: 3058
RenameTo(File.DirRootExternal&\
javac 1.8.0_65
shell\src\horsetrailer\B4A\AntennaBearingTool\main_subs_0.java:4403: error: ';' expected
_renameto(RemoteObject.concat(main.mostCurrent.__c.getField(false,"File").runMethod(true,"getDirRootExternal"),RemoteObject.createImmutable("/ABT/")),main.mostCurrent._oldname,RemoteObject.concat(main.mostCurrent.__c.getField(false,"File").runMethod(true,"getDirRootExternal"),RemoteObject.createImmutable("/ABT/")),main.mostCurrent._newname)} };

B4X:
Sub RenameTo(DirName As String, OldFileName As String, NewDirName As String, NewFileName As String) As Boolean
    Dim Source As JavaObject
    Source.InitializeNewInstance("java.io.File",Array(DirName,OldFileName))
    Dim Dest As JavaObject
    Dest.InitializeNewInstance("java.io.File",Array(NewDirName,NewFileName))
    Return Source.RunMethod("renameTo",Array As Object(Dest))
End Sub

Sub BtnSiteListPlus_click
    VibBip   
'Looks for file not named correctly in lowercase and renames it correctly   
    If File.Exists(File.DirRootExternal&"/ABT/", "impsitelist.csv") = False Then                        'Only run if file not found
        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
                    RenameTo(File.DirRootExternal&"/ABT/", OldName, File.DirRootExternal&"/ABT/", NewName) As Boolean     'Go to Rename Sub
                End If   
            Next
        End If
        Table.Clear
    End If
End Sub
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi Erel,
Fairly similar in release mode as below.

B4A Version: 9.50
Java Version: 8
Parsing code. (0.09s)
Building folders structure. (0.05s)
Compiling code. (0.51s)
Compiling layouts code. (0.08s)
Organizing libraries. (0.00s)
(AndroidX SDK)
Generating R file. (0.00s)
Compiling generated Java code. Error
B4A line: 3058
RenameTo(File.DirRootExternal&\
javac 1.8.0_65
src\horsetrailer\B4A\AntennaBearingTool\main.java:3472: error: ';' expected
_renameto(anywheresoftware.b4a.keywords.Common.File.getDirRootExternal()+"/ABT/",mostCurrent._oldname,anywheresoftware.b4a.keywords.Common.File.getDirRootExternal()+"/ABT/",mostCurrent._newname)} };
^
1 error

Regards Roger
 
Upvote 0

emexes

Expert
Licensed User
Also, I can't imagine any good coming from renaming a file to the same name, so perhaps add a check that renaming is actually required:
B4X:
NewName = OldName.ToLowerCase
If NewName = "impsitelist.csv" Then
    If NewName <> OldName Then
        RenameTo(...
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
emexes

My understanding is that Android is case sensitive and if the user names the file IMPSITELIST.CSV and the program is looking for impsitelist.csv the file won't be found.
Am I wrong? If so please advise.
My aim is to name the file in the correct case.

Regards Roger
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
check that renaming is actually required
If the name is already in the correct case ie "impsitelist.csv" then there is no need to rename it.

At best, renaming "impsitelist.csv" to "impsitelist.csv" will do nothing (other than waste some CPU time).

At worst, it might cause a "duplicate file name" error. It would be disappointing if Android did this, but... stranger things have happened :)
 
Upvote 0

rraswisak

Active Member
Licensed User
Longtime User
Just try rename same filename with different case on some letter with file manager... it does't work, from one device show 'The name already exists' while other say 'File is already in use'.

May be you can try rename the file twice, first rename - add a letter in front of filename and then second rename to desire lower case filename.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
If the name is already in the correct case ie "impsitelist.csv" then there is no need to rename it.

At best, renaming "impsitelist.csv" to "impsitelist.csv" will do nothing (other than waste some CPU time).

At worst, it might cause a "duplicate file name" error. It would be disappointing if Android did this, but... stranger things have happened :)
emexes,
The code in the first post already does that.
"If File.Exists(File.DirRootExternal&"/ABT/", "impsitelist.csv") = False Then "

Roger
 
Upvote 0

emexes

Expert
Licensed User
The code in the first post already does that.
"If File.Exists(File.DirRootExternal&"/ABT/", "impsitelist.csv") = False Then "
Good point. My suggestion is moot. Withdrawn, Your Honour!

At worst, it might cause a "duplicate file name" error. It would be disappointing if Android did this, but... stranger things have happened
Just try rename same filename with different case on some letter with file manager... it does't work, from one device show 'The name already exists' while other say 'File is already in use'.
Android :-(
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Just try rename same filename with different case on some letter with file manager... it does't work, from one device show 'The name already exists' while other say 'File is already in use'.

May be you can try rename the file twice, first rename - add a letter in front of filename and then second rename to desire lower case filename.
rraswisak. First I have to find the file of the right name but not all in lower case programmaticaly. Second I have to change the name of the file to lower case programmaticaly.
Roger.
 
Upvote 0
Top