{"id":71,"date":"2012-10-15T08:38:05","date_gmt":"2012-10-15T06:38:05","guid":{"rendered":"http:\/\/blog.kassebaum.eu\/?p=71"},"modified":"2012-10-15T08:40:36","modified_gmt":"2012-10-15T06:40:36","slug":"tpath-combine","status":"publish","type":"post","link":"https:\/\/www.kassebaum.eu\/blog\/2012\/10\/15\/tpath-combine\/","title":{"rendered":"TPath.Combine"},"content":{"rendered":"<p>I guess that everyone already had to solve this small issue. There are two variables, one with a path, the other with the filename and they have to be combined.<br \/>\nIn the old Delphi world there is the function IncludeTrailingPathDelimiter from System.SysUtils.<\/p>\n<pre class=\"lang:delphi decode:true \" title=\"IncludeTrailingPathDelimiter\" >\r\nprocedure TMyObject.DoSomething;\r\nvar\r\n  sFileName: string;\r\n  sPath: string;\r\nbegin\r\n  sFileName := 'Datei.txt';\r\n  sPath := 'c:\\temp';\r\n\r\n  sFileName := IncludeTrailingPathDelimiter(sPath) + sFileName;\r\n  Writeln(sFileName);\r\nend;\r\n<\/pre>\n<p>With the unit System.IOUtils Delphi offers the new record TPath with the method combine.<\/p>\n<pre class=\"lang:delphi decode:true \" title=\"TPath.Combine\" >\r\nprocedure TMyObject.DoSomething;\r\nvar\r\n  sFileName: string;\r\n  sPath: string;\r\nbegin\r\n  sFileName := 'Datei.txt';\r\n  sPath := 'c:\\temp';\r\n\r\n  sFileName := TPath.Combine(sPath, sFileName);\r\n  Writeln(sFileName);\r\nend;\r\n<\/pre>\n<p>In our example both functions do the same. But if you look at the implementation of TPath.Combine you can see that it offers some other functionalities. TPath.Combine calls TPath.DoCombine that checks the valid chars and also checks if Path2 is already absolute. This means that the following code raises an exception.<\/p>\n<pre class=\"lang:delphi decode:true \" title=\"TPath.Combine\" >\r\nprocedure TMyObject.DoSomething;\r\nvar\r\n  sFileName: string;\r\n  sPath: string;\r\nbegin\r\n  sFileName := 'Datei.txt';\r\n  sPath := 'c:\\temp' + #0;\r\n\r\n  sFileName := TPath.Combine(sPath, sFileName);\r\n  Writeln(sFileName);\r\nend;\r\n<\/pre>\n<p>And this code does work. <\/p>\n<pre class=\"lang:delphi decode:true \" title=\"TPath.Combine\" >\r\nprocedure TMyObject.DoSomething;\r\nvar\r\n  sFileName: string;\r\n  sPath: string;\r\nbegin\r\n  sFileName := 'c:\\temp\\Datei.txt';\r\n  sPath := 'c:\\temp';\r\n\r\n  sFileName := TPath.Combine(sPath, sFileName);\r\n  Writeln(sFileName);\r\nend;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I guess that everyone already had to solve this small issue. There are two variables, one with a path, the other with the filename and they have to be combined. In the old Delphi world there is the function IncludeTrailingPathDelimiter &hellip; <a href=\"https:\/\/www.kassebaum.eu\/blog\/2012\/10\/15\/tpath-combine\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[5],"class_list":["post-71","post","type-post","status-publish","format-standard","hentry","category-tipsandtricks","tag-delphi"],"_links":{"self":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/71","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/comments?post=71"}],"version-history":[{"count":7,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"predecessor-version":[{"id":78,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/71\/revisions\/78"}],"wp:attachment":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}