pifantastic
pifantastic

Reputation: 861

Opening File paths with spaces in Delphi 5

(Using Delphi 5)

I am attempting to open a log file using the following code:

// The result of this is:
// C:\Program Files\MyProgram\whatever\..\Blah\logs\mylog.log
fileName := ExtractFilePath(Application.ExeName) + '..\Blah\logs\mylog.log';

// The file exists check passes
if (FileExists(fileName)) then
begin
    logs := TStringList.Create();

    // An exception is thrown here: 'unable to open file'
    logs.LoadFromFile(fileName);
end;

If I relocate the log file to C:\mylog.log the code works perfectly. I'm thinking that the spaces in the file path are messing things up. Does anyone know if this is normal behavior for Delphi 5? If it is, is there a function to escape the space or transform the path into a windows 8.3 path?

Upvotes: 0

Views: 3592

Answers (8)

FlyingGuy
FlyingGuy

Reputation: 333

Delphi 5 has never had a problem opening files with spaces and I am still using it since it is uber stable and works great for older XP apps. You need to check your code closely.

Upvotes: 0

Olivier Pons
Olivier Pons

Reputation: 15778

Simple :


// if log file = "C:\Program files\mylog.log"
// you'll get :
// »»»»» fileName = 'C:\Program files..\Blah\logs\mylog.log'
// if log file = "C:\mylog.log"
// you'll get :
// »»»»» fileName = 'C:..\Blah\logs\mylog.log'

Try this code instead, I'm pretty sure it will fit your needs :


fileName := IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName))
  + '..\Blah\logs\mylog.log';

Regards,

Olivier

Upvotes: 0

Kcats
Kcats

Reputation: 884

The spaces are not a problem. While the '..' could be a problem in Delphi 5, mosts probably the file is locked by the process that writes to it. If you have control of it, make sure it opens the file with fmShareDenyWrite and not fmShareExclusive or fmShareCompat (which is the default).

Also, you can use:

fileName := ExpandFileName(ExtractFilePath(Application.ExeName) + '..\Blah\logs\mylog.log');

to obtain the absolute path from a relative path.

Also, as others have said, it is not good idea to write anything in Program Files. Regular users (that are not Administrators or Power Users) do not have rights to write there (although in Vista is will be virtualized, is is still not a good idea). Use the appropriate Application Data folder for the user (or all users). This folder can be obtained using:

SHGetFolderPath(0,folder,0,SHGFP_TYPE_CURRENT,@path[0])

where folder is either CSIDL_COMMON_APPDATA or CSIDL_LOCAL_APPDATA. See this delphi.about.com article for an example.

Upvotes: 1

Stijn Sanders
Stijn Sanders

Reputation: 36840

It's odd that Delphi 5 would throw errors about this. I know of an issue with FileExists failing on files with an invalid last-modified-date (since it internally uses FileAge), but it's the opposite here. Instead of using "..\" I would consider risking the current path, and loading from a relative path: LoadFromFile('..\Something\Something.log'); especially for smaller applications, or by calling ExtractFilePath twice: ExtractFilePath(ExtractFilePath(Application.ExeName))

Upvotes: 2

Toby Allen
Toby Allen

Reputation: 11213

Are you sure its not the "..\" thats causing the problem rather than the spaces. Have you tried to see if it works at

c:\My\Path\nospaces\

If so and you are always using the ..\ path, maybe write a simple function to remove the last folder from your application path and create a full correct pathname.

Upvotes: 2

JosephStyons
JosephStyons

Reputation: 58685

Delphi 5 can open files with spaces - that is certainly not the problem. To prove it, try copying it to c:\my log.log- it should open fine.

Is there any more information in the error message you receive? The most likely thing is that someone else (perhaps your own program) is still writing to the log.

Upvotes: 1

Kevin Gale
Kevin Gale

Reputation: 4448

I'm pretty sure Delphi has always handled spaces so I doubt that is the issue.

You don't show the full path. Any chance it is really long? For example I could believe an issue with paths longer than 255 characters.

It's also a bad idea to put log files under Program Files. Often normal users are not given permission to write to anything under Program Files.

Upvotes: 1

skamradt
skamradt

Reputation: 15538

I'm pretty sure that Delphi 5 handles spaces in filenames ok but it has been a very long time since I have used that specific version. Is the file currently open by another process? It also could be a permissions issue. Can you instead of loading it into a tStringList, try opening it with a tFileStream with the filemode set to "fmOpenRead or fmShareDenyNone".

fStm := tFileStream.Create( filename, fmOpenRead or fmShareDenyNone );

then load your tStringlist from the stream:

Logs.LoadFromStream ( fStm );

Upvotes: 3

Related Questions