2

I have a folder with images and they can have different formats but the name will always be unique. Is there a way to get the file extension if I know the file's name without the extension (eg. index and not index.html)? I could just check if the file exists for every extension I expect to be there but that seems like a bad solution to me.

Example: I know there is an image called PIC but I don't know the extension (could be either '.png', '.jpg' etc.) therefore I can not use the file command.

2
  • 1
    Possible duplicate of Node.js get file extension Commented May 6, 2016 at 13:41
  • 1
    I want to remove the flag but unfortunately can't. If a mod could reject that would be great Commented May 6, 2016 at 15:07

3 Answers 3

2

Well, if your running Unix based systems, this could be a workaround.

var sys = require('util')
var exec = require('child_process').exec;

function puts(error, stdout, stderr) { 
   console.log(stdout) 
}

// this is where you should get your path/filename
var filename = "login";
// execute unix command 'file'
exec("file " + filename, puts);

I tested it for a PNG file and an EJS file, both with no extensions (what wouldn't make difference). The results are below:

PNG: photo: PNG image data, 100 x 100, 8-bit/color RGB, non-interlaced

EJS (what's basically a HTML): login: HTML document, ASCII text

You can check file command line parameters to make it easier to work with the string (e.g. file -b filename). If using Windows, then you'd have to check an alternative command for file.

Hope it's somehow useful.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. I think I did not ask my question specific enough, so I updated my question.
Can you use the command dir or ls? I mean, if you can work with operation system comands, the code snipped in the answer could be adapted to the OS command (e.g. ls index* OR dir index*).
add -b and --extension to get a list of possible extensions..tested on jpg without extension gave me jpeg/jpg/jpe/jfif
0

var path = require('path')

path.extname('index.html') // returns '.html'

Here is the referenced answer

Node.js get file extension

Updated :

https://www.npmjs.com/package/file-extension npm install --save file-extension allows you to specify a filename then it will return the extension

2 Comments

As stated in my question I do not know the file's extension. I just know there is a file called 'index.SOMETHING'.
$ npm install --save file-extension here is the documentation npmjs.com/package/file-extension
0

I know I'm late to the party, but you can use the grep command in unix based systems.

ie ls | grep PIC. What this does is first give a directory listing of the working directory, and then searches for the phrase PIC from the output of the directory listing and prints it. (so the only thing that will be printed is the filename)

In Windows, use dir PIC.* /b

You can execute these commands using child_process as shown in other answers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.