Get a path to parent folder in Inno Setup
I need to get the parent folder of {app}
. It's standard if the end user didn't change the default, but if he did, it becomes a little more problematic. Basically, I need a function, that will output everything till the last \
backslash (inclusive). Wanted to try Pos
, but it only detects the first instance of the character.
Use the ExtractFilePath
function:
Extracts the drive and directory parts of the given file name. The resulting string is the leftmost characters of FileName, up to and including the colon or backslash that separates the path information from the name and extension. The resulting string is empty if FileName contains no drive and directory parts.
Examples:
-
ExtractFilePath('C:\foo\bar')
=>'C:\foo\'
-
ExtractFilePath('C:\foo\bar\')
=>'C:\foo\bar\'
-
ExtractFilePath('C:\foo\foo\bar')
=>'C:\foo\foo\'
-
ExtractFilePath('..\foo\foo\bar')
=>'..\foo\foo\'
-
ExtractFilePath('C:bar')
=>'C:'
-
ExtractFilePath('\\server\foo\bar')
=>'\\server\foo\'
-
ExtractFilePath('foo')
=>''
-
ExtractFilePath('\foo')
=>'\'
-
ExtractFilePath('')
=>''
-
ExtractFilePath('C:/foo/bar')
=>'C:/foo/'
-
ExtractFilePath('C:/foo/bar/')
=>'C:/foo/bar/'
It does not matter, if bar
is a file or a (sub)folder. The function operates on the string only. It does not check any physical files or folders anyhow. So it does not even matter if any part of the path (or even the drive) exists or not.