0

I have to download files from the web over several requests. The downloaded files for each request have to be put in a folder with the same name as the request number.

For example:

My script is now running to download files for request number 87665. So all the downloaded files are to be put in the destination folder Current Download\Attachment87665. So how do I do that?

Destination folder: Current Download is fixed. only need to create Attachmentxxxxxx dynamically, where xxxxxx any request number.

This is Python version of code: but I want it in a Ruby, just for your reference to understand what I am looking for

request_number = 82673

# base dir
_dir = "D:\Current Download"       

# create dynamic name, like "D:\Current Download\Attachment82673"
_dir = os.path.join(_dir, 'Attachment%s' % request_number)

# create 'dynamic' dir, if it does not exist
if not os.path.exists(_dir):
    os.makedirs(_dir)
CodeLover
  • 1,054
  • 6
  • 24
  • 40
  • 2
    You do it exactly the same way you would create a directory/file with "static" name. – Sergio Tulentsev Jan 10 '13 at 04:20
  • Converting your Python code to Ruby is a very simple and straightforward task, which you shouldn't need any help doing. Simply investigate the methods available to you in the [`File`](http://www.ruby-doc.org/core-1.9.3/File.html) and [`Dir`](http://www.ruby-doc.org/core-1.9.3/Dir.html) classes. – the Tin Man Jan 10 '13 at 15:39

2 Answers2

2

Use Dir.mkdir("#{Rails.root}/#{whatever}/#{example.join('bla')}").
http://www.ruby-doc.org/core-1.9.3/Dir.html#method-c-mkdir

ichigolas
  • 7,595
  • 27
  • 50
  • Could you please see mu updated description, hope it will help you to understand what exactly i am looking for! – CodeLover Jan 10 '13 at 07:19
2

Can multiple downloads be occurring at once? If so you need something that can randomly create the number with no chance of there being a collision.

Take a look at Ruby's Tempfile module, which is made to do what you're talking about, in particular, the open method.

A utility class for managing temporary files. When you create a Tempfile object, it will create a temporary file with a unique filename.

require 'tempfile'

file = Tempfile.new('foo')
file.path      # => A unique filename in the OS's temp directory,
            #    e.g.: "/tmp/foo.24722.0"
            #    This filename contains 'foo' in its basename.
file.write("hello world")
file.rewind
file.read      # => "hello world"
file.close
file.unlink    # deletes the temp file

Also read the document about "Explicit close" and "Unlink after creation".

No matter what you do, the more files that exist in a given folder, the longer it will take for the system, or your code, to generate a unique filename.

You could use a database to keep track of a serial number also.

And, "Generate unique filenames" talks about this problem too, with many solutions. The best is probably to use uuidgen on *nix systems.

The uuidgen command generates a Universally Unique IDentifier (UUID), a 128-bit value guaranteed to be unique over both space and time.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Yes. you are right partially. I have to download `10000 +` files through the script, from 600 requests form. Just to Audit purpose i have to keep them in a separate folder.Thus I need folder creation dynamically and the name of the folder would be `Current Download + requestnumber` as I mentioned in the description. I need to store those files permanently in the local disk for Audit. – CodeLover Jan 10 '13 at 06:40
  • 1
    If you read the docs, it lets you specify the folder name on the fly. The files can auto-delete, or not, it's up to you, and is controlled by how you set up your code. See the "Explicit close" section. – the Tin Man Jan 10 '13 at 06:58
  • `+1` for the suggestions! interesting one is `You could use a database`. here a bit explanation can i have from you? – CodeLover Jan 10 '13 at 07:09
  • Could you please see mu updated description, hope it will help you to understand what exactly i am looking for! – CodeLover Jan 10 '13 at 07:18
  • Look into database Serial data-types. They're used to generate sequences of numbers over a huge range of values, and, are often used for this sort of problem. – the Tin Man Jan 10 '13 at 15:42
  • Can you help me in the [post](http://stackoverflow.com/questions/14257012/how-to-open-pop-up-window-table-i) ? – CodeLover Jan 10 '13 at 15:54
  • I fixed the tags for that question. It's has nothing to do with Ruby or Selenium, only HTML and JavaScript. – the Tin Man Jan 10 '13 at 16:14