Posted by webmaster Guido on July 02, 2008
In Reply to Counterpart to "ProcessPath" posted by Lee P12462 on June 29, 2008: ProcessPath takes a c:\dir1\dir2\filename.ext and splits it into drive(c), dirpart(\dir1\dir2) and filepart (filename.ext).
: Is there a corresponding procedure that takes those three pieces and makes the c:\dir1\dir2\filename.ext from them?
: I basically want to change filename.ext to something different but I can't find a procedure to do that. ChangeFileExt lets me change the .ext but I want to change the filename too.
: I have a work around (filespec := Drive + ':' + dirpart + '\' + mynewfilepart) but I can't help but think I'm missing something obvious.
: Any ideas?
: Thanks, Lee
Delphi doesn't have a counterpart (inverse) of ProcessPath. So, the only way to go is assembling the new "filespec" as you described.
Mind however, that the code that you suggested doesn't give the correct result if the original file is in the "root" directory of a drive, for example: c:\abc.txt
Then you get, with a new filename of 'def.txt':
...and assembling the new filespec would result in:
You only have to add a backslash if there is not already a backslash at the end of the "DirPart":
ProcessPath(FullName, Drive, DirPart, FilePart); if DirPart[Length(DirPart)] <> '\' then DirPart := DirPart + '\'; Filespec := Drive + ':' + DirPart + MyNewFilePart;