Working with Files


  Finding the First File by Type:
 
            The code listed below will return the first file in the selected directory that has the
            desired file extension.
            Declare a variable in the implementation section.
            Like this:
            Var
            SearchRec: TSearchRec;
 

            function TForm1.GetFirstFile : String;
            Var
            ErrorCode : LongInt;
            begin
                ErrorCode := FindFirst('C:\graphics\'+'*.bmp', faAnyFile, SearchRec);
                If ErrorCode = 0 then //No Error occurred
                 Result := SearchRec.Name //returns the first filename found
                else //An error occurred or no files in directory with the specified extension
                 Result := ''; //Return an empty string on error
            end;


 Get the Next File:
 
            IMPORTANT:
            Code listed below needs to be called after the GetFirstFile function is called.
            If this is not done you will get error message.

            function TForm1.GetNextFile : String;
            begin
             //SearchRec knows what to look for because it was defined in the previous procedure
             if (FindNext(SearchRec) = 0) then //if the FindNext function returns 0 a file was found
              Result := SearchRec.Name //Returns the file name
             else //No more files were found or an error occurred
              begin
                FindClose(SearchRec); //Free the SearchRect Structure
                Result := ''; //Return a blank string if no more files are found
              end;
           end;



 

  Iterating Trough the Directory:
 
            The code listed below iterates through the directory and returns the file names
            with the specified extension. The code listed below works in conjunction with
            the code listed above.
            The Code Below adds each File name to a StringGrid

         procedure TForm1.GetTheFileNames; 
         var
          FaxFileName : String;
          RowID : Longint;
         begin
           RowID := 1; //Set RowID to 1, the first Row in the Grid
           FaxFileName := GetFirstFaxFile; //Get the first file Name

             With StringGrid1 do
             begin
              If FaxFileName <> '' then //If a File was found
              begin
                 Cells[0,RowID] := FaxFileName; //Add it to the Grid
              end;
 
                  While FaxFileName <> '' do //As long as a file is found, add them to the grid
                  begin
                    FaxFileName := GetNextFaxFile; //Ge the Next File
                    If FaxFileName <> '' then //If a file was found
                    begin
                     Inc(RowID);
                     RowCount := RowID+1; //Add A new Row to The Grid
                     Cells[0,RowID] := FaxFileName; //Add the File Name
                    end;
                  end;
             end;
         end;
 
 
 Starting an External Application
            Starting an external Application from Delphi using the ShellExecute API call. To
            make it work you must  add the  ShellAPI unit declared to the uses clause .

            Procedure Form1.Button1Click(Sender : TObject);
               Var
                ErrorCode : Integer;
                FileName : String;
                begin
 
                FileName := Edit1.Text;//Get the file name from the Edit box

                ErrorCode := ShellExecute(0,nil,PChar(FileName),nil,'',SW_RESTORE);
                //Trap possible errors and show a message
                    Case ErrorCode of
                       0: Begin
                              MessageDlg('System is out of memory or resources!', mtError, [mbOk],0);
                              Exit;
                          end;

                       ERROR_FILE_NOT_FOUND: begin
                                                                               MessageDlg('File: ' +FileName + ' was not found!', mtError,
                                                                               [mbOk],0);
                                                                               Exit;
                                                                             end;

                       ERROR_PATH_NOT_FOUND: begin
                                                                                  MessageDlg('Path For:' +FileName + ' was not found!',
                                                                                  mtError,
                                                                                  [mbOk],0);
                                                                                  Exit;
                                                                               end;

                       ERROR_BAD_FORMAT: begin
                                                                         MessageDlg('File: ' +FileName + ' Invalid File or Error in
                                                                         EXE!', mtError, [mbOk],0);
                                                                         Exit;
                                                                      end;

                       SE_ERR_ACCESSDENIED: begin
                                                                            MessageDlg('File: ' +FileName + ' File Access Deniede!', mtError,
                                                                            [mbOk],0);
                                                                            Exit;
                                                                         end;
 
                       SE_ERR_ASSOCINCOMPLETE: begin
                                                                                    MessageDlg('File: ' +FileName + ' no Asscociation
                                                                                    found!', mtError, [mbOk],0);
                                                                                    Exit;
                                                                                  end;

                       SE_ERR_DDEBUSY: begin
                                                                MessageDlg('File: ' +FileName + ' DDE is Busy!', mtError, [mbOk],0);
                                                                Exit;
                                                             end;

                       SE_ERR_DDEFAIL: begin
                                                               MessageDlg('File: ' +FileName + ' DDE Transaction Failed!', mtError,
                                                               [mbOk],0);
                                                               Exit;
                                                            end;

                       SE_ERR_DDETIMEOUT: begin
                                                                        MessageDlg('File: ' +FileName + ' DDE Timed Out!', mtError,
                                                                        [mbOk],0);
                                                                        Exit;
                                                                      end;

                       SE_ERR_DLLNOTFOUND: begin
                                                                            MessageDlg('File: ' +FileName + ' DLL Not Found!', mtError,
                                                                            [mbOk],0);
                                                                            Exit;
                                                                         end;
 

                       SE_ERR_NOASSOC: begin
                                                                MessageDlg('File: ' +FileName + ' No Asscociation Found!', mtError,
                                                                [mbOk],0);
                                                                Exit;
                                                              end;

                       SE_ERR_OOM: begin
                                                       MessageDlg('File: ' +FileName + ' Insufficiant Memory!', mtError, [mbOk],0);
                                                       Exit;
                                                     end;

                   SE_ERR_SHARE: begin
                                                      MessageDlg('File: ' +FileName + 'Caused sharing Violation!', mtError,
                                                      [mbOk],0);
                                                      Exit;
                                                    end;
              end;//End for Case
               end;   
 

 


E-mail: