Question

I'm using ruby on rails and I'm wondering if there is a way for me to open a file in which I am missing the last few characters of the file name.

For example: my file name is "/folder/files/A2222_revA.pdf". However, these file names are changing from A2222_revA to A2222_revB to A2222_revC over time. I have the path and non-changing name (A2222) saved as variables, and my goal is to just open any file with the name A2222... since there will never be more than one file with that specific prefix.

Is there a way to just have next file name in alphanumeric order open within a specified folder?

I have a controller to view these files that looks like this:

  def view
    @drawing = Drawing.find(params[:id])
    @path = @drawing.draw_path(@drawing)

    send_file( @path,
    :disposition => 'inline',
    :type => 'application/pdf',
    :x_sendfile => true )
  end 

and the draw_path method looks like this:

  def draw_path(drawing)
    folder = drawing[:file_location]
    file_name = drawing[:drawing_number]
    path = "X://engineering/shop\ prints/"+folder+"/"+file_name+".pdf"
    return path
  end

Obviously this method is not complete and I will need to add more logic.

Was it helpful?

Solution 2

So if there is really only one file at any given point in time, you could

path = Dir["/folder/files/A2222_*.pdf"].first

no?

OTHER TIPS

One approach would be to get a list of files which match your prefix, e.g.:

files = Dir.glob(File.join(path, "A2222_rev*"))

Use glob function to retrieve filename then pass to File.open

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top