Re: copying files w/ GetWindowsDirectory

[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]

Posted by webmaster Guido on November 06, 2001 at 20:31:51:

In Reply to: copying files w/ GetWindowsDirectory posted by p12174 Doug on November 03, 2001 at 09:31:59:

: i have some code that i've been working on, utilizing the "GetWindowsDirectory" parameters/functions, etc..

: the code that i've been working with, is for like opening a file in the \system32 directory of the end user(s) system, without actually knowing the user(s) drive/directory that they installed nt4/2000/xp to..
: (like they could be using the D: drive, as well as a different name, than the default for their nt4/2000/xp installation.

: here's my sample code:

: procedure TForm1.Button1Click(Sender: TObject);
: var
: WinName : String; WindowsDir : Array[0..Max_Path] of Char;
: begin
: GetWindowsDirectory(WindowsDir, Max_Path);
: WinName := StrPas(WindowsDir);
: Memo1.Lines.LoadFromFile(WinName + '\system32\filename.txt');
: Label1.Caption := WinName + '\system32\filename.txt';
: end;

: end.

: what i'm trying to figure out is this.. how to use the GetWindowsDirectory, but instead of opening a file, "copying" a predetermined/specific file to the "\system32" directory of the users WindowsDir, etc..

: any suggestions?

: i'd love to find a reference somewhere or another on the "GetWindowsDirectory" functions, etc.. and others or similar one's in it's class as well..
: (i can't find any reference to it in my Object pascal language reference, or any of my other delphi books that i have..


For opening a file from a certain location, copying a file to a certain location or deleting a file at a certain location, essentially the technique is the same:

1. Get the directory info with a function like GetWindowsDirectory, or compose it yourself
(such as Dest := 'C:\TestDir').

2. Append a backslash if the last character is not a backslash. That's because functions like GetWindowsDirectory give the directory without a backslash, EXCEPT if it is the root of a disk. Thus, in the classical situation you get C:\Windows -- but if Windows is installed in the root of drive C, you get C:\

3. If necessary, append some more stuff to the destination directory, like "System32\" in your example.

4. Append the name of the file to handle.

5. Do your stuff (open, copy, delete...) and maybe give some feedback if it failed.

An example:

procedure TForm1.Button1Click(Sender: TObject);
  Source, FileName, DestDir, Destination : string;
  Buff: array[0..Max_Path] of Char;
  OK: Boolean;
  // Set up destination directory, for example:
  GetWindowsDirectory(Buff, Max_Path);
  DestDir := StrPas(Buff);
  // Set up full path of destination
  if DestDir[Length(DestDir)] <> '\' then
    DestDir := DestDir + '\';
  DestDir := DestDir + 'System32\';
  Source := 'C:\Test\TestFile01.dat';
  // ExtractFileName(Location) returns the leftmost characters of Location,
  // starting with the first character after the colon or backslash that 
  // separates the path info from the name and extension
  FileName := ExtractFileName(Source);
  Destination := DestDir + FileName;
  // CopyFile(lpSource, lpDest, FailIfExists) will NOT overwrite the testination
  // if it already exists, if the last parameter FailIfExists is TRUE. It will also
  // return FALSE in that case. If you always want to overwrite the destination,
  // specify FALSE as the last parameter.
  OK := CopyFile(PChar(Source), PChar(Destination), TRUE);
  // Let's check if everything went all right
  if OK then
    ShowMessage('File ' + Source + ' was copied to ' + Destination)
    ShowMessage('Error: copy of ' + Source + ' to ' + Destination + ' failed.');

Tip: GetWindowsDirectory, GetSystemDirectory, FileCopy and many more are documented in the "Win32 Programmer's Reference", usually accessible as the last topic on the "Contents" that you get when selecting Help | Contents in Delphi. The "Win32 Api" helpfiles, such as win32.hlp, usually are located in the directory "Program Files\Common Files\Borland Shared\MSHelp".

Related Articles and Replies

[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]