DaveR Valued Contributor
Joined: 03 Sep 2005 Posts: 413 Location: Australia
|
Posted: Wed Apr 25, 2007 7:43 am Post subject: Compact Path function |
|
|
Just thought I'd share my user defined function for truncating long paths and filenames. It returns truncated paths/filenames similiar to the Windows PathCompactPathExA Function.
For example, if the long path is "C:\Program Files\Visual Dialog Script\Demos" and the 1st parameter is 40, PathCompactPathEx will return "C:\...\Visual Dialog Script\Demos". Or if the 1st parameter is 30, PathCompactPathEx will return "C:\...\Demos"
Note:
If the 2nd parameter is a file name that is too long, rather than a path, the file name will be truncated to the max characters length (the 1st parameter), including the ellipsis. For example, if the input file name is "My Filename.txt" and the 1st parameter is 10, PathCompactPathEx will return "My Fil...".
I've included an optional 3rd parameter to retain the file's extension in the returned truncated filename, if desired. For example, if the 3rd parameter is 1, and the input file name is "My Filename.txt" and the 1st parameter is 12, PathCompactPathEx will return "My F...txt".
Still to do:
Include compacting of web addresses - preferably without using any 3rd-party librarys.
Code: | #define function,PathCompactPathExA
# Use:
# %x = @PathCompactPathExA(<length>,<path-to-compact>,<show-ext>)
# Show ext is optional - use 1 for yes
:PathCompactPathExA
if @both(@numeric(%1),%2)
%%maxLen = %1
%%Path = %2
%%PathLen = @len(%%Path)
if @greater(%%PathLen,%%maxLen)
if @not(@path(%2))
if @equal(1,%3)
%%Ext = @ext(%%Path)
%%newPath = @substr(%%Path,1,@diff(%%maxLen,@sum(3,@len(%%Ext))))...%%Ext
else
%%newPath = @substr(%%Path,1,@diff(%%maxLen,3))...
end
else
%%Root = @substr(%%Path,1):\...\
%%wLen = @diff(%%maxLen,7)
%%mPath = @substr(%%Path,@diff(%%PathLen,%%wLen),%%PathLen)
%%mPath = @strdel(%%mPath,1,@pos(\,%%mPath))
%%newPath = %%Root%%mPath
end
else
%%newPath = %%Path
end
else
exit 0
end
exit %%newPath |
Thanks to Aslan's help here:
http://www.vdsworld.com/forum/viewtopic.php?p=31454#31454 _________________ cheers
Dave |
|