I was struggling to find an option besides using the file plugin. Using the preexisting function “file list”. I was curious if there wasn’t some way to get a files size? For example. If generating log files. I would want to enable an option to limit the log to max file size, otherwise keep appending data to it. I came across this use case while attempting to record the body of a REST call to my server. I wanted to quickly have a copy and paste of hopefully all potential variations of the data structure this call contained. Then import it as a custom type. Any simple way of doing this?
There are a few ways to solve this, the easiest ways are to:
- Use a FileList component
- Use a CSharp component to execute a bit of C# code
Here is a basic guide for both:
FileList
For the file list you can simply add the File plugin, then use the FileList component. Add the folder path (can be done dynamically) and then also add the file name in the search pattern (this can also be done dynamically, and is only required if you just want to see the specific file).
This will return a bunch of file metadata, including the File Size. You will need to use a for each component to access the file data as the FileList returns a list.
C#
For the C# solution, you will need to add the CSharp plugin. Then add the following code snippet as the function:
long GetFileSize(string file)
{
long length = new System.IO.FileInfo(file).Length;
return length;
}
The input parameter (file) will be the full file name including the location. This will then return the file size as well.
After you have determined the file size, you can check if it is less than a certain amount. If it is, then append, else create a new file.
You can also get the file size in an expression, like
new #.System.IO.FileInfo(“my file path”).Length
Another way to log the result of multiple calls, is to create a file for each call. The TextFileWrite function has a property ‘File exists’ that has an option ‘Increment file name’. Write all the calls to a folder with the same file name and Linx will append a number for each write.