Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Richard Newsgroups: comp.lang.pascal.delphi.misc Subject: Re: Open dialog to display directory only (D4) Date: Wed, 22 Jul 2015 19:19:19 +0200 Lines: 125 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-Trace: individual.net 4lo0E99GWQ4fEsTZf75P4gOzCwfxfb8DmGT3GOeBPYPUaXt8/T Cancel-Lock: sha1:nquttmP5H3LtRx50o4kq6C2T+9Q= In-Reply-To: Xref: csiph.com comp.lang.pascal.delphi.misc:709 On 20/07/15 23:15, P E Schoen wrote: > Hopefully there are still people reading this newsgroup... > > I am working on a new Delphi project called DupZapper which intends to > open a directory which contains many duplicate files (in this case, > email), and then find the duplicates and move them to a separate ZAP > directory. I can hard code the email directory and then use the Open > Dialog to select all the files in the folder I want to ZAP, and perhaps > that is easiest and best. But I would like to know how to navigate to > the directory in case I want to use this application on a different > computer or for a different user. My email directory is: > > C:\Users\paul_000\AppData\Local\Microsoft\Windows Live Mail\Peschoen (paul) > > It would be easy enough to store this file location in a text file for > the application so it can be edited, or perhaps it is available in the > registry. I don't do much with Delphi lately and I'm rather rusty, and > want to maintain at least a modicum of familiarity and competence. > > Thanks, > > Paul This should work in D5. (I do not know if this works with D4) unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Registry; type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } FIniFile: TRegIniFile; end; var Form1: TForm1; implementation uses ShlObj, FileCtrl; var lg_StartFolder: String; {$R *.DFM} const SECTION = 'Sample'; procedure TForm1.FormCreate(Sender: TObject); begin FIniFile := TRegIniFile.Create('Sample Registry'); Edit1.Text := FIniFile.ReadString(SECTION, 'HandleFolder', 'No folder selected'); end; function BrowseForFolderCallBack(Wnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer stdcall; begin if uMsg = BFFM_INITIALIZED then SendMessage(Wnd,BFFM_SETSELECTION, 1, Integer(@lg_StartFolder[1])); result := 0; end; function BrowseForFolder(const browseTitle: String; const initialFolder: String =''): String; var browse_info: TBrowseInfo; folder: array[0..MAX_PATH] of char; find_context: PItemIDList; begin FillChar(browse_info,SizeOf(browse_info),#0); lg_StartFolder := initialFolder; browse_info.pszDisplayName := @folder[0]; browse_info.lpszTitle := PChar(browseTitle); browse_info.ulFlags := BIF_RETURNONLYFSDIRS; browse_info.lpfn := BrowseForFolderCallBack; find_context := SHBrowseForFolder(browse_info); if Assigned(find_context) then begin if SHGetPathFromIDList(find_context,folder) then result := folder else result := ''; end else result := ''; end; procedure TForm1.Button1Click(Sender: TObject); begin Edit1.Text := BrowseForFolder('Select folder'); if (DirectoryExists(Edit1.Text)) then FIniFile.WriteString(SECTION, 'HandleFolder', Edit1.Text); end; procedure TForm1.FormDestroy(Sender: TObject); var Cleanup: TRegistry; key: string; begin key := FIniFile.FileName; FIniFile.Free; // make sure we don't leave junk in the registry behind. Cleanup := TRegistry.Create; try Cleanup.DeleteKey(key); finally Cleanup.Free; end; end; end. // EOF