• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

Win 98 problem with get Dir & ini files

In delphi 4 I use the following code to run a program when a button is pressed, svnewspath is read from an ini file using Tinifile.readstring.

On windows 95, NT4 and 2000 it works fine but on all the 98 machines I've tried it just gives a "sorry file not found: d:\\" error.

Wht is 98 neither handling the string from the ini file or the getDir funcion.

procedure TForm1.SBtnNewsClick(Sender: TObject);
Var
  Dir, Path : String;
begin
  GetDir(0,Dir);
  Path := Dir+'\'+svNewsPath;
  if FileExists(Path) then
    begin
    ChDir(extractFilePath(Path));
    WinExec(Pchar(Path),SW_MAXIMIZE);
    ChDir(Dir);
    end
  else
    ShowMessage('Sorry, File not found:'+Path);

end;
0
mike_tipping
Asked:
mike_tipping
  • 5
  • 5
  • 2
  • +3
1 Solution
 
ckanetaCommented:
Maybe you have an extra backslash in the path.
0
 
TheNeilCommented:
GetDir is a tricky one - I never know if it includes a '\' or not so just modify your code so that it reads:

GetDir(0, Path);
IF Path[Length(Path)] <> '\'
THEN
  Path := Path + '\';
Path := Path + svNewPath;
IF FileExists...

By the way, if you're trying to find out if a path exists or not, try this routine:

FUNCTION PathExists(sPath : STRING): BOOLEAN;
  VAR
    srcFile   : TSearchRec;
    sSubPath  : STRING;
    bFinished : BOOLEAN;
    iCount    : INTEGER;
  BEGIN
    Result := FALSE;
    IF sPath = ''
    THEN
      Exit;

    IF sPath[Length(sPath)] = '\'
    THEN
      SPath := Copy(sPath, 1, Length(sPath) - 1);

    sSubPath := '';
    iCount := Length(sPath);
    REPEAT
      bFinished := FALSE;
      IF sPath[iCount] = '\'
      THEN
        bFinished := TRUE
      ELSE
      BEGIN
        sSubPath := sPath[iCount] + sSubPath;
        iCount := iCount - 1;
      END;
    UNTIL bFinished OR (iCount = 0);

    sSubPath := UpperCase(TRIM(sSubPath));
    IF FindFirst(sPath, faDirectory, srcFile) = 0
    THEN
    BEGIN
      IF UpperCase(TRIM(srcFile.Name)) = sSubPath
      THEN
        Result := TRUE
      ELSE
        WHILE FindNext(srcFile) = 0
        DO
          IF UpperCase(TRIM(srcFile.Name)) = sSubPath
          THEN
            Result := TRUE;
      FindClose(srcFile);
    END;
  END;


The Neil
0
 
mike_tippingAuthor Commented:
It seems to be a thing with 98 not the code, I'm wondering if 98 is doing somthing strange as the code works on NT, 2000 and 95
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
TheNeilCommented:
As I said, it could be a quirk in GetDir. Have you checked the value of Path before you do the FileExists?

The Neil
0
 
mike_tippingAuthor Commented:
This apparently only happens when the exe is run from a CD, run it from the HD and it's fine. Run it from the CD on 95, NT and 200 and it's fine...

0
 
TheNeilCommented:
According to the Delphi help, GetDir will return a valid path even if the drive doesn't exist. Might it be worthwhile checking to see if the drive exists before you do the GetDir? I still think that it's something to do with the creation of the path string

The Neil
0
 
mike_tippingAuthor Commented:
I don't think it's a problem with get dir, thinking about it the error message says file d:\\ doesn't exist and since its from a CD the drive letter seems to be picked up fine, it seems to be the ini file reading which ias at fault, code is below, but why would an ini file be read ok everywhere but on a CD under 98?

procedure TForm1.FormActivate(Sender: TObject);
Var
  UDLName : array[0..255] of char;
  LangSection : String;
  LingoIni : TIniFile;
  Dir : String;
begin
  GetDir(0,Dir);
  LingoIni := TInifile.Create(Dir+'\Lingo.ini');
  svNewsPath := Lingoini.readString('setups','News','');
  svRoutePath := Lingoini.readString('Setups','Route','');
  svMapPath := Lingoini.readString('Setups','Maps','');
0
 
EpsylonCommented:
What happens when you use GetCurrentDir instead of GetDir?
0
 
MadshiCommented:
Why do you use GetDir at all? Perhaps you want to get the path where your application was started from? Then you should use ExtractFilePath(ParamStr(0)). Please tell us more about what your code is supposed to do.

BTW, you should not use WinExec, instead you should use ShellExecute, there you can give in a default directory for the new process, so you don't need this ChDir stuff before and after WinExec. I even don't know if your ChDir before WinExec has any effect on WinExec.

Regards, Madshi.
0
 
mike_tippingAuthor Commented:
The program is basically a CD front end that runs variouse install routines. The paths and buttons images for these routines are set up with an ini file. I run Get Dir to find where I am at the time that add the path from the ini file onto that to get the location of the install exe I'm going to run.

I take your point that GetDir isn't the best thing to use and shell execute would be better that WinExec but the problem isn't with this, Looking at the error message I get back the Directory has been retrieved (D:\ is the root of the CD rom), it's the inhi file that isn't being read properly.

As I say this is a 98 only problem and only on a CD, works find on HD.

Any ideas?
0
 
gandalf_the_whiteCommented:
i had the same problem
my program worked on the harddisk
but failed to work after being burned on a cd

the reason is that getdir has two "modes"
if you are in a subdirectory you'll get:
F:\development\test

if you are in the root of a directory you'll get
F:\

and thats the reason you get d:\\
because your file is in the root of the cd

if you load an image with
  GetDir(0,Dir);
  image1.picture.loadfromfile(dir+'//test.jpg');
or
  GetDir(0,Dir);
  image1.picture.loadfromfile(dir+'\\test.jpg');
it works !!! --tested under d5 and win98se

but only on a fixed disk and not on a cd
(i think its the cd-filesystem)

you should accept TheNeil's answer
my solution to the problem looks nearly the same

regards
fred
0
 
TheNeilCommented:
Thanks Fred,

This was the point I was trying to get across (badly as it turns outason I put in checks to look for trailing '\' characters

The Neil
0
 
TheNeilCommented:
Oops, must swicth overwrite OFF. What I meant to say (before screwing it all up) was that this was the reason WHY I kept checking for trailing '\' characters

The Neil
0
 
gandalf_the_whiteCommented:
8-)
0
 
mike_tippingAuthor Commented:
I don't think it was the answer to the problem but the code was usefull to make the solution I came up with work properly. Ititialy I try to read the ini file from the CD if I can't (win98) I copy the ini file into the systems temp diretory and read it from there.

0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 5
  • 5
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now