ParamStr and ParamCount?

I'm using this code in my application to check if there are any parameters on the commandline, and if there are any, then open the files.

  if Paramcount > 0 then
      for i := 1 to Paramcount do OpenFile(ParamStr(i));

If I pass a file to my application, f.x
hahaha.txt or something, I always get
HAHA~1.TXT or something like this.
Is this ParamStr function some old crap from Win3.1?
Is there a better way do to this?
LVL 3
hagurAsked:
Who is Participating?
 
intheConnect With a Mentor Commented:
cheers :-)

for i:= 0 to ParamCount do
begin
  if FindFirst( ParamStr( i), faAnyFile, SearchRec) = 0 then
    Memo1.Lines.Add( ExtractFilePath( ParamStr( i)) + SearchRec.Name; { = The LongFileName}
  FindClose( SearchRec);
end;  

Regards Barry
0
 
hagurAuthor Commented:
Edited text of question.
0
 
sandman_brCommented:
Try passing the filename between parentheses. If you are using APP.EXE C:\BIGFILENAME.TXT, try APP.EXE "C:\BIGFILENAME.TXT" and see what happens.
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
intheCommented:
When you are putting a file name in an argument as a param, you have to deal with spaces.  Otherwise 'C:\My Documents' will be treated as 'C:\My' and 'Documents' (two seperate arguments).
 
There are a couple of ways to deal with this.  One is to wrap each argument with double quotes as sandman br said like  ("C:\My Documents").  The other way is to grab the ShortFileName equivalent via GetShortPathName() api or even could do it via findfirst functions:
for i:= 0 to ParamCount do
begin
  if FindFirst( ParamStr( i), faAnyFile, SearchRec) = 0 then
    Memo1.Lines.Add( ExtractFilePath( ParamStr( i)) + SearchRec.Name; { = The LongFileName}
  FindClose( SearchRec);
end;  
0
 
hubdogCommented:
paramstr has a bug(i think)
it can not process the param include space like inthe have said.

I solve this problem by use getcommandline api to get the whole paramstr,and use  string handle routines to process it by myself.


regards,hubdog
0
 
hagurAuthor Commented:
Thanks for your comments guys.

Inthe: You findfirst example works well for me.  Post your comment as an answer.
0
All Courses

From novice to tech pro — start learning today.