• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

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
0
PillalamarriVenkateswaraRao
Asked:
PillalamarriVenkateswaraRao
  • 3
  • 2
  • 2
  • +2
1 Solution
 
ThievingSixCommented:
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
 
ThievingSixCommented:
A big assumption =P Basically an example of a text file without an extension..
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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
 
ThievingSixCommented:
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
 
mikelittlewoodCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now