Function: Copy_file and Move_file

The copy_file and move_file functions allow you to perform file operations in ACF. You can copy or move files from a source path to a destination path. However, please note that the source file must exist, and the destination directory must also exist before performing these operations.

About move_file

When using the move_file function, the behavior depends on whether the source and destination are on different disks or the same drive:

To rename a file, you can simply move it to the desired location with a different name.

Prototype:

string copy_file ( <Source file path>, <Destination path>); 
string move_file ( <Source file path>, <Destination path>); 

Parameters:

Return value:

Type string: "OK" if the operation was successful, otherwise an error message.

Example: Copy File

function testCopyFile ()

    string file1 = select_file ("Choose a file to copy?", desktop_directory()) ; 
    if ( file1 == "" ) then
        return "";  // User hit cancel
    end if
    string wohin =  select_directory("Destination directory?");
    if ( wohin == "" ) then
        return "";  // User hit cancel
    end if
    string newfn = regex_replace("^(.+)/(.+\..+)$", file1, wohin + "/\2"); 
    print format ("Chosen file: %s\nDest.dir: %s\nNew Path: %s\n", file1, wohin, newfn); 
    if ( file1 == newfn ) then
        throw "No match in regex"; 
    end if
    return copy_file ( file1, newfn ) ;  

END

Example: Move File

function testMoveFile ()

    string file1 = select_file ("Choose a file to move?", desktop_directory()) ; 
    if ( file1 == "" ) then
        return "";  // User hit cancel
    end if
    string wohin =  select_directory("Destination directory?");
    if ( wohin == "" ) then
        return "";  // User hit cancel
    end if
    string newfn = regex_replace("^(.+)/(.+\..+)$", file1, wohin + "/\2"); 
    print format ("Chosen file: %s\nDest.dir: %s\nNew Path: %s\n", file1, wohin, newfn); 
    if ( file1 == newfn ) then
        throw "No match in regex"; 
    end if
    return move_file ( file1, newfn ) ;  

END

In these examples, a file selection dialogue allows you to choose a file, followed by a directory selection dialogue for specifying the destination directory. A regular expression (regex_replace) is used to create a new path for the destination. The console output displays the chosen file, destination directory, and the new path.

Moving or Copying Directories

Both move_file and copy_file functions can also work on directories. If the source path points to a directory, the entire directory will be moved or copied to the destination. In the example provided, we've adjusted the regular expression to accommodate directory names.

Example: Copy Directory

function testCopyDirectory ()

    string dir1 = select_directory ("Choose a directory to copy?", desktop_directory()) ; 
    if ( dir1 == "" ) then
        return "";  // User hit cancel
    end if
    string wohin =  select_directory("Destination directory?");
    if ( wohin == "" ) then
        return "";  // User hit cancel
    end if
    string newdir = regex_replace("^(.+)/([^/]+)$", dir1, wohin + "/\2"); 
    
    print format ("Chosen directory: %s\nDest dir: %s\nNew Path: %s\n", dir1, wohin, newdir); 
    if ( dir1 == newdir ) then
        throw "No match in regex"; 
    end if
    return copy_file ( dir1, newdir ) ;  

END

In this example, you can choose a directory for copying to another location. The regular expression creates the new path for the destination directory.

These functions provide convenient ways to copy or move files and directories within your FileMaker solution.