como buscar o endereço de um arquivo pelo nome usando delphi

Delphi

08/07/2016

tenho a seguinte sistuação

Tenho um instalador com o arquivo .exe + BD access
o instalador cria um atalho do executável na area de trabalho

Problema:
preciso fazer uma rotina que enxergue o local do arquivo pelo nome para que eu possa fazer a conexão.
motivo: o BD está criptografado e eu queria fazer algo automatizado para que o o usuario nao precisasse buscar o arquivo, pois quero dixa-lo inacessivel.
como faço isso:
Algo do tipo
Busca 'nomedoarquivo' retorna 'endereço' e depois conecta o BD.

Obrigada!!!
Cristina Silva

Cristina Silva

Curtidas 0

Melhor post

Raylan Zibel

Raylan Zibel

08/07/2016

Tente:

uses FileCtrl;

//-----selecionando a pasta inicial-------------------------------------------//
procedure TForm1.BitBtnSelecionarDiretorioClick(Sender: TObject);
const
  SELDIRHELP = 1000;
var
  dir: string;
begin
  if SelectDirectory(dir, [sdPerformCreate, sdPrompt], SELDIRHELP) then
    edtDir.Text := dir;
end;

//-----retornando lista de pastas e subpastas---------------------------------//
procedure TForm1.GetSubDirs(const sRootDir: string; slt: TStrings);
var
  srSearch: TSearchRec;
  sSearchPath: string;
  sltSub: TStrings;
  i: Integer;
begin
  sltSub := TStringList.Create;
  slt.BeginUpdate;
  try
    sSearchPath := AddDirSeparator(sRootDir);
    if FindFirst(sSearchPath + '*', faDirectory, srSearch) = 0 then
      repeat
        if ((srSearch.Attr and faDirectory) = faDirectory) and
          (srSearch.Name <> '.') and (srSearch.Name <> '..') then
        begin
          slt.Add(sSearchPath + srSearch.Name);
          sltSub.Add(sSearchPath + srSearch.Name);
        end;
      until (FindNext(srSearch) <> 0);

    FindClose(srSearch);

    for i := 0 to sltSub.Count - 1 do
    begin
      GetSubDirs(sltSub.Strings[i], slt);
      Application.ProcessMessages;
    end;
  finally
    slt.EndUpdate;
    FreeAndNil(sltSub);
  end;
end;

//-----adicionando barra no final do caminho da pasta-------------------------//
function TForm1.AddDirSeparator(dir: string): string;
begin
  if Copy(dir, length(dir) - 1, 1) = '\' then
    Result := dir
  else
    Result := dir + '\';
end;

//-----procurando arquivo em cada pasta da lista------------------------------//
procedure TForm1.BitBtnProcurarArquivoClick(Sender: TObject);
var
  slDir: TStrings;
  i: Integer;
  SR: TSearchRec;
  Numero: Integer;
begin
  Screen.Cursor := crHourGlass;
  try
    slDir := TStringList.Create;
    GetSubDirs(edtDir.Text, slDir);
    for i := 0 to slDir.Count - 1 do
    begin
      Numero := FindFirst(AddDirSeparator(slDir[i]) + edtNomeArquivo.Text,
        faAnyFile, SR);
      if Numero = 0 then
        if (SR.Attr and faDirectory) <> faDirectory then
        begin
          lblEndereco.Caption := 'Endereço: ' + AddDirSeparator(slDir[i]) + SR.Name;
          ShowMessage(lblEndereco.Caption);
          Break;
        end;
    end;
  finally
    slDir.Free;
    Screen.Cursor := crDefault;
  end;
end;


[img]http://projetosr.com.br/share/form_findfile.PNG[/img]
GOSTEI 1

Mais Respostas

Raylan Zibel

Raylan Zibel

08/07/2016

Cristina, você saberia a pasta inicial de onde procurar?
GOSTEI 0
Cristina Silva

Cristina Silva

08/07/2016

Então, vai pra pasta padrao de instalação de programas do windows.
GOSTEI 0
Cristina Silva

Cristina Silva

08/07/2016

Muito obrigada!!! vou testar!!
GOSTEI 0
Raylan Zibel

Raylan Zibel

08/07/2016

Muito obrigada!!! vou testar!!


Deu certo?
GOSTEI 0
Cristina Silva

Cristina Silva

08/07/2016

Sim, mas eu encontrei uma forma mais facil:

declarando SWSystem na uses
e no procedimento coloquei uma variavel caminho declarada como: shortstring;
e recebe
caminho := gsAppPath;


Pronto =)
GOSTEI 1
POSTAR