小弟初学,代码很乱,不好意思。以下是我的代码,目的是找出str中所有扩展名为.bat的文件名,如本例中应是abcde.bat和poiut.bat。但我写得这段代码只能找出第一个.bat的文件名abcde.bat,后面一个就不行了,怎样才能把它们都找出来并且都保存下来呢?注意事先不知道有多少个.bat,本例有2个只是举的例子。
急啊!求各位快帮忙吧!
非常感谢!
var
  Form1: TForm1;
  str: string;
  MyChar: string;
  p, pTmp: Integer;
  L: Integer;
  pLen: Integer;
  MyStrList: TStringList;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
  MyStrList:=TStringList.Create;
  str:='abcdefghi=abcde.bat qwertyioj/poiut.bat';
  L:=Length(str);
  p:=pos('.bat',str);
  pTmp:=p;
  MyChar:=copy(str,p+4,1);
  if (MyChar='"') or (MyChar=' ') or (MyChar='>') then
    begin
      while (pTmp>0) and (str[pTmp]<>'/') and (str[pTmp]<>'\') and
                         (str[pTmp]<>'=') and (str[pTmp]<>'"') do
        begin
          Dec(pTmp);
        end;
          if (str[pTmp]='/') or (str[pTmp]='\') or
             (str[pTmp]='=') or (str[pTmp]='"') then
            begin
              pLen:=(L-pTmp)-(L-(p+3));
              MyStrList.Add(copy(str,pTmp+1,pLen));
              delete(str,1,p+3);
            end;
    end;
end;

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      MyStrList:=TStringList.Create;
      str:='abcdefghi=abcde.bat qwertyioj/poiut.bat';
      while(pos('.bat',str)>0) do//加一个循环就可以了/////////////
      begin
       p:=pos('.bat',str);
       L:=Length(str);
       pTmp:=p;
       MyChar:=copy(str,p+4,1);
       if (MyChar='"') or (MyChar=' ') or (MyChar='>') then
        begin
          while (pTmp>0) and (str[pTmp]<>'/') and (str[pTmp]<>'\') and
                             (str[pTmp]<>'=') and (str[pTmp]<>'"') do
            begin
              Dec(pTmp);
            end;
              if (str[pTmp]='/') or (str[pTmp]='\') or
                 (str[pTmp]='=') or (str[pTmp]='"') then
                begin
                  pLen:=(L-pTmp)-(L-(p+3));
                  MyStrList.Add(copy(str,pTmp+1,pLen));
    //              delete(str,1,p+3);
                end;
        end;
        delete(str,1,p+3);//避免死循环,如abcde.bat_qwertyioj
      end;//////////
    end;
      

  2.   

    to yang6130(2.5G) 
    谢谢!请你再告诉我,如果要在EDIT里显示这些文件名,应该在哪里加代码?如果要在别的地方调用这些文件名呢?
    请快再告诉我这两个问题,搞定了马上加分。
    谢谢!
      

  3.   

    lou shang de dui a!
      

  4.   

    to yang6130(2.5G) 
    谢谢!请你再告诉我,
    1、如果要在EDIT或MEMO里显示这些文件名,应该在哪里加代码?
    2、如果要在别的地方调用这些文件名呢?
    请快再告诉我这两个问题,搞定了马上加分。
    谢谢!
      

  5.   

    to yang6130(2.5G) 
    好像不行啊,还是只能显示出第一个文件名来!!!
    急啊!!!
      

  6.   

    edit1.text:=file.extractfilename;
      

  7.   

    to: jianke5555(剑客) 
    是啊,还是那些乱七八糟的问题,不过好像就差一点点了。
    快告诉我啊!!
      

  8.   

    怎样把得到的那些文件名全都显示出来啊,随便显示在一个EDIT或是MEMO里都行。
    我刚才试了
    memo1.Lines:=mystrlist;
    edit1.Text:=mystrlist.Text;还是只能显示出第一个文件名,
    是不是后面的根本没有存进去,还是显示方法不对?
    拜托了!
      

  9.   

    你用Edit當然只能得到一個值了.
    如果得到第二個值可以:
       Edit1.Text := mystrlist.strings[1];(第一個為strings[0]);
      

  10.   

    str:='abcdefghi=abcde.bat qwertyioj/poiut.bat ';
    字符串最后要加一个空格,否则字符串没有规律了
    yang6130(2.5G) 告诉你的方法是正确的在单击事件最后加一句 
                    memo1.Lines := MyStrList;祝你好运! ^_^