AA: Finding Last File in Folder

If you wish to skip past my rambling, click here.

Automation often involves directory management, which is a fancy way of saying “Arrange data into respective storage units/bins” which is also a fancy way of saying “Store data appropriately”.

Fancy makes you look a lot more sophisticated than you actually are.

Storing excel workbooks in folders is one such example of directory management, but that is too simplistic.

Here is a realistic example of directory management.

A set of excel workbooks that contains data which has to be tabulated each month, while also requiring all sorts of complex calculations to be performed with the data present in each file and storing them in a given directory, is one such example.

Processing inventories, refining data for GP forecasting, comparing tabulated data with data from government websites to check whether they adhere to state policies (GST) etc. before pushing them into an SFTP comes under this category as well.

Once the processes are complete, you often have to store them into a central database, or push them into a CRM such as SAP or Oracle. You might also have to zip selective files, rename the zipped collection with the date it was processed on, and send it across to stakeholders.

Hellish examples indeed, but these are the ones that you will surely encounter once you venture out into the wild and wrangle with a few of projects on your own.

That is a Lot To Take In.

Processes often come with sub processes that aren’t specifically mentioned, or are only lightly touched on during discussions with end users.

The end user not only wants his process to be automated, but he also wishes to log and monitor the process being automated because he doesn’t trust you is unaware, or not technically adept enough to understand the techniques we developers use to test automations.

Developers have their own way of testing use cases, since they are the ones developing them. We can run processes from the control room itself, add in necessary breakpoints and refine the workflow if necessary, while end users on the other hand, may only be equipped with enough technical knowledge to trigger automations, and maybe access log files.

They can’t resolves issues on their own, which is why they rely on us to develop and test automations.

Wait, Where Is This Fancy Directory Management You Were Talking About?

It is all over the place.

When you download files, it has to end up in designated locations within the client’s machine. You can’t place it in the Downloads directory because that would make it difficult to manage, nor can you pool it into his personal directory.

What, you thought the automation would run on your end? You are developing automations so that the client can run them on HIS end, not yours.

You silly goose.

Long story short, not managing and maintaining directories will piss your clients off.

Happy Client, Won’t Get Violent, so keep an eye out for such things.

This is why we usually develop sub-automations that create folders and subfolders that will house the data our automation will you know…automate.

Always Create Folders in The Beginning. Noted.

No, no, no!

You can’t simply program the bot to create folders, without first checking whether the folder(s) you are planning to create actually exist or not.

If you program the bot to create a folder which already exists, it will crash.

Again, you silly goose.

Wait, Why Will It Crash? Can’t It Just Move Onto The Next Step?

Welcome to software engineering.

Robots aren’t equipped with awareness. They are perfect slaves that do exactly what they’re told, to the letter. Or in this case, exactly to the bits and pieces of code we program into it.

Here is an article on that.

Anyways, this is what the “Create Folder Workflow” will look like in Automation Anywhere:

Why am I using a Try Catch? Click here to find out more.
Why am I using a try Catch with an empty Catch? I was too lazy to add in the rest.

Now that I am done going off topic, lets get into some of the technical stuff I promised you.

We will now engage in a simple exercise which involves automatically retrieving the name of the last file present in a given folder.


First, we declare a counter which will hold the number of files present in a given folder. The counter of course, will be a number variable.

The variable has been initialized to 0 but this step isn’t necessary, as you also have the option of heading over to the variables corner and plugging it in there.

Then drag in a Loop: For Each File in Folder, and add in a Number: Increment Action which will increment the counter. This segment provides us with the number of files present in a given directory.

If the part where we assign Dictionary values into the Loop confuses you, here is an article on how to use Dictionaries in Automation Anywhere.

Why Did We Do This?

This loop simply counts the number of files present in a given directory. If you want to count the number of Excel Workbooks, or pdf in a given directory, you can wrap the nCounter variable inside of an If Action and increment it only when it validates to true i.e. enters the If Action Block.

$DictFileinFolder{extension}$ = .xlsx
$DictFileinFolder{extension}$ = .pdf

Onto The Second Loop

Now that we have the total number of files, we will again drag in a Loop: For Each File in Folder Action which will house an If Action: Number Condition.

The If Action will iterate through each File, while decrementing the number variable until it equals 1. This means we have reached the end of the given File Directory, which also means we have reached the last file in folder.

Feel free to plagiarize and improvise.

This was my approach, and maybe you can think of something smarter. Try building on what I have detailed so far.

This isn’t exactly the best approach, but it ought to give you an idea of what is possible, and of the routes you can take to achieve a solution.


There are, however, limitations to this process, if you have been paying attention.

Retrieving the Last file from a folder is too vague, since we have yet to define the context.

Is it the last created file? Or is it the last modified file? How about the last edited file?

Every edit is a modification, but not all modification is an edit.
-William Shakespeare (did not say this)

What about the size? Should it be within a given range, or do you know what its exact size is? When was it created? Do we have to specify a date for that as well?

Where are you going to find the answers to all of that?

You have to rely on VBScript for that, but we won’t go into any of that today.

If I were to throw everything at you right now, you might run away.

Don’t worry though, we will explore it in the coming days.

Leave a Comment