How to rename a file

What would be the procedure in Xojo, to call a document in my computer directory and once selected, rename this file?

The objective is:

  • Get a specific file in a directory
  • Open the file
  • Save the file in another directory with a different name.

Does it require a plugin?

Thanks…

[code]Dim sourceFile As FolderItem = SpecialFolder.Documents.Child(“MyFile.txt”)

If sourceFile.Exists Then
Dim destFile As FolderItem = SpecialFolder.ApplicationSupport.Child(sourceFile.Name)
sourceFile.MoveTo(destFile)
End If[/code]

Taken from: http://developer.xojo.com/xojo-io-folderitem$MoveTo

No Plugin required :slight_smile:

Just to be sure and to make it right let you me add code to check the dest file for existence. Especially when you rename, copy, move or make backup of files you temporary access. You never know it if there’s not already a destination.

The new framework FolderItem raises an exception. The old one “only” reported it to .LastErrorCode. So you’re app will most likely crash.

if not destfile.exists then sourceFile.MoveTo(destFile) else // err end if

[quote=253904:@Sascha S]Dim sourceFile As FolderItem = SpecialFolder.Documents.Child(“MyFile.txt”)

If sourceFile.Exists Then
Dim destFile As FolderItem = SpecialFolder.ApplicationSupport.Child(sourceFile.Name)
sourceFile.MoveTo(destFile)
End If[/quote]

Well explained!, but what If the SourceFile name is variable. I tried

Dim sourceFile As FolderItem = SpecialFolder.Desktop.Child("DescargaMultiple").item(0)

But I got an Out of bounds error :frowning:

[quote=384949:@Gerardo García]Well explained!, but what If the SourceFile name is variable. I tried

Dim sourceFile As FolderItem = SpecialFolder.Desktop.Child("DescargaMultiple").item(0)

But I got an Out of bounds error :([/quote]
Ok I notice:

  1. Item its not “Zero based” like arrays
  2. I need to discard Invisible files, like .DS_Store(Mac) or thumb(Windows)

[quote=384951:@Gerardo García]Ok I notice:

  1. Item its not “Zero based” like arrays
  2. I need to discard Invisible files, like .DS_Store(Mac) or thumb(Windows)[/quote]
    Yes, Analizing FolderItem.Visible = True and the last three characters of the Filename (Extension) :smiley: :smiley:

Warning:

On macOS, some files are not displayed by the Finder, but their name do not start with a dot, and are not declared as invisibles. I forgot the name(s).

.DS_STore does not have extensions (still MacOS), some other system files does not have too (still forget who).

MOVES the item, not COPY Iitem to… new location.

Was a misunderstanding… :slight_smile:

Actually, it doesn’t have a file name the “DS_Store” is the extension…

File names starting with a dot are invisible in Linux and Unix.