#1 2006-07-07 15:35

Galla
Member
Registered: 2006-07-07
Posts: 2

Giving the right extension according to file type

I've got some pdf and zip files with an erroneous html extension in a folder with other (truly) html files.
I'm trying to renaming those files looking at the first byte of the content ('%PDF' for pdf and 'PK' for zip files) and choosing the right extension accordingly.
I tried a Pascal script like this:

var
  FirstLine: String;
begin
  FirstLine := FileReadLine(FileName, 1);
  if (Pos('%PDF', FirstLine) = 1) then
    WideChangeFileExt(FileName, 'pdf')
end.

but it doesn't work :-(
Can you help me?

thanks,
Galla

Offline

#2 2006-07-07 16:14

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,370

Re: Giving the right extension according to file type

Gala,

You don't even need to use PascalScript rule! There is a meta tag that can detect a correct extension for over 70 different data file formats! You can access it via the Insert rule, by clicking on a little button with an icon of a lightning, and at the very bottom you'll see tag called "FileSig_Extension".

Simply use Extension rule, and paste ":FileSig_Extension:" as a new extension, that will put a correct extension for your PDFs and ZIPs smile

EDIT:
Unfortunately, it will not detect any extension for real HTML files, because they are text files and don't contain any signature... for that reason, use the script below.

Offline

#3 2006-07-07 16:24

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,370

Re: Giving the right extension according to file type

By the way, you were very close in your Script smile
Here is your code with few modifications:

var
  FirstLine: String;
begin
  FirstLine := FileReadLine(FilePath, 1);
  if Pos('%PDF', FirstLine) = 1 then
    FileName := WideChangeFileExt(FileName, '.pdf');
end.

First of all, you used FileName to read the line from the file, so nothing would be actually read because file would not be found! FileName variable does not contain the whole path, but FilePath variable does. And another little mistake was in WideChangeFileExt() function - it doesn't change the extension of the variable that you pass to it as a parameter, but it returns the new variable with a the changed extension. Tricky, but that's how it works wink

Offline

#4 2006-07-10 09:36

Galla
Member
Registered: 2006-07-07
Posts: 2

Re: Giving the right extension according to file type

Yeah! Great, man, it worked!!
Thank you!

big_smile

Offline

Board footer

Powered by FluxBB