I am trying to copy some files into a directory using Ruby, i need to assure the files inside the directory not exceed 2GB. is there any way to check the total size of files inside the directory.
Right-click the file and click Properties. The image below shows that you can determine the size of the file or files you have highlighted from in the file properties window. In this example, the chrome. jpg file is 18.5 KB (19,032 bytes), and that the size on disk is 20.0 KB (20,480 bytes).
using ruby how to get number of files in a given Directory,the file count should include count from recursive directories. Eg: folder1(2 files) -----> folder2(4 files) and folder2 is inside folder1. total count for above case should be 6 files.
length; var x = File(path). lengthSync();
Just mydir
:
Dir['mydir/*'].select { |f| File.file?(f) }.sum { |f| File.size(f) }
mydir
and all subdirectories:
Dir['mydir/**/*'].select { |f| File.file?(f) }.sum { |f| File.size(f) }
However, it's the sum of exact file sizes, not the space they occupy on the disk. For disk usage, this is more accurate:
Dir['mydir/*'].select { |f| File.file?(f) }
.sum { |f| File.stat(f).then { |s| s.blocks * s.blksize } }
In Windows, You can use win32ole gem to calculate the same
require 'win32ole'
fso = WIN32OLE.new('Scripting.FileSystemObject')
folder = fso.GetFolder('directory path')
p folder.name
p folder.size
p folder.path
For Unix-flavoured OS's, it may be easiest to simply shell out to the operating system to use the du command.
For example
du -bs #=> 1214674782
is the disk space in bytes consumed by my computer's current directory and its nested subdirectories.
du ../.rvm/src -bs #=> 722526755
is the same for a specified directory. Within Ruby, you can use the Kernel#system command:
s = system("du -bs") #=> 1214674782
To obtain the bytes consumed by the current directory only (and not nested subdirectories) remove "b"
from the flags.
s = system("du -s") #=> 1301136
Note disk space consumption is somewhat more than the total of all file sizes, but that could be a better measure of what you are looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With