Link to home
Start Free TrialLog in
Avatar of PillalamarriVenkateswaraRao
PillalamarriVenkateswaraRaoFlag for India

asked on

Open the file which is not having any file extension

Hello All,
  We are having lots of files which are generating from ERP. Those files are not having any extenstion type. So how can open those files from our delphi application.

Thanks
Avatar of ThievingSix
ThievingSix
Flag of United States of America image

Lets say you have an extensionless file called 'test' in your C: drive.

You can use the code below like this(Just a rough example):

procedure TForm1.Button1Click(Sender: TObject);
var
  MyFile : DWORD;
begin
  MyFile := OpenFile('C:\test');
  If MyFile <> INVALID_HANDLE_VALUE Then
    begin
    WriteLineToFile(MyFile,'Hello');
    CloseHandle(MyFile);
  end;
end;
function OpenFile(FileName: String): DWORD;
begin
  Result := CreateFile(PChar(FileName),GENERIC_READ Or GENERIC_WRITE,0,nil,OPEN_EXISTING,0,0);
end;
 
function CreateFile(FileName: String): DWORD;
begin
  Result := CreateFile(PChar(FileName),GENERIC_READ Or GENERIC_WRITE,0,nil,OPEN_ALWAYS,0,0);
end;
 
function WriteLineToFile(hFile: DWORD; Output: String): Boolean;
var
  Buffer : PChar;
  BytesWritten : DWORD;
begin
  Buffer := False;
  If Pos(#13#10,Output) = 0 Then Output := Output + #13#10;
  Buffer := AllocMem(Length(Output));
  If Buffer = nil Then Exit;
  Try
    Buffer := PChar(Output);
    If WriteFile(hFile,Buffer^,Length(Output),BytesWritten,nil) Then
      begin
      Result := (Length(Output) = BytesWritten);
    end;
  Finally
    FreeMem(Buffer);
  end;
end;
 
function ReadLineFromFile(hFile: DWORD): String;
var
  OutBuffer: PByte;
  Output : String;
  BytesRead : DWORD;
  I : DWORD;
begin
  OutBuffer := AllocMem(1);
  If OutBuffer = nil Then Exit;
  Try
    I := 0;
    Output := Result;
    ReadFile(hFile,OutBuffer^,1,BytesRead,nil);
    Inc(I,BytesRead);
    Repeat
      Output := Output + Char(OutBuffer^);
      OutBuffer^ := 0;
      ReadFile(hFile,OutBuffer^,1,BytesRead,nil);
      Inc(I,BytesRead);
    Until ((I > MaxSize) Or (OutBuffer^ in [0,13]));
    Result := Output;
  Finally
    FreeMem(OutBuffer,1);
  end;
end;

Open in new window

..assuming the files aren't encrypted and are in ascii format, right?
A big assumption =P Basically an example of a text file without an extension..
Avatar of PillalamarriVenkateswaraRao

ASKER

Thanks for the information.
Here I'm using ShellExecute Function to access the file (In our example c:\test)
Here test is not having any file extension. So how can open that file.
Thanks.
Ah you mean execute it not open it for read/write.

Is the file you want to execute a text file or a file with readable text? Or is it an executable file you wish to run?
You can go into File Associations in windows where you set which programs are associated with which file extensions. For example html files are launched by your default web browser. If you install a new web-browser, it will usually change this. Same with mp3s. But here comes the problem - the file is extension-less, and I don' t think you can set a file association for an extensionless file.
Meaning, quite simply, that I don't think you can 'execute' an extensionless file.
I could be wrong.
Do you want to be able to edit it as a text file?
Have you just tried loading it directly into a TMemo?
memo1.lines.loadfromfile('c:\myfile')
Never tried it myself but I don't see why you can't.
why don't you just rename the file before opening it ?
normally when you process a erp file, you start with giving it a different extension like '.prc' (processing)
when finished you give it '.ok' or '.err'
ASKER CERTIFIED SOLUTION
Avatar of PillalamarriVenkateswaraRao
PillalamarriVenkateswaraRao
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial