PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Need file-type identifier proggy

I'm looking for a freeware program that can determine a file's type (and thus assign an appropriate extension) based on the file's contents.

Quite a while ago my 512MB CF card was corrupted, and after a restore I have thousands (2,446 files @ 426 MB) of files that are named FILE####.CHK. A lot of them are jpgs, but there is a whole slew of other stuff in there.

Dan East
Damn..that's harsh..


I haven't heard of an app like that. Maybe just use a hex editor and do it manually?
Kevin Gelso · 17 Nov 2004, 00:40 UTC
Dan East wrote:
... and after a restore I have thousands of files that are named FILE####.CHK.


I don't think he will want to do them one by one. I mean c'mon.. he hasn't even finished Varium yet!!! :wink: :lol:
I mainly want to identify and rename the jpgs. I found TrID, but the GUI of it requires .NET, and I don't feel like downloading a 28 MB runtime to run a 33k app.

So I need a program that, given a directory, will scan every file in it, and rename the files it can positively identify so they have the proper extension.

Dan East
Well, so much for TrID. I tried the DOS version on a single file, and got the result:

Analyzing...

100.0% (.AIR) MS Flight Simulator Aircraft Performance Info (4/3)

I'm positive beyond a shadow of a doubt that I did not have an ".AIR" file on my CF card. :) I've never even used MS Flight Simulator.

Dan East
Haha..


I still think looking at everything through a hex editor may be the best solution.
I'm guessing you're just going to have to write a little script to look for files with magic bytes (if they have them) and assign the appropriate extension. I'm not sure exactly what % of the files would have nice self-identifying headers like that. All image files I've ever worked with do. It looks like .jpgs start with "0xFF 0xD8" and end with "0xFF 0xD9".

Sorry, but I don't know of or see any better options. You're a better coder than I am, though, so I'm sure you've already thought about this approach.
What about just rename all files to ".jpg", and go "View as thumbnails" in explorer. Whatever ones show pictures you know they are jpgs.

Yeah, I know it would be more fun to code something though.
IRFanView will ask if you want to rename it to the proper extension when you open it, and has a batch rename option.
Here's a simple framework that might be of assistance. Right now, it just has a very crude jpg checker.

I'll let you worry about implementing any sort of file manipulations.

It's very untested, use at your own risk, etc.

fileid.h:
Code:
// fileid.h
#pragma once

#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>


using namespace std;


//////////////////////////////////////////////////////////////////////////////////////////////////
//
// CFileIdentifier :: every potential type of file should derive its own FileIdentifier
//
//////////////////////////////////////////////////////////////////////////////////////////////////

class CFileIdentifier {

protected:
// ctor / dtor
CFileIdentifier();
virtual ~CFileIdentifier();

public:

// file functions
virtual bool isPotentialMatch(const char *filename) { return true; } // if the validation is going to be expensive, you might want to
// override this function so that it can cull out the obvious non-matchs

virtual bool validate(const char *filename) =0; // returns true if the file in question passes the validation tests

// data
string extension;

// static members
static vector<CFileIdentifier *> identifiers; // keeps tracks off all the file type identifiers
};



//////////////////////////////////////////////////////////////////////////////////////////////////
//
// CJPG_id :: identifier for jpegs
//
//////////////////////////////////////////////////////////////////////////////////////////////////

class CJPG_id : protected CFileIdentifier {
public:
// ctor / dtor
CJPG_id() { extension = ".jpg"; }
virtual ~CJPG_id() { }

virtual bool validate(const char *filename); // returns true if the file in question passes the validation tests
};


fileid.cpp:
Code:
// fileid.cpp

#include "fileid.h"

#ifndef BYTE
typedef unsigned __int8 BYTE;
#endif



//////////////////////////////////////////////////////////////////////////////////////////////////
//
// CFileIdentifier :: every potential type of file should derive its own FileIdentifier
//
//////////////////////////////////////////////////////////////////////////////////////////////////

// static members
vector<CFileIdentifier *> CFileIdentifier::identifiers; // keeps tracks off all the file type identifiers


CFileIdentifier::CFileIdentifier() {
// make sure this identifier isn't already in the list
vector<CFileIdentifier *>::iterator iter = find(identifiers.begin(), identifiers.end(), this);

if(iter == identifiers.end())
identifiers.push_back(this);
}


CFileIdentifier::~CFileIdentifier() {
vector<CFileIdentifier *>::iterator iter = find(identifiers.begin(), identifiers.end(), this);

if(iter != identifiers.end())
identifiers.erase(iter);
}



//////////////////////////////////////////////////////////////////////////////////////////////////
//
// CJPG_id :: identifier for jpegs
//
//////////////////////////////////////////////////////////////////////////////////////////////////

// This would really be a better isPotentialMatch() candidate, but writing a full-blown validator would take a while

bool CJPG_id::validate(const char *filename) {
// open up the file (I probably should be passing in an already opened file, but meh
FILE *fp = fopen(filename, "rb");

if(fp) {
BYTE buffer[2];

// get the file size
fseek(fp, ftell(fp), SEEK_END);

long file_end = ftell(fp);

if(file_end == -1) { // ftell failed for some reason
fclose(fp);
return false;
}

rewind(fp);


// check the first two bytes for 0xff and 0xd8
fread(buffer, 1, 2, fp);

if(buffer[0] == 0xff && buffer[1] == 0xd8 && file_end > 3) {

// check the last two bytes for 0xff and 0xd9
fseek(fp, file_end - 2, 0);
fread(buffer, 1, 2, fp);

if(buffer[0] == 0xff && buffer[1] == 0xd9) {
fclose(fp);
return true;
}

}

// close the file
fclose(fp);
}

return false;
}


filecheck.h:
Code:
// filecheck.h

#pragma once


#include "fileid.h"


class CExtensionFinder {
private:
// don't instantiate me
CExtensionFinder() { }
CExtensionFinder(const CExtensionFinder &blah) { }
~CExtensionFinder() { }


public:
static vector<string> find(const char *filename); // find the file extension for the given file

private:
static vector<string> results;
};


filecheck.cpp:
Code:
// filecheck.cpp

#include "filecheck.h"

// static stuff
vector<string> CExtensionFinder::results;


// find(const char *filename):: file checking function
// note: returns the extensions of all the file identifiers that validate()'ed it.
// In theory, you should only get one if your validator function is good.

vector<string> CExtensionFinder::find(const char *filename) {
results.clear(); // clear the previous results, if there were any

// get all matches
for(int i = 0; i < CFileIdentifier::identifiers.size(); ++i) {
// run it by the potential match screener
if(CFileIdentifier::identifiers[i]->isPotentialMatch(filename)) {
// validate the file now
if(CFileIdentifier::identifiers[i]->validate(filename))
results.push_back(CFileIdentifier::identifiers[i]->extension);
}
}

return results;
}


test.cpp:
Code:
#include "filecheck.h"
#include "fileid.h"

using namespace std;


int main(int argc, char *argv[]) {
// for everyone of the file types you want to check for, instantiate the appropriate file id'er
CJPG_id jpg_checker;

vector<string> results;

while(--argc > 0) {
results.clear();

printf("\n\nfile: %s\n", argv[argc]);

results = CExtensionFinder::find(argv[argc]);

if(results.size()) {

printf("potential file extensions: \n");

for(int i = 0; i < results.size(); ++i) {
printf("\t- %s\n", results[i].c_str());
}

} else {

printf("\t- no extensions found.\n");

}
}

printf("\npress enter to quit");
getchar();

return 1;
}
Thanks Andy! I'll give it a try when I have a chance.

Dan East
Not exactly what your asking but to append an extension to lots of files, copy over to your PC, then fire up a DOS shell and type:

Code:

CD /mydir/
REN *.chk *.jpg
In a Unix environment, the file command will identify the type of a file using a slew of magic number information.
I can't believe you guys wrote a script to do that.