July 31, 2008

Extract Filename and Folder from Path

Filed under: Scripting — Marcus Tettmar @ 1:34 pm

Here’s a quick way to extract just the filename from a full path:

Let>file_path=C:\Program files\Skype\Phone\Skype.exe

//get the file name on its own
Separate>file_path,\,parts
Let>exe_name=parts_%parts_count%

//now get just the folder path
MidStr>file_path,1,{length(%file_path%)-length(%exe_name%)},folder_only

This uses Separate to explode the path into an array using the ‘\’ character as the delimiter. We’ve called the array “parts”. “parts_count” contains the number of elements created. So parts_%parts_count% is the last element, which happens to be the filename.

Now, if we want just the folder path, use MidStr to extract length of file_path minus length of exe_name.

NB: For some reason the curly brackets in the last line of code above are causing the text to output on the next line. That should all be one line. Click on “view plain” to see the raw code if you want to copy it to Macro Scheduler.