vampiredom
22nd November 2011, 06:14
This is loosely related to this thread (http://forum.doom9.org/showthread.php?t=145464) and I thought of posting there but I did not want to veer too much off-topic. So, here it is...
######################################################################################################################
# SplitFilename()
# 2011/11/22
# a function for splitting filenames into different parts. The "which" parameter determines which parts are returned
#
# parameter: filename (string)
# -----------------
# This should be a filename or a path. Both forward slashes and backslashes are allowed
# It works with both files and folders, relative and absolute paths.
# If the supplied string is a path to a folder, it should end in a / or \
#
# which (integer)
# ---------------
# This parameter should be an integer between -2 and 4, and returns the following result:
# Example: C:\Folder\Subfolder\Filename.ext
# |
# | -2 --> drive + folder C:\Folder\Subfolder\
# | -1 --> drive + folder + basename C:\Folder\Subfolder\Filename
# | 0 --> basename + ext Filename.ext
# | 1 --> drive C:
# | 2 --> folder \Path\Subfolder\
# | 3 --> basename Filename
# | 4 --> ext .ext
#
# An empty string is returned if there is no appropriate result
# (such as a relative path, a filename with no path or a file with no extension)
# Values outside of the range of -2 to 4 also return an empty string
# When returned, drive letter always ends with : and extension always begins with a .
#
#######################################################################################################################
function SplitFilename(
\ string filename,
\ int which
\ ) {
# first get the extension. If there is a slash after the ".", then it is not an extension but a dotted folder name
ext = RightStr(filename, FindStr(RevStr(filename), "."))
ext = (FindStr(ext, "/") > 0 || FindStr(ext, "\") > 0) ? "" : ext
# now determine drive name, folder name, and file basename
# allow both "/" and "\" for folder names
drive = LeftStr(filename, FindStr(filename, ":"))
dir1 = (Findstr(filename, "\") > 0) ? LeftStr(filename, StrLen(filename) - FindStr(RevStr(filename), "\") +1) : ""
dir2 = (Findstr(filename, "/") > 0) ? LeftStr(filename, StrLen(filename) - FindStr(RevStr(filename), "/") +1) : ""
dir = (StrLen(dir1) > StrLen(dir2)) ? dir1 : dir2
folder = (StrLen(dir) > 0) ? RightStr(dir, StrLen(dir) - StrLen(drive)) : ""
basename = MidStr(filename, StrLen(drive)+StrLen(folder)+1, StrLen(filename) - StrLen(drive) - StrLen(folder) - StrLen(ext))
# copy extension to filename and clear extension for filenames that begin with .
isDotFile = (ext != "" && basename == "") ? true : false
dotBase = ext
basename = (isDotFile) ? dotBase : basename
ext = (isDotFile) ? "" : ext
(filename == "") ? ""
\ : (which == -2) ? drive + folder
\ : (which == -1) ? drive + folder + basename
\ : (which == 0) ? basename + ext
\ : (which == 1) ? drive
\ : (which == 2) ? folder
\ : (which == 3) ? basename
\ : (which == 4) ? ext
\ : ""
}
A quick way to test exactly how this works can be accomplished by running a script like this:
f = "C:\Folder\Subfolder\Filename.ext"
BlankClip()
subtitle("---> " + f, font="Courier New", y= 10, text_color=$FFFF00)
subtitle("-2 = " + Splitfilename(f,-2), font="Courier New", y= 40, text_color=$DDDDDD)
subtitle("-1 = " + Splitfilename(f,-1), font="Courier New", y= 70, text_color=$DDDDDD)
subtitle(" 0 = " + Splitfilename(f, 0), font="Courier New", y=100, text_color=$DDDDDD)
subtitle(" 1 = " + Splitfilename(f, 1), font="Courier New", y=130, text_color=$DDDDDD)
subtitle(" 2 = " + Splitfilename(f, 2), font="Courier New", y=160, text_color=$DDDDDD)
subtitle(" 3 = " + Splitfilename(f, 3), font="Courier New", y=190, text_color=$DDDDDD)
subtitle(" 4 = " + Splitfilename(f, 4), font="Courier New", y=220, text_color=$DDDDDD)
######################################################################################################################
# SplitFilename()
# 2011/11/22
# a function for splitting filenames into different parts. The "which" parameter determines which parts are returned
#
# parameter: filename (string)
# -----------------
# This should be a filename or a path. Both forward slashes and backslashes are allowed
# It works with both files and folders, relative and absolute paths.
# If the supplied string is a path to a folder, it should end in a / or \
#
# which (integer)
# ---------------
# This parameter should be an integer between -2 and 4, and returns the following result:
# Example: C:\Folder\Subfolder\Filename.ext
# |
# | -2 --> drive + folder C:\Folder\Subfolder\
# | -1 --> drive + folder + basename C:\Folder\Subfolder\Filename
# | 0 --> basename + ext Filename.ext
# | 1 --> drive C:
# | 2 --> folder \Path\Subfolder\
# | 3 --> basename Filename
# | 4 --> ext .ext
#
# An empty string is returned if there is no appropriate result
# (such as a relative path, a filename with no path or a file with no extension)
# Values outside of the range of -2 to 4 also return an empty string
# When returned, drive letter always ends with : and extension always begins with a .
#
#######################################################################################################################
function SplitFilename(
\ string filename,
\ int which
\ ) {
# first get the extension. If there is a slash after the ".", then it is not an extension but a dotted folder name
ext = RightStr(filename, FindStr(RevStr(filename), "."))
ext = (FindStr(ext, "/") > 0 || FindStr(ext, "\") > 0) ? "" : ext
# now determine drive name, folder name, and file basename
# allow both "/" and "\" for folder names
drive = LeftStr(filename, FindStr(filename, ":"))
dir1 = (Findstr(filename, "\") > 0) ? LeftStr(filename, StrLen(filename) - FindStr(RevStr(filename), "\") +1) : ""
dir2 = (Findstr(filename, "/") > 0) ? LeftStr(filename, StrLen(filename) - FindStr(RevStr(filename), "/") +1) : ""
dir = (StrLen(dir1) > StrLen(dir2)) ? dir1 : dir2
folder = (StrLen(dir) > 0) ? RightStr(dir, StrLen(dir) - StrLen(drive)) : ""
basename = MidStr(filename, StrLen(drive)+StrLen(folder)+1, StrLen(filename) - StrLen(drive) - StrLen(folder) - StrLen(ext))
# copy extension to filename and clear extension for filenames that begin with .
isDotFile = (ext != "" && basename == "") ? true : false
dotBase = ext
basename = (isDotFile) ? dotBase : basename
ext = (isDotFile) ? "" : ext
(filename == "") ? ""
\ : (which == -2) ? drive + folder
\ : (which == -1) ? drive + folder + basename
\ : (which == 0) ? basename + ext
\ : (which == 1) ? drive
\ : (which == 2) ? folder
\ : (which == 3) ? basename
\ : (which == 4) ? ext
\ : ""
}
A quick way to test exactly how this works can be accomplished by running a script like this:
f = "C:\Folder\Subfolder\Filename.ext"
BlankClip()
subtitle("---> " + f, font="Courier New", y= 10, text_color=$FFFF00)
subtitle("-2 = " + Splitfilename(f,-2), font="Courier New", y= 40, text_color=$DDDDDD)
subtitle("-1 = " + Splitfilename(f,-1), font="Courier New", y= 70, text_color=$DDDDDD)
subtitle(" 0 = " + Splitfilename(f, 0), font="Courier New", y=100, text_color=$DDDDDD)
subtitle(" 1 = " + Splitfilename(f, 1), font="Courier New", y=130, text_color=$DDDDDD)
subtitle(" 2 = " + Splitfilename(f, 2), font="Courier New", y=160, text_color=$DDDDDD)
subtitle(" 3 = " + Splitfilename(f, 3), font="Courier New", y=190, text_color=$DDDDDD)
subtitle(" 4 = " + Splitfilename(f, 4), font="Courier New", y=220, text_color=$DDDDDD)