GETFILEPATH retrieves the path information for the file accessed by the most recent READ or WRITE command. This can be useful if the READ or WRITE command does not specify its target file, leaving it up to the user to select a file at run time.
The arguments of GETFILEPATH must be the names of string variables. If a String Variable does not already exist for one or more of the arguments, it will be created. If any argument refers to an existing String Variable, then that variable's contents will be replaced with the file information.
The first argument, filenameStringVariable, is required. It will contain the filename of the file accessed, including its extension if any, without any other path information. For example, if the file selected was "C:\someDir\myFile.txt", then filenameStringVariable will contain "myFile.txt".
The second argument, baseNameStringVariable, and all subsequent arguments are optional. This argument is the name of the file without its extension. For example, if the file name is "myFile.txt", then baseNameStringVariable will contain "myFile".
The optional third argument, extensionStringVariable, if present contains the file extension including the dot. For example, if the filename is "myFile.txt", then extensionStringVariable will contain ".txt".
The optional fourth argument, parentPathStringVariable, will contain the path from the root directory to the the directory containing the file accessed but not including the file name. The path will include a final path separator such as "\". For example, if the full path of a file were "C:\someDir\myFile.txt", then the parentPathStringVariable will contain "C:\someDir\".
The optional fifth argument, fullPathStringVariable, will contain the full path from root directory to and including the file name with its extension.
If any optional argument is present, then all preceding arguments must also be present.
In Microsoft Windows, remember that the Windows path component separator, "\", is considered by Java to be an escape character. Therefore, to create a valid path you need to use either the forward slash, "/" or two backslashes to represent a single backslash in a string. For example, the file "C:\someDir\myFile.txt" would need to be written as "C:/someDir/myFile.txt" or "C:\\someDir\\myFile.txt".
This example program demonstrates a use of the GETFILEPATH command. The objective of the program is to allow the user to select an input data file, process the data in two different ways, then write the results to two different output files whose names are based on the name of the input file. The WRITE commands in this example are commented out because this program is just a schematic outline.
The example program's overall structure is the following: First, the READ command lets the user select a file via an Open File dialog. Next, the GETFILEPATH command saves the file path information in its arguments. Next the program processes the file's input data somehow ("process A"). Then an output file path based on the input file path is constructed using a STRING command. You can use the full flexibility of the STRING command to create the new name. The process A data is written to the output file. Then a second process is carried out on the input data, a second name is constructed for a second output file, and the data is written to that new file. You can copy the example and paste it into Statistics101 and run it. It will print out the results of the constructed file names.
'Let user select the input file: READ A B C 'Retrieve user-selected file's path information: GETFILEPATH fname baseName extension parentPath 'Print out the file info just for demo purposes: PRINT fname PRINT baseName PRINT extension PRINT parentPath PRINT "------- Process A --------" 'Process the data to make output data vectors D and E ' . . . 'Construct the first output file's name based on the 'input file's name: STRING parentPath baseName "_processA" extension outFileNameA 'WRITE file outFileNameA D E PRINT "------- Process B --------" 'Process the data to make output data vector F ' . . . 'Construct the second output file's name based on the 'input file's name: STRING parentPath baseName "_processB" extension outFileNameB 'WRITE file outFileNameB F PRINT outFileNameA PRINT outFileNameB