Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[Resolved] Read undefined filenames

Status
Not open for further replies.
Fot those of you who don't get the thread's title at all, let me explain what I mean... well of course, it has to do with the filename reading/opening code in Ruby RGSS, i.e. something like this:
Code:
file = File.open("Example.txt")
Well, let's say, though, I want to have variable filenames I can't line up like numbers to work with the for x in 0..n function. Instead, those might be filenames like "TestExample.txt", "TestWhatever.txt", "TestPlaceholder.txt", whatever you can think of... well, the key element is the "Test" in all of those, as I search for a method with which I can check if there's a file with this component in *.txt format... I tried the .exist? method, but I didn't succeed... I hope you can help me out with this...

EDIT: Oh, and while on it, it'd be handy to know how one'd be able to open all files in a directory in a for x in 0..n like way, one after another...

Thanks in advance; please tell me if I expressed myself unclear...
 
Use this:
Code:
entries = Dir.entries(".")
entries.delete_at(0)
entries.delete_at(0)
entries.sort!
c = 0
for i in 0...entries.size
  if File.extname(skin[i - c]) != ".txt"
    entries.delete_at(i - c)
    c += 1
  end
end
p entries
 
Hm... I could put that in use (and I don't need the "Test"-containing method anymore, maybe for good-to-know-purposes, though...), now I only need the method for deleting the ".txt" from each arrays' contents... I tried this:
Code:
    for i in 0...entries.size
      entries[i].delete(".txt")
    end
... what returned me contents without t's and x's...#

Oh, and thanks up to this point of course!
 
Use this:
Code:
entries = Dir.entries(".")
entries.delete_at(0)
entries.delete_at(0)
entries.sort!
c = 0
for i in 0...entries.size
  if File.extname(skin[i - c]) != ".txt"
    entries.delete_at(i - c)
    c += 1
  else
    entries[i - c] = File.basename(entries[i - c], ".txt")
  end
end
p entries
 
Damn, you're refering exactly to the part I took out... damn me ^_^ Well, you might take a look at what I used until now, the commented-out stuff is what I thought would work, but didn't...
Code:
  def method
    entries = Dir.entries("Directory/.")
    entries.delete_at(0)
    entries.delete_at(0)
    entries.sort!
    #for i in 0...entries.size
    #  entries[i].delete(".txt")
    #end
    return entries
  end
 
Use this:
Code:
def method
  entries = Dir.entries("Directory/.")
  entries.delete_at(0)
  entries.delete_at(0)
  entries.sort!
  for i in 0...entries.size
    entries[i] = File.basename(entries[i], ".txt")
  end
  return entries
end
PD: Sorry, I don't understand well that...
 
Status
Not open for further replies.

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top