Files and folders - Resource class
The Resource class is intended for general manipulations with files and folders, for example, creating a folder under the path specified, or a file with a specific content in a defined location, or copy/move a folder or a file to the location specified.
See the Files and Folders action group in RPA Recorder for better understanding the Resource API. For a quick start, do as follows.
- Create a script with files/folders manipulations in RPA Recorder.
- Export this script as a Bot task or export to Groovy code and analyze the auto-generated code which uses the Resource API.
Examples
Resource class usage
This example performs the following actions:
- Downloads a text file from S3 file storage.
- Checks that the new file was downloaded to a temp folder. The check
result is saved in the
is_existingvariable. - Appends the downloaded text file content with a new content.
- Reads the result into the
content_utf8variable. - Creates a new directory with a new file (randomly generated name).
- Overwrites this file content with a new string containing the current timestamp.
Expand to see the example
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" scriptlang="groovy">
<robotics-flow>
<robot driver="desktop" close-on-completion="true" name="desktopDriver">
<script><![CDATA[
def s3Path = 'https://pub_demo.s3.amazonaws.com/trainings/new.txt';
def newDir = 'D:\\temp\\new\\';
def filePath = downloadFileOnAgent(s3Path);
def newFileName = newDir + UUID.randomUUID() + '.txt';
def now = new Date().format( 'yyyyMMdd:hh:mm:ss');
is_existing = Resource.exist(filePath);
if (is_existing) {
Resource.append(filePath, '___ New Content ___', 'utf-8');
content_utf8 = Resource.read(filePath, 'utf-8');
Resource.delete(filePath);
}
Resource.createDirectoryOverwrite(newDir);
Resource.createFileSkip(newFileName);
Resource.overwrite(newFileName, "File overwritten at ${now}", 'utf-8');
]]></script>
</robot>
</robotics-flow>
<export include-original-data="true">
<single-column name="is_existing" value="${is_existing}"/>
<single-column name="content_utf8" value="${content_utf8}"/>
</export>
</config>
Actions with files and folders
This example performs the following actions:
- Getting all files and folders recursively and exporting as a list
- Getting all TXT files recursively and copying them to the destination folder
- Getting all folders changed in the last five days and moving them to the destination folder
Expand to see the example
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" scriptlang="groovy">
<robotics-flow>
<robot driver="desktop" close-on-completion="true" name="desktopDriver">
<script><![CDATA[
import com.workfusion.rpa.helpers.resources.Filter
import java.time.temporal.ChronoUnit
def sourceFolder = 'D:\\_temp\\configs\\'
def destinationFolder = 'D:\\_temp\\txt\\'
// Getting all files and folders recursively and exporting as a list
def filterAll = Filter.filesAndFolders().includeSubFolders().get()
def allFolderFiles = Resource.listFolder(sourceFolder, filterAll)
sys.defineVariable("allFolderFiles", allFolderFiles)
// Getting all TXT files recursively and copying them to destination folder
def filterByExtension = Filter.files().includeSubFolders().pattern('.*.txt').get()
def onlyTxtFiles = Resource.listFolder(sourceFolder, filterByExtension)
onlyTxtFiles.each { filename ->
Resource.copyOverwrite(filename, destinationFolder)
};
// Getting all folders changed in the last 5 days and moving them to destination folder
def filterByDate = Filter.folders()
.includeSubFolders()
.modifiedInLast(Integer.valueOf("5"), ChronoUnit.DAYS)
.get()
def onlyNewFolders = Resource.listFolder(sourceFolder, filterByDate)
onlyNewFolders.each { foldername ->
Resource.moveSkip(foldername, destinationFolder)
};
]]></script>
</robot>
</robotics-flow>
<export include-original-data="true">
<multi-column list="${allFolderFiles}" split-results="true">
<put-to-column name="paths"/>
</multi-column>
</export>
</config>
Resource class methods
The Resource class has the following options:
- You can choose what to do in case some conflicts occur while the actions are executed, for example, overwrite or skip (keep an existing) file or folder, or fail the process execution.
- You can create text files with the** encoding** that enables writing, saving, and displaying all characters properly.
- When using the listFolder() method, you might need to import the following classes:
com.workfusion.rpa.helpers.resources.Filterjava.time.temporal.ChronoUnit
| Type | Method | Description |
|---|---|---|
| void | append(String path, String value, String encoding) | Opens a file using the path specified, adds a given string value to the end of the file |
| boolean | createDirectoryFail(String path) | Creates a folder under a path specified in the input field. If such folder already exists, exception is thrown |
| boolean | createDirectoryOverwrite(String path) | Creates a folder under a path specified in the input field. If such folder already exists, it will be overwritten |
| boolean | createDirectorySkip(String path) | Creates a folder under a path specified in the input field. If such folder already exists, no action is taken |
| boolean | createFileFail(String path) | Creates a file under a path specified in the input field. If such file already exists, exception is thrown |
| boolean | createFileOverwrite(String path) | Creates a file under a path specified in the input field. If such file already exists, it will be overwritten |
| boolean | createFileSkip(String path) | Creates a file under a path specified in the input field. If such file already exists, no action is taken |
| boolean | delete(String path) | Deletes a file or a folder under a path specified with entire content |
| boolean | exist(String path) | Checks if a file or a folder already exists and returns a Boolean result |
| void | overwrite(String path, String value, String encoding) | Opens a file using the path specified, deletes file content, adds a given string value to the file |
| String | read(String path, String encoding) | Reads content from a specified file and returns a string value. |
| List<String> | listFolder(String path) | Reads content of a defined folder including files and sub-folders, and returns the result (full paths to the items) as a List |
| List<String> | listFolder(String path, Filter filter) | Reads content of a defined folder, filters the content (Filter object), and returns the result (full paths to the items) as a List |
| void | moveFail(String resourceFrom, String pathTo) | Moves a file or a folder from one location to another. If such file or folder already exists, exception is thrown |
| void | moveOverwrite(String resourceFrom, String pathTo) | Moves a file or a folder from one location to another. If such file or folder already exists, it will be overwritten |
| void | moveSkip(String resourceFrom, String pathTo) | Moves a file or a folder from one location to another. If such file or folder already exists, no action is taken |
| void | copyFail(String resourceFrom, String pathTo) | Copies file or folder from one location to another. If such file or folder already exists, exception is thrown. |
| void | copyOverwrite(String resourceFrom, String pathTo) | Copies file or folder from one location to another. If such file or folder already exists, no action is taken. |
| void | copySkip(String resourceFrom, String pathTo) | Copies file or folder from one location to another. If such file or folder already exists, no action is taken. |