ACF Library

MoveFileToBackupLoc

Back to List

Description: Rename file to a backup name, finds an unused name to rename to.

FileMaker Prototype:

Set Variable [$res; ACF_Run("MoveFileToBackupLoc"; string_path)]

Category: EXCEL

Dependencies

  • GetFilenameFromPath: Return tha filename from a given full path - i.e. remove the directories. NOTE: put USE bootstrap; below the package header instead of copying the code for GetFilenameFromPath
  • GetDirectoriesFromPath: Remove the filename part from a full path, and return the directory path NOTE: put USE bootstrap; below the package header instead of copying the code for GetDirectoriesFromPath
  • GetExtentionFromPath: Retrieve the file type (extention) from a filename or a full path. NOTE: put USE bootstrap; below the package header instead of copying the code for GetExtentionFromPath

Function source:

// Function: MoveFileToBackupLoc
// Description: Moves the specified Excel file to a backup location. 
//              If a file with the backup name already exists, appends a unique suffix to the filename.
// Parameters:
//   - path: The full path to the original Excel file that needs to be backed up.
// Returns:
//   - The full path of the newly created backup file.

function MoveFileToBackupLoc(string path) 
    string newPath; 
    // Extract the directory, filename, and file extension from the given path
    string dir = GetDirectoriesFromPath(path) + "/";      // Directory containing the file
    string fn = GetFilenameFromPath(path);               // Filename with extension
    string ext = GetExtentionFromPath(path);             // File extension (e.g., "xlsx")

    // Generate the base filename (without extension)
    string barefile = substitute(fn, "." + ext, ""); 

    // Initialize the backup file name
    newPath = dir + barefile + "_backup." + ext;

    // If the backup file already exists, add a numeric suffix to create a unique name
    int i = 1; 
    while (file_exists(newPath)) 
        newPath = dir + barefile + "_backup" + i + "." + ext;
        i++;
   end while

    // Move the original file to the new backup location
    string res = move_file(path, newPath); 

    // Return the path of the newly created backup file
    return newPath; 
end

Back to List