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
LVL 13
PillalamarriVenkateswaraRaoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dagan HooverDeveloperCommented:
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

0
rfwoolfCommented:
..assuming the files aren't encrypted and are in ascii format, right?
0
Dagan HooverDeveloperCommented:
A big assumption =P Basically an example of a text file without an extension..
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

PillalamarriVenkateswaraRaoAuthor Commented:
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.
0
Dagan HooverDeveloperCommented:
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?
0
rfwoolfCommented:
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.
0
Mike LittlewoodEngineerCommented:
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.
0
Geert GOracle dbaCommented:
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'
0
PillalamarriVenkateswaraRaoAuthor Commented:
I solved my problem using below code.
Thanks

ExecuteFile:=q5.FieldByName('FilName').AsString;
FillChar(SEInfo, SizeOf(SEInfo), 0) ;
SEInfo.cbSize := SizeOf(TShellExecuteInfo) ;
Inc(k);
with SEInfo do
begin
fMask := SEE_MASK_NOCLOSEPROCESS;
Wnd := Application.Handle;
lpFile := PChar(ExecuteFile) ;
nShow := SW_SHOWNORMAL;
end; 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.