How to resolve the algorithm Rename a file step by step in the REALbasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Rename a file step by step in the REALbasic programming language

Table of Contents

Problem Statement

Rename:

This should be done twice:   once "here", i.e. in the current working directory and once in the filesystem root. It can be assumed that the user has the rights to do so. (In unix-type systems, only the user root would have sufficient permissions in the filesystem root.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Rename a file step by step in the REALbasic programming language

Source code in the realbasic programming language

Sub Renamer()
    Dim f As FolderItem, r As FolderItem

    f = GetFolderItem("input.txt")
    'Changing a FolderItem's Name attribute renames the file or directory.
    If f.Exists Then f.Name = "output.txt"
    'Files and directories are handled almost identically in RB.
    f = GetFolderItem("docs")
    If f.Exists Then f.Name = "mydocs"

    'Jump through hoops to find the root directory.
    r = RootDir(GetFolderItem("."))

    f = r.Child("input.txt")
    'Renaming in a different directory identical to renaming in current directory.
    If f.Exists Then f.Name = "output.txt"
    f = r.Child("docs")
    If f.Exists Then f.Name = "mydocs"
End Sub

Function RootDir(what As FolderItem) As FolderItem
    'RB doesn't have an easy way to find the root of the current drive;
    'not an issue under *nix but troublesome under Windows.
    If what.Parent <> Nil Then  'Nil = no parent = root.
        Return RootDir(what.Parent) 'Recursive.
    Else
        Return what
    End If
End Function

  

You may also check:How to resolve the algorithm Brace expansion step by step in the TXR programming language
You may also check:How to resolve the algorithm Character codes step by step in the Tailspin programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Shell one-liner step by step in the Nim programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the PureBasic programming language