?
Solved

Follow up on: I want to copy the favorites to another location for restoring it later.....

Posted on 2006-06-30
2
Medium Priority
?
240 Views
Last Modified: 2010-04-05
Following up on http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21904991.html

Hi all

The person for whom i was making this program asked if it was possible for her, to choose the backup location instead of me setting the folder in advance, I managed to get the selected location in an editfield, but how to go from there?

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Folders, StdCtrls, ShellApi, FileCtrl, ShlObj, ActiveX;

type
  TForm1            =  class(TForm)
    btn1: TButton;
    btn2: TButton;
    btn3: TButton;
    edt1: TEdit;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
  private
     // Private declarations
  public
     // Public declarations
  end;

var
  Form1:            TForm1;
  lpbi: TBrowseInfo;
  pidlStart: PItemIDList;
  Malloc: IMalloc;
  whichfolder: string;
  pidlSelected: PItemIDList;

implementation
{$R *.DFM}



function Transfer(FromPath, ToPath: String): Boolean;
var  lpOpStruct:    TSHFileOpStruct;
begin

  // Clear struct
  FillChar(lpOpStruct, SizeOf(TSHFileOpStruct), 0);

  // Set params
  lpOpStruct.Wnd:=GetDesktopWindow;
  lpOpStruct.wFunc:=FO_COPY;
  lpOpStruct.pFrom:=PChar(FromPath+'\*'#0#0);
  lpOpStruct.pTo:=PChar(ToPath+#0#0);
  lpOpStruct.fFlags:=FOF_NOCONFIRMATION or FOF_SILENT or FOF_NOERRORUI or FOF_NOCONFIRMMKDIR;

  // Perform the operation
  result:=(SHFileOperation(lpOpStruct) = 0) and (lpOpStruct.fAnyOperationsAborted = False);

end;


// Import the favorites
procedure TForm1.btn2Click(Sender: TObject);
var  szFavorites:   String;
begin

  // Get favorites folder
  szFavorites:=GetFolderPath(CSIDL_FAVORITES);

  // Check folder
  if ((GetFileAttributes(PChar(szFavorites)) and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY) then
  begin
     // Transfer from backup loaction
     Transfer('d:\backup\favorites', szFavorites);
  end;

end;



//this lets the user select a backupfolder with variable name = whichfolder
procedure TForm1.btn3Click(Sender: TObject);
 var
 lpbi: TBrowseInfo;
  pidlStart: PItemIDList;
  Malloc: IMalloc;
  whichfolder: string;
  pidlSelected: PItemIDList;

begin
SHGetSpecialFolderLocation(Handle, $00, pidlStart);
  SHGetMalloc(Malloc);
  with lpbi do
  begin
    hwndOwner := Handle;
    pidlRoot := pidlStart;
    GetMem(pszDisplayName, MAX_PATH);
    lpszTitle := PChar('Kies map voor het opslaan....');
    ulFlags := $00000051;
    lpfn := nil;
  end;
  pidlSelected := SHBrowseForFolder(lpbi);
  if pidlSelected <> nil then
  begin
    if SHGetPathFromIDList(pidlSelected, lpbi.pszDisplayName) then
    whichfolder := StrPas(lpbi.pszDisplayName);
    edt1.Text:=whichfolder; //here i will have the user selected location
    Malloc.Free(pidlSelected);
  end;
  FreeMem(lpbi.pszDisplayName);
  Malloc.Free(pidlStart);

end;


// Export the favorites
procedure TForm1.btn1Click(Sender: TObject);
var  szFavorites:   String;
begin

  // Get favorites folder
  szFavorites:=GetFolderPath(CSIDL_FAVORITES);

  // Check folder
  if ((GetFileAttributes(PChar(szFavorites)) and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY) then
  begin
     // Transfer to backup location
     Transfer(szFavorites, 'd:\backup\favorites');
  end;

end;
end.
0
Comment
Question by:fdehell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 2000 total points
ID: 17021857
In the export routine (under btn1Click), just substitue the TO path with the edit field contents. The routine I gave you will create the path if it does not exit. To enhance the code, you should probably check for a blank edit field, and very that the Transfer routine is successful eg:

  if not(Transfer(frompath, topath)) then ShowMessage('export failed');


Russell

---

// Export the favorites
procedure TForm1.btn1Click(Sender: TObject);
var  szFavorites:   String;
begin

  // Get favorites folder
  szFavorites:=GetFolderPath(CSIDL_FAVORITES);

  // Check folder
  if ((GetFileAttributes(PChar(szFavorites)) and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY) then
  begin
     // Transfer to backup location
     Transfer(szFavorites, Edit1.Text);
  end;

end;
0
 
LVL 1

Author Comment

by:fdehell
ID: 17026799
Perfect thx!!!!
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 21 hours left to enroll

752 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