You are not logged in.
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
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
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
By the way, you were very close in your Script
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
Offline
Yeah! Great, man, it worked!!
Thank you!
Offline