One question that can come up in SmartConnect is, “How can we delete source files that have been moved into a folder, such as an archive folder”?
This can be done by creating a task that runs before the map. We can create this task by clicking on the Tasks tab on the Map Setup window.
Then we can right click on “Tasks that run before the map”, select New Task -> Run Script.
This will open a window where we can write a script to delete these files using .Net code.
In this example, we will include logic on deleting files that are older than X number of days.
Unless you are familiar coding in .NET, this will take some research to figure out how to write it.
Below is some sample code to delete files older than 10 days. We can change the days value as needed for the specific requirements.
As we can see in the code above, we will be able to set the directory to wherever the files we want to be deleted are stored.
The code will run through each file in that directory and delete it, if it is older than 10 days.
This is a very useful option to save space since these files have already been integrated. It also saves time by not having to manually
go and delete all these files every day.
Delete Files with a Certain Extension
What if we want to delete files like above, but only files with a certain extension. We can do this by updating our
.NET code from above. Let’s say that we want to delete files older than X amount of days that have a file extension “.txt”.
We can do this by adding one piece to the .NET code above. As we can see below, we added what file extension to delete behind the “GetFiles” function.
This script will now only grab files that have a “.txt” extension and delete the file if it is older than 10 days.
This is also a great option, since we may have other files in our folder that we do not want to delete.