Board index » delphi » How to find Windows directory?

How to find Windows directory?

Quote
Mike McGraw wrote:
> I know that 99 percent of windows users have the \windows folder on
> the C: drive.  Is there an easy way to find the drive directory of
> Windows?  I know I can start with the C:\ drive and work up looking
> for msdos.sys and then parse it.  I don't want to search the registry.

> Is there an easier way?

Yes. There should be an environment variable called windir which indicates
the Windows directory. Use GetEnv.

--
Did you expect a meaningful signature here? Optimist.

 

Re:How to find Windows directory?


Quote
mmcg...@funky.org (Mike McGraw) wrote:
>I have a utility I wrote to run in a dos box,  I decided I want to
>allow (not necessary for defaults) a config or ini file for it.  I
>want to place the config file in thw \windows folder.

>I know that 99 percent of windows users have the \windows folder on
>the C: drive.  Is there an easy way to find the drive directory of
>Windows?  I know I can start with the C:\ drive and work up looking
>for msdos.sys and then parse it.  I don't want to search the registry.

Finally, at the very end we get a hint about which version of Windows
you're asking about.  :-)

For Win95/98, an environment variable named "winbootdir" holds the
location of the Windows directory.  Note that the variable name is all
lowercase letters.

For Windows NT, presumably there is something similar.

For earlier versions of Windows (v3.x and up), "windir" holds this
information, IIRC.

Obviously, you will need to run your utility in a DOS box under Windows,
as these environment variables do not exist in a plain DOS boot.

--
Robert B. Clark
Visit ClarkWehyr Enterprises On-Line at http://www.iquest.net/~rclark/ClarkWehyr.html

Re:How to find Windows directory?


Quote
>Yes. There should be an environment variable called windir which indicates
>the Windows directory. Use GetEnv.

If that doesn't work, modify code below to get what you need.

Robert

var b,
      env : word;
begin
Env := MemW[PrefixSeg:$2C];
b:=0;
repeat
  while Mem[Env:b] <> 0 do
   begin
    Write(char(Mem[Env:b]));
    inc(b);
   end;
  inc(b);
  WriteLn;
until Mem[Env:b] = 0;
end.

Re:How to find Windows directory?


Quote
Frederic wrote:
> Yes. There should be an environment variable called windir which indicates
> the Windows directory. Use GetEnv.

But GetEnv won't find windir, because it always upper-cases the variable
name.  Hence, GetEnv('windir') will return the value of WINDIR, not the
value of windir.

     - Rich

Re:How to find Windows directory?


Quote
Rich Pasco <pa...@best.com> wrote:
>Frederic wrote:

>> Yes. There should be an environment variable called windir which indicates
>> the Windows directory. Use GetEnv.

>But GetEnv won't find windir, because it always upper-cases the variable

[OT]

The getenv() function in C _is_ case sensitive.  Perhaps that's what
Frederic had in mind.

[end OT]

Quote
>name.  Hence, GetEnv('windir') will return the value of WINDIR, not the
>value of windir.

There are various ways around that.  One that I liked to use in batch
files was

        @echo off
        if %1!==! %0 windir
        set | find /v "CMDLINE=" | find "%1="
        if not errorlevel 1 echo Found it!

For TP/BP, use the EnvStr() function rather than GetEnv():

(* Demonstrates how to get a case-sensitive environment variable.   *)
(* 01 July 1999 Robert B. Clark, Turbo Pascal 7.0                   *)

Uses Dos;

VAR
   i, chk  : Integer;
   evar    : String;

BEGIN
     evar := 'windir';
     chk := 0;

     { Do not append '=' to the end of evar! }
     { Case is significant }

     if ParamCount > 0 then
        evar := ParamStr(1);

     { Skip through the environment table looking for 'evar' }

     for i := 1 to EnvCount do
     begin
         if Pos(evar+'=', EnvStr(i)) <> 0 then
         begin
              Writeln('Found ', evar, ':  ', EnvStr(i));
              chk := 1;
              break
         end
     end;

     if chk = 0 then
         Writeln(#7, evar, ' not found!')
END.

--
Robert B. Clark
Visit ClarkWehyr Enterprises On-Line at http://www.iquest.net/~rclark/ClarkWehyr.html

Re:How to find Windows directory?


Quote
"Robert B. Clark" wrote:
> The getenv() function in C _is_ case sensitive.  Perhaps that's what
> Frederic had in mind.

No :-)

Quote
> > Hence, GetEnv('windir') will return the value of WINDIR, not the
> >value of windir.

By the way: on my system, windir is all lowercase:

windir=C:\WINDOWS
PATH=C:\;C:\WINDOWS;C:\WINDOWS\COMMAND;C:\LINKS;C:\PATH
PROMPT=$E[1;37;40m$P$G
CMDLINE=mode con codepage select=437

--
writeln('I am proud to hate C++');

Other Threads