Peter Kiers
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(E xeDir) + 'Emu3270.emr';
CurrentFile := CfgFile;
if LoadOptions(CfgFile) then
ShowMessage('Warning: Errors found in config file: ' + CfgFile);
end;
end;
Greetings,
Peter Kiers
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(E
CurrentFile := CfgFile;
if LoadOptions(CfgFile) then
ShowMessage('Warning: Errors found in config file: ' + CfgFile);
end;
end;
Greetings,
Peter Kiers
ASKER
Oke, but is it possible to put the Command line parameter in a variable CurrentFile?
P.
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(E xeDir) + 'Emu3270.emr';
CurrentFile := CfgFile;
if LoadOptions(CfgFile) then
ShowMessage('Warning: Errors found in config file: ' + CfgFile);
end;
end;
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(
fileName:=ParamStr(i);
end;
LoadOptions(ParamStr(1));
CurrentFile := fileName;
MakeConnect();
end else
begin
ExeDir := ExtractFilePath(ParamStr(0
CfgFile := IncludeTrailingBackslash(E
CurrentFile := CfgFile;
if LoadOptions(CfgFile) then
ShowMessage('Warning: Errors found in config file: ' + CfgFile);
end;
end;
ASKER
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(E xeDir) + 'Emu3270.emr';
CurrentFile := CfgFile;
if LoadOptions(CfgFile) then
ShowMessage('Warning: Errors found in config file: ' + CfgFile);
end;
end;
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(E
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
ExtractFileExt(ParamStr(i)
is of course better than AnsiContainsStr strings to get the extension
ASKER
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.
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
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 :/
I meant ExtractFileName :/
ASKER
Yes, I don't want the path, where should i put ExtractFilename?
P.
P.
Have you tried
CurrentFile := ExtractFileName(ParamStr(1 ));
CurrentFile := ExtractFileName(ParamStr(1
ASKER
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(E xeDir) + 'Emu3270.emr';
CurrentFile := CfgFile;
if LoadOptions(CfgFile) then
ShowMessage('Warning: Errors found in config file: ' + CfgFile);
end;
end;
Peter
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(
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(E
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.
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?
if the application is opened directly, without a file being clicked, do you want the CurrentFile to have the value of CfgFile?
ASKER
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.
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(E xeDir) + '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;
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(E
CurrentFile := CfgFile;
ext:='.EMU';
for i := 0 to ParamCount do begin
if ParamStr(i)<>'' then
if AnsiContainsStr(UpperCase(
CurrentFile :=ParamStr(i);
end;
LoadOptions(CurrentFile);
MakeConnect()
if LoadOptions(CfgFile) then
ShowMessage('Warning: Errors found in config file: ' + CfgFile);
end;
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you too now it works.
Peter Kiers
Peter Kiers
procedure TForm1.Button1Click(Sender
var
i:integer;
begin
for i := 0 to ParamCount do
ShowMessage(ParamStr(i)) ;
end;