Android Question rename a file in assets

Beja

Expert
Licensed User
Longtime User
Hello friends,

How can I change the name of a file in assets or sdcard using TODAY'S date value?

in VB it is something like:

Dim tday as string
tday = format now("mmddyy")
tday = "C\" & tday & ".txt"
Name "C:\Old.txt" As tday

Thanks in advance
 

DonManfred

Expert
Licensed User
Longtime User
1. Copy file with new name.
2. Delete old file
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks for the time and advice,

What I need is b4a equivalent of the above routine to change the file name (in code).
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Maybe .. ?
B4X:
Dim tday As String

DateTime.DateFormat = "MMMddyyyy"
tday = DateTime.Date(DateTime.Now) & ".txt"

File.Copy(File.DirAssets,"Old.txt",File.DirDefaultExternal,tday)
 
  • Like
Reactions: eps
Upvote 0

eps

Expert
Licensed User
Longtime User
Looks good mangojack

Then just a File.Delete of the old file to replicate a move, unless the intention is to keep the old files..?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Agreed, but I posted the actual code to do this, which the OP asked for after your post

Really???

He wrote in #3

>What I need is b4a equivalent of the above routine to change the file name (in code).

I already said that he must copy and delete. No need to say more. He is using B4A for 2 years now... He should be able to copy and delete a file!
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
For renaming, you can use this code

B4X:
Dim Success as boolean
Success=RenameFile(File.DirRootExternal & "/path/oldfilename.txt", File.DirRootExternal & "/path/newfilename.txt")

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
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks so much mangojack and marcick for the code example.. I will run and test them.

DonManfred,
Thanks for the time you are taking to interact in this thread.
You said, what it means, that I used b4a for two years and therefore I don't deserve the kind of detailed help I need.
1- yes I own the license for about 2 years, but it is not true that I used or had the time to study b4a for 2 years.
2- At the time you hit the send key, then the question does not belong to you only.. so many new comers will be interested in the answer. In this way, the whole forum should be considered as FAQs. that every visitor, be it a member or not can benefit from it.
3- Since you first advised me, now I also have the right to advice you that there is only one alpha in this forum, and we should refrain from telling people. I am a member and only have the right to answer a posted question if I have an answer, or read other people's answers so I can learn a new thing.

Finally, and don't take me wrong, everything I have said is without prejudice to your right to freedom of speech.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
@Beja

1) I did not say you have studied B4A for 2 years. I just mention that you ARE using B4A. 727 posts from you in this forum speaks his own language... You cannot be a newbie i suppose
Everything i said was that you SHOULD be ABLE (with 727 posts here) to find the code for Copying and Deleting a file here in forum or in the documentation. This commands are part of the core-lib which is marked on every new Project you create.
In fact you got the solution to your issue with the first answer you got
2) I fully agree
3) i agree to this too.

Finally, and don't take me wrong, everything I have said is without prejudice to your right to freedom of speech. ;-)
 
Last edited:
Upvote 0

Beja

Expert
Licensed User
Longtime User
Klaus,
danke.. It's very important to know that any data on the assets directory is read-only and can not be edited by app users, this
means that if one wants a text file to be edited and saved at runtime, then they should save it in the external SD card.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Not necessarily on external SD Card.

This is the point. DirInternal is internal, read-write space, for the App not necessarily internal memory or external memory (as some devices don't have external memory capabilities).

It really depends, as klaus mentioned, what you are trying to achieve here. Are you saving a file that is for use by the App only or for sharing with other Apps or available outside of the App.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
If the file is only used by your application use DirInternal.
If the file must be accessible from another application use DirRootExternal or DirDefaultExternal. You need to check if external storage is avalable.
If you have a file in DirAssets and want to write to it you must copy it somewhere else.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks guys, I think I am getting it now..
I have a table of contacts, (not the phone contacts).. and I want to open and add, edit and delete entries..., so from what you said here, the right place is the external SD card. (not DirAssets, not DirInternal)

best,
Beja
 
Upvote 0
Top