Solved

Win 98 problem with get Dir & ini files

Posted on 2000-02-25
15
214 Views
Last Modified: 2010-04-04
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
Comment
Question by:mike_tipping
  • 5
  • 5
  • 2
  • +3
15 Comments
 
LVL 3

Expert Comment

by:ckaneta
ID: 2558438
Maybe you have an extra backslash in the path.
0
 
LVL 5

Expert Comment

by:TheNeil
ID: 2558456
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
 

Author Comment

by:mike_tipping
ID: 2558498
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
 
LVL 5

Expert Comment

by:TheNeil
ID: 2558521
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
 

Author Comment

by:mike_tipping
ID: 2558576
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
 
LVL 5

Expert Comment

by:TheNeil
ID: 2558604
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
 

Author Comment

by:mike_tipping
ID: 2558675
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 13

Expert Comment

by:Epsylon
ID: 2559760
What happens when you use GetCurrentDir instead of GetDir?
0
 
LVL 20

Accepted Solution

by:
Madshi earned 80 total points
ID: 2560555
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
 

Author Comment

by:mike_tipping
ID: 2564160
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
 
LVL 3

Expert Comment

by:gandalf_the_white
ID: 2564735
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
 
LVL 5

Expert Comment

by:TheNeil
ID: 2564765
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
 
LVL 5

Expert Comment

by:TheNeil
ID: 2564872
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
 
LVL 3

Expert Comment

by:gandalf_the_white
ID: 2565031
8-)
0
 

Author Comment

by:mike_tipping
ID: 2580465
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now