Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

Capture filename

Dear Experts,

In the code below when the user clicks on the file the appriopriate app starts
if the user starts the app then a defaultfile called CfgFile get loaded.
When the defaultfile gets loaded the name CfgFile is stored in the variable CurrentFile
But how to get the filename of the file the user clicks in the explorer.

procedure TMainForm.Startup;
begin
if ParamCount > 0 then
  begin
    LoadOptions(ParamStr(1));
  CurrentFile := ???????????             //how to put the filename the user clicks on in the string CurrenFile?
    MakeConnect();
  end else
    begin
    ExeDir := ExtractFilePath(ParamStr(0));
    CfgFile := IncludeTrailingBackslash(ExeDir) + 'Emu3270.emr';
    CurrentFile := CfgFile;
    if LoadOptions(CfgFile) then
    ShowMessage('Warning: Errors found in config file: ' + CfgFile);
   end;
end;

Greetings,

Peter Kiers
Avatar of TName
TName

You can check all command line parameters yourself. The first will of course be paramstr(0), the app name:

procedure TForm1.Button1Click(Sender: TObject) ;
var
   i:integer;
begin
   for i := 0 to ParamCount do
     ShowMessage(ParamStr(i)) ;
end;
Avatar of Peter Kiers

ASKER

Oke, but is it possible to put the Command line parameter in a variable CurrentFile?

P.
Yes, if CurrentFile is a string. You can also use ExtractFilePath(Paramstr(x)) if you don't want the path.
You can try something like this (Not tested!):
Replace '.PAS' with the actual extension of your file type:

procedure TMainForm.Startup;
var
   i:integer;
   fileName,ext:String;
begin
if ParamCount > 0 then
  begin
  ext:='.PAS';
  CurrentFile :='';
  for i := 0 to ParamCount do begin
     if ParamStr(i)<>'' then
       if AnsiContainsStr(UpperCase(ParamStr(i)),ext) then
         fileName:=ParamStr(i);
  end;
  LoadOptions(ParamStr(1));
  CurrentFile := fileName;
    MakeConnect();
  end else
    begin
    ExeDir := ExtractFilePath(ParamStr(0));
    CfgFile := IncludeTrailingBackslash(ExeDir) + 'Emu3270.emr';
    CurrentFile := CfgFile;
    if LoadOptions(CfgFile) then
    ShowMessage('Warning: Errors found in config file: ' + CfgFile);
   end;
end;
I allready got it:

procedure TMainForm.Startup;
begin
  if ParamCount > 0 then
  begin
    LoadOptions(ParamStr(1));
    CurrentFile := (ParamStr(1));
    MakeConnect();
  end else
  begin
    ExeDir := ExtractFilePath(ParamStr(0));
    CfgFile := IncludeTrailingBackslash(ExeDir) + 'Emu3270.emr';
    CurrentFile := CfgFile;
    if LoadOptions(CfgFile) then
      ShowMessage('Warning: Errors found in config file: ' + CfgFile);
  end;
end;

Just a correction to what I said above:
ExtractFileExt(ParamStr(i));
is of course better than AnsiContainsStr strings to get the extension
Your code is better...

Your code works fine, only when I click on the new button
I get an message:

I/O ERROR 6

this is the procedure:

procedure TMainForm.New1Click(Sender: TObject);
begin
  if socOpen then Warning1
  else begin
    LoadOptions(CfgFile);
     CurrentFile := CfgFile;
    Connect1Click(sender);
  end;
end;

How do I found out what I/O error 6 means?
And do you know how to solve this?

P.
>You can also use ExtractFilePath(Paramstr(x))
I meant ExtractFileName :/
Yes, I don't want the path, where should i put ExtractFilename?

P.
Have you tried
CurrentFile := ExtractFileName(ParamStr(1));
It works when the user clicks on the file in the explorer, but the part
where the defaultfile get loaded when the user the apllication start
I don't know where to put the code.

procedure TMainForm.Startup;
var
   i:integer;
   fileName,ext:String;
begin
if ParamCount > 0 then
  begin
  ext:='.EMU';
  CurrentFile :='';
  for i := 0 to ParamCount do begin
     if ParamStr(i)<>'' then
       if AnsiContainsStr(UpperCase(ParamStr(i)),ext) then
         fileName:=ParamStr(i);
  end;
  LoadOptions(ParamStr(1));
  CurrentFile := ExtractFileName(ParamStr(1));  
  MakeConnect();
  end else                                                     //in this part below I don't know where to put the line?
    begin
    ExeDir := ExtractFilePath(ParamStr(0));
    CfgFile := IncludeTrailingBackslash(ExeDir) + 'Emu3270.emr';
    CurrentFile := CfgFile;
    if LoadOptions(CfgFile) then
    ShowMessage('Warning: Errors found in config file: ' + CfgFile);
   end;
end;


Peter
If you want a default CurrentFile, then delete
CurrentFile :='';
and place this
CurrentFile :='SomeFile.emu';
at the beginning of the Startup procedure (before if ParamCount > 0 then !).
This will initialize CurrentFile with the default value.
Then, if paramstr(1) isn't empty, this value will get overwritten wit the name of the file that was clicked.
If not, it will keep the 'SomeFile.emu' value.
Ok, so there are no misunderstandings:
if the application is opened directly, without a file being clicked, do you want the CurrentFile to have the value of CfgFile?
That didn't work either...

When the user clicks in the explorer on the file the app get loaded and you see

just the name of the file in the title bar: Test.emu

But when the user loads the application and clicks on the toolbutton Open
a Opendialog appears and when the user clicks on the same file, the full
path of the file will appear in the title bar.


P.
I'm trying to understand what your intention is.
As I understand it:
If the app is started by clicking on an emu file, you want this file to be loaded as CurrentFile and used to configure.
If the app is started directly (exe) then the default ConfigFile should be used.
And if you tell me what you see in the title bar, that doesn't really help (how did it get there). I don't know what happens in the rest of your application ;)


procedure TMainForm.Startup;
var
   i:integer;
   ext:String;
begin
  ExeDir := ExtractFilePath(ParamStr(0));
  CfgFile := IncludeTrailingBackslash(ExeDir) + 'Emu3270.emr';
  CurrentFile := CfgFile;
  ext:='.EMU';  
  for i := 0 to ParamCount do begin
     if ParamStr(i)<>'' then
       if AnsiContainsStr(UpperCase(ParamStr(i)),ext) then
         CurrentFile :=ParamStr(i);
  end;
  LoadOptions(CurrentFile);  
  MakeConnect()      
   
  if LoadOptions(CfgFile) then
  ShowMessage('Warning: Errors found in config file: ' + CfgFile);

end;
I'm sorry but my English isn't very good, your code above works better
But I want to see the name of the file in the titlebar, without the path.
And to test it I use a toolbutton with the code:

Caption := Currentfile

Peter
ASKER CERTIFIED SOLUTION
Avatar of TName
TName

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you too now it works.

Peter Kiers