I’m using Delphi 2010 on Windows 7 and have a problem with single quotes doubling while searching a directory recursively.
This is my code that searches for directories.
if FindFirst(aPath + '*', faDirectory, sr) = 0 then
try
repeat
if (sr.Name <> '.') and (sr.Name <> '..') then
if (sr.Attr and faDirectory) = faDirectory then
SearchFolderEx(aPath + sr.Name + '\', aSearchMasks);
until FindNext(sr) <> 0;
finally
FindClose(sr);
end;
Now with a path like this (starting at “C:\New folder\”)
C:\New folder\New Folder's\New Text Document.txt
FindFirst/FindeNext doubles the single quote
'New Folder''s'
and FindData.cFileName from the TSearchRec looks like this
('N', 'e', 'w', ' ', 'F', 'o', 'l', 'd', 'e', 'r', '''', 's', #0, #0, ...)
where could be the problem and how can I fix it?
There’s no problem here and nothing needs to be fixed. The
'is the string delimiter and is simply escaped for representation as''. When the debugger shows you''in a string, that’s just its way of representing a single quote character.The documentation covers this topic here: Character Strings.
So,
is a string of length 1 whose single element is the quote symbol.
Likewise
is a Delphi string literal defining the string
The debugger shows you the contents of the variable using the same rules as are used for string literals.