Solved

Capture filename

Posted on 2006-10-30
17
268 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:peterkiers
  • 9
  • 8
17 Comments
 
LVL 28

Expert Comment

by:TName
ID: 17835578
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;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 17835720
Oke, but is it possible to put the Command line parameter in a variable CurrentFile?

P.
0
 
LVL 28

Expert Comment

by:TName
ID: 17835816
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;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 17835823
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;

0
 
LVL 28

Expert Comment

by:TName
ID: 17835984
Just a correction to what I said above:
ExtractFileExt(ParamStr(i));
is of course better than AnsiContainsStr strings to get the extension
0
 
LVL 1

Author Comment

by:peterkiers
ID: 17836003
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.
0
 
LVL 28

Expert Comment

by:TName
ID: 17836013
>You can also use ExtractFilePath(Paramstr(x))
I meant ExtractFileName :/
0
 
LVL 1

Author Comment

by:peterkiers
ID: 17836070
Yes, I don't want the path, where should i put ExtractFilename?

P.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 28

Expert Comment

by:TName
ID: 17836674
Have you tried
CurrentFile := ExtractFileName(ParamStr(1));
0
 
LVL 1

Author Comment

by:peterkiers
ID: 17836758
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
0
 
LVL 28

Expert Comment

by:TName
ID: 17836820
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.
0
 
LVL 28

Expert Comment

by:TName
ID: 17836870
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?
0
 
LVL 1

Author Comment

by:peterkiers
ID: 17836897
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.
0
 
LVL 28

Expert Comment

by:TName
ID: 17837155
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;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 17837226
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
0
 
LVL 28

Accepted Solution

by:
TName earned 500 total points
ID: 17837421
Thanks for the explanation, Peter.
If you want the filename without the path, either use:

CurrentFile :=ExtractFileName(ParamStr(i));
instead of
CurrentFile :=ParamStr(i);

or later (anytime before you use it):
CurrentFile:=ExtractFileName(CurrentFile);
0
 
LVL 1

Author Comment

by:peterkiers
ID: 17839720
Thank you too now it works.

Peter Kiers
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now