Save a file by clicking on a link

Joel P. wrote in post #1094315:

During the download, no. After the download, yes.

Yes, That was the confirmation I was expecting. Hum,If it can be done
during download,could be great. Now I have to develop such renaming
code.

Thanks for your help.

During the download, no. After the download, yes.

Joel P. wrote in post #1094315:

During the download, no. After the download, yes.

A serious problem I am having during download of files:

In the webpage there is file : “Christopher Herman BR.bmp”

But after the download I am getting two files namely -

“Christopher Herman BR.bmp” and “Christopher Herman BR.bmp.part” – this
is the reason original file getting corrupted. How such .part file is
coming?

Could you please tell me why so?

The final profile I am using:

==========================================================================

profile = Selenium::WebDriver::Firefox::Profile.new
profile[‘browser.download.dir’] = ‘C:\Documents and Settings\rakshiar\My
Documents\userdata\Rubydownloads’
profile[‘browser.download.folderList’] = 2
profile[‘browser.helperApps.neverAsk.saveToDisk’] =
“application/pdf,image/jpeg,image/pjpeg,image/gif,image/bmp,image/x-windows-bmp,image/tif,image/x-tif,image/tiff,image/x-tiff,application/x-compressed,application/x-zip-compressed,application/zip,multipart/x-zip”
profile[‘browser.download.manager.showWhenStarting’] = false
driver = Selenium::WebDriver.for :firefox, :profile => profile

==========================================================================

I am using windows-XP

@download_directory = “#{Dir.pwd}/RubyDownloads”
@current_downloads = Dir.entries( @download_directory )

def wait_for_download

#Find the newest file
difference = [1,2] - here why you took size 2 of array?
until difference.size == 1 - why 1?
  difference = Dir.entries(@download_directory) - @current_downloads -- in 

the above line what are we doing?

  sleep 0.1
end

difference = Dir.entries(@download_directory) - @current_downloads

If you subtract one array from another you get the difference. In this
case, the difference is the most recent file.

“BR.bmp.part” is the result of a temporary file being created during the
download, this is one of the reasons I said the download waiting method
needs a bit of rewriting. It should continue to look until there is only
one file difference, but a “part” file might mess that up because the
difference is then two files.
What I suspect is happening is that it’s grabbing the “.part” file
rather than waiting for the complete file, so you’ll need to exclude
files with a “.part” extension.

Joel P. wrote in post #1094622:

difference = Dir.entries(@download_directory) - @current_downloads

If you subtract one array from another you get the difference. In this
case, the difference is the most recent file.

“BR.bmp.part” is the result of a temporary file being created during the
download, this is one of the reasons I said the download waiting method

Can you do me a favor by re-writing it? I am totally confused here.

difference = Dir.entries(@download_directory).reject {|f| f =~ /.part\z/
} -
@current_downloads

difference = [1,2] - here why you took size 2 of array?

So that a variable called difference exists for the next line, and is
not 1.

until difference.size == 1 - why 1?

Because then there will be a single file, which is the one you’ve just
downloaded. More than one is venturing into the unknown :slight_smile:

  difference = Dir.entries(@download_directory) - @current_downloads

in the above line what are we doing?
The current files minus the previous files = the new file.

Arup R. wrote in post #1094617:

@download_directory = “#{Dir.pwd}/RubyDownloads”
@current_downloads = Dir.entries( @download_directory )

def wait_for_download

#Find the newest file
difference = [1,2] - here why you took size 2 of array?
until difference.size == 1 - why 1?
  difference = Dir.entries(@download_directory) - @current_downloads -- in

the above line what are we doing?

  sleep 0.1
end

Could you please answer my above said questions?

Joel P. wrote in post #1094173:

Arup R. wrote in post #1094152:

Is it possible to pause the next set of statements until the download is
not being finished?

I need to rewrite this at some point, it was a quick-fix and doesn’t
currently have failsafes. Anyway, this is what I’m using:


@download_directory = “#{Dir.pwd}/RubyDownloads”
@current_downloads = Dir.entries( @download_directory )

def wait_for_download

#Find the newest file
difference = [1,2]
until difference.size == 1
  difference = Dir.entries(@download_directory) - @current_downloads
  sleep 0.1
end
file_name = difference.first
puts "Found new file: #{file_name}"

=================================
I think below should work also:

Dir.chdir(“C:/Documents and Settings/peter/My
Documents/userdata/Rubydownloads”) do

until Dir.glob(’**/.part’)[0] != []

sleep 0.1

end

But also confused - is this the optimized one? Or could it be more
better with a more bit of engineering?
end

Almost everything can be done differently. You just have to tailor the
code to suit your way of thinking and your specific task.
As for the “best” way to do something, that will always depend on your
objectives - Do you want it… easy to make, easy to use, easy to
expand, immutable, versatile, limited, fast to run, intelligent enough
to correct user errors, etc.

This is why we “play” with code. It’s the way you’ll discover new
things, sometimes approaches no one else has ever thought of :slight_smile:

Joel P. wrote in post #1094713:

Almost everything can be done differently. You just have to tailor the
code to suit your way of thinking and your specific task.
As for the “best” way to do something, that will always depend on your
objectives - Do you want it… easy to make, easy to use, easy to
expand, immutable, versatile, limited, fast to run, intelligent enough
to correct user errors, etc.

This is why we “play” with code. It’s the way you’ll discover new
things, sometimes approaches no one else has ever thought of :slight_smile:

OMG! Still not working.

Dir.chdir(“C:/Documents and Settings/rakshiar/My
Documents/userdata/Rubydownloads”) do

                until Dir.glob('**/*.part*')[0] != []

                   sleep 0.1
                   #puts "h"

                end

Any better solution? It seems this couldn’t stop the execution of next
line,until the download finished.

I also have seen that,during download of the file - “Christopher Herman
BR.bmp” , two target folders created. The file size is 1.02 MB.

Try increasing the interval (to 1 or more for testing purposes). Perhaps
the “.part” file and the destination file are created more than 0.1
second apart from each other and it isn’t picking them up
simultaneously. That way you could use the original code, because it
should continue to wait until the two files become only 1 file.

Am 01.02.2013 11:49, schrieb Arup R.:

                 end

Any better solution? It seems this couldn’t stop the execution of next
line,until the download finished.

I did not follow this thread, but I am pretty sure that
the condition you are using does not test correctly what
you want to find out.

Consider that Dir.glob returns an array of filename strings,
so that your condition always is true. (The first element
will never be an empty array).

A tiny change to the code will fix this…

Furthermore, assuming that you want to sleep as long as
the partial file does exists, you would also have to
use while' instead ofuntil’ (or invert the condition).

General tip: test the code snippet in isolation (using irb),
with files you create or delete manually.

sleep(0.1) while Dir.glob(’(Path)/**/.part’)[0] != nil

Am 01.02.2013 14:34, schrieb Joel P.:

sleep(0.1) while Dir.glob(’(Path)/**/.part’)[0] != nil

No prompting, please! :slight_smile:

And there are better ways to test for an empty array.

unknown wrote in post #1094748:

Am 01.02.2013 11:49, schrieb Arup R.:

                 end

Any better solution? It seems this couldn’t stop the execution of next
line,until the download finished.

I did not follow this thread, but I am pretty sure that
the condition you are using does not test correctly what
you want to find out.

Consider that Dir.glob returns an array of filename strings,
so that your condition always is true. (The first element
will never be an empty array).

A tiny change to the code will fix this…

Furthermore, assuming that you want to sleep as long as
the partial file does exists, you would also have to
use while' instead ofuntil’ (or invert the condition).

General tip: test the code snippet in isolation (using irb),
with files you create or delete manually.

Yes! Exactly this I have followed and made the code working: One change
I did-

until Dir.glob(’**/.part’)== []

sleep 0.1
#puts “h”

end

Now all are set, for GOD sake:) Thanks to all of you! You people are
really love Ruby- which is proved by your continuous suggestions. It
seems I started to fall in love with “Ruby” :stuck_out_tongue:

Am 01.02.2013 14:50, schrieb Joel P.:

head.empty? = true

You probably mean

head.empty? == true

but the `== true’ part is not necessary:

study while head.empty?

:wink:

head.empty? = true

I don’t write pseudo-code carefully :slight_smile: