Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a better way to check file size using SCP before downloading?
    primarykey
    data
    text
    <p>I have this code to download a file from a remote machine, but I want to limit it to files that are less than 5MB.</p> <p>So far the code works. but is there a better way to check the filesize before downloading?</p> <pre><code>Net::SCP.start(hname, uname, :password =&gt; pw ) do|scp| fsize=scp.download!("#{remdir}/#{filname}").size;puts fsize scp.download!("#{remdir}/#{filname}", "#{filname}") do |ch, name, sent, total| File.open(lfile, 'a'){ |f| f.puts "#{name}: #{(sent.to_f * 100 / total.to_f).to_i}% complete"} #puts "Sending : #{(sent.to_f * 100 / total.to_f).to_i}% complete" #puts "#{name}: #{sent}/#{total}" #print "\r#{name}: #{(sent.to_f * 100 / total.to_f).to_i}%" end end </code></pre> <p>Does this cause any problem if I am use it for large files?</p> <pre><code>fsize=scp.download!("#{remdir}/#{filname}").size;puts fsize </code></pre> <p>This page says the file will be returned as a string: <a href="http://ruby.about.com/od/ssh/ss/netscp_6.htm" rel="nofollow">http://ruby.about.com/od/ssh/ss/netscp_6.htm</a></p> <p><strong>Update</strong>: I tried SFTP aswell. first it did not work for full path to file. and secondly it did not do what i wanted. so was using scp.download!().size. i know i am doing the download twice :(</p> <pre><code>require 'net/sftp' # did not take full path "/user/myname/filename" remote_path="somefile.txt" Net::SFTP.start(hname, uname, :password =&gt; pw) do |sftp| attrs = sftp.stat!("rome_desc.txt") ; puts attrs # gave # ☼ ↨?% (? '→ ??Q{ ?Qt;? sftp.stat!(remote_path) do |response| puts response #returned no such file (2) # but did not do below upload operation. unless response.ok? sftp.upload!("#{filname}", remdir) end end end </code></pre> <p><strong>Update:2 Solution</strong> found the solution using the the comments provided from below users and after searching the net.</p> <pre><code> Net::SFTP.start(hname, uname, :password =&gt; pw) do |sftp| #, :verbose =&gt; Logger::DEBUG sftp.dir.glob("./#{remdir}", "#{filname}") do |entry| p entry.name file_size=entry.attributes.size; file_size = '%.2f' % (("#{file_size}").to_f / 2**20) ; File.open(lfile, 'a'){ |f| f.puts "File size is #{file_size} mb"} if file_size &lt; file_size_param then sftp.download!("#{remdir}/#{filname}", filname) else File.open(lfile, 'a'){ |f| f.puts "File size is greater than #{file_size_param} mb. so can not Download File"} end end end </code></pre> <p>used .attributes.size to obtain the file size and perform the download operation by checking the filesize.</p> <pre><code> sftp.dir.glob("./#{remdir}", "#{filname}") do |entry| p entry.name file_size=entry.attributes.size </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload