The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

Im trying to read the extension of a file, is there a function like, get_extension(), or something like that?

in Engine by (37 points)

3 Answers

0 votes

No, but if you're using a Directory to get files, you can get the file extension from the name of the file since it's at the end of the name.

by (8,550 points)
–1 vote

once you get the full path to your file using directory, just use get_extension()
example below

func dircontents(path):
var dir = Directory.new()
if dir.open(path) == OK:
dir.list
dirbegin()
var file
name = dir.getnext()
while file
name != "":
if dir.currentisdir():
print("Found directory: " + filename)
else:
print("Found file: " + file
name)
filename = dir.getnext()
print(filename.getextension())
else:
print("An error occurred when trying to access the path.")

func onButtonpressed():
dir
contents("res://sounds/music");

by (28 points)

under scores have been removed in my above post. so please use correct functions
Thanks

+1 vote

I am a bit late to the party - but for anyone stumbling across this via search engine: Godot Strings provide the exact wished functionality by now: https://docs.godotengine.org/en/stable/classes/class_string.html#class-string-method-get-extension

by (45 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.