Ruby Filename error? - ruby

I forked this gist from https://gist.github.com/mattdipasquale/571405
I am getting the following error:
deDUPER.rb:14:in `read': Invalid argument - /Volumes/Drobo #1 2009-2012/AMNH Video/2012/2012-01-17 Creatures of Light/Capture Scratch/Art 3:9/Capture Scratch/2012-03-09_microraptor livestream/A Cam_Microraptor livestream.mov (Errno::EINVAL)
from deDUPER.rb:14:in `block in <main>'
from deDUPER.rb:10:in `each'
from deDUPER.rb:10:in `<main>'
I think it is caused from illegal characters in the file or folder names, but i'm not sure. I don't want to change the file or folder names because they are linked to old Final Cut Pro project files that rely on referenced filepaths to keep the project intact. Does anyone have experience with this? Is there a way I can get this script to work without having to change the file or folder names?
# Define the unique method that removes duplicates
#!/usr/bin/ruby
require 'digest/md5'
library_path = ARGV[0]
hash = {}
Dir.glob(library_path + "/**/*", File::FNM_DOTMATCH).each do |filename|
next if File.directory?(filename)
puts 'Checking ' + filename
key = Digest::MD5.hexdigest(IO.read(filename)).to_sym
if hash.has_key? key
# puts "same file #{filename}"
hash[key].push filename
else
hash[key] = [filename]
end
end
hash.each_value do |filename_array|
if filename_array.length > 1
puts "=== Identical Files ===\n"
filename_array.each { |filename| puts ' '+filename }
end
end

Related

Getting error while opening file , i have created folder with book_name > chapter_name in code and then creating file

In child folder(chapter_number), i am creating file
#!/usr/bin/env ruby
require 'roo'
Dir.glob("**/*.xlsx") do |file|
xlsx = Roo::Spreadsheet.open(file)
bookname = xlsx.column(1)
cahpter_number_array = xlsx.column(2).uniq
cahpter_number_array.each do |chapter|
book_name = bookname[1] if bookname
chapter_number = chapter if (cahpter_number_array && (chapter != "Chapter"))
Dir.mkdir(book_name) unless File.exists?(book_name)
Dir.mkdir("#{book_name}/#{chapter_number}") unless File.exists?("#{book_name}/#{chapter_number}")
xlsx.column(3).each do |md|
output_name = "#{book_name}/#{chapter_number}/#{File.basename(md.partition('-').first, '.*')}.md" if (md != "Verse")
output = File.open("#{output_name}", 'w')
output << "hello"
end
end
end
Error:
`initialize': Is a directory # rb_sysopen - . (Errno::EISDIR)
Below link is my source file:
source file link
Come on now, that isn't really your code. You can't call partition on a number:
file_name = [1,2,3,4,5,6,7]
file_name.each do |md|
... md.partition('-')
so you would have gotten an error for that before getting the error you posted.
In any case, the error message is saying that outputname is set equal to "." and when ruby tries to execute File.open(".", 'w') ruby finds that "." is the name of a directory on your system, and you can't write a directory. You can witness the same error doing this:
~/ruby_programs$ mkdir my_dir
~/ruby_programs$ irb
2.3.0 :001 > File.open('my_dir', 'w')
Errno::EISDIR: Is a directory # rb_sysopen - my_dir
from (irb):1:in `initialize'
from (irb):1:in `open'
from (irb):1
from /Users/7stud/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'

Errno::EISDIR in 'initialize' : Is a directory # rb_sysopen - persistent.bak (Errno::EISDIR)

I am iterating through files in a folder to search for specific string.
There is a folder name as persistent.bak. While going through this folder, it is giving error... in 'initialize' : Is a directory # rb_sysopen - persistent.bak (Errno::EISDIR).
Dir.glob("**/*.*") do |file_name|
fileSdfInput = File.open(file_name)
fileSdfInput.each_line do |line|
if ((line.include?"DATE")
#count = #count + 1
end
end
end
your glob Dir.glob("**/*.*") matches the pattern persistent.bak
So inside your loop, you're actually trying to open the folder named persistent.bak as a file, which ruby doesn't appreciate.
Just to convince yourself, try to output the file name, you'll see it.
Simplest workaround :
Dir.glob("**/*.*") do |file|
next if File.directory? file
fileSdfInput = File.open(file)
fileSdfInput.each_line do |line|
if (line.include?"DATE")
#count = #count + 1
end
end
end

File.rename not working on Windows

Here's the code:
files = Dir.glob("*")
files.each do |file|
if File.extname(file) == ".pdf"
format = file.split(".pdf")
format = format.join("").split(" ")
format[0] = format[0].gsub(".","/")
format[0] << "_"
format[0].prepend("_")
format[-1] << ".pdf"
format = format.join("")
puts "Changed #{file} to #{format}"
File.rename(file,format)
end
end
It is running from the same directory as the files. I have tried giving the File.rename parameters absolute paths as well, by appending the result of Dir.pwd to them. Code isn't very clean as it was something I whipped up real quick, please excuse that.
I get the following error:
Changed 05.01.14 Mid-Day 1.pdf to _05/01/14_Mid-Day1.pdf
script.rb:12:in `rename': No such file or directory - (05.01.14 Mid-Day 1.pdf, _05/01/14_Mid-Day1.pdf) (Errno::ENOENT)
from script.rb:12:in `block in <main>'
from script.rb:2:in `each'
from script.rb:2:in `<main>'
If I call File.exists?(file) it comes back as true. I'm thoroughly confused on why this doesn't work.
I am on running this on Windows.
The directory structure _05/01 does not seem to exist. You need to create the directories first with FileUtils.mkdir_p before being able to move the file there.

ruby: `read': Invalid argument -(Errno::EINVAL) at File.read

I'm doing a simple script to check crc of all files...
require "zlib"
exit if Object.const_defined?(:Ocra)
files = Dir.glob("*")
File.open('dir.txt', 'a+') do |file|
file.puts files
end
File.read('dir.txt').each_line { |line|
file = File.read(line) ; nil
file_crc = Zlib.crc32(file,0).to_s(16)
puts line, file_crc
}
The problem is at the line File.read('dir.txt').each_line { |line|
I get this error:
test.rb:13:in `read': Invalid argument - 1.exe (Errno::EINVAL)
from C:/Users/Administrador/Desktop/1.rb:13:in `block in <main>'
from C:/Users/Administrador/Desktop/1.rb:12:in `each_line'
from C:/Users/Administrador/Desktop/1.rb:12:in `<main>'
PD: 1.exe is a file listed in the "dir.txt".
Have you checked that the line doesn't contain extra characters? p line.
IIRC line will contain the newline character, use line.chomp.

How to rename file and directory in a zip file using rubyzip

I'm trying to rename a file and a directory in a zip. I'd tried three different, all not working. What is the right command to do it?
Below is the excerpt of my code:
require 'zip/zip'
...
def renaming_zip(zip_file)
Zip::ZipFile.open(zip_file).each do |entry|
if entry.name == "mimetype"
puts "#{entry.name} is a file ? #{File.file? entry.name}"
puts " class ? #{entry.class}"
new_filename = "#{entry.name.gsub("mimetype", "#mimetype-new")}"
#found_entry = entry.get_entry("mimetype")
#found_entry.name = new_filename #1st try
puts " new filename #{new_filename}"
#File.rename(entry.name, new_filename) #2nd try
#entry.rename(entry.name, new_filename) #3rd try
end
end
end
if I execute without any renaming trial command, I get this output, so you can see the file exists in the zip. It's just not a File class, but a Zip::ZipEntry class, and I'm able to parse the new name.
mimetype is a file ? false
class ? Zip::ZipEntry
new filename #mimetype-new
with 1st try (uncommented), I get this error:
mimetype is a file ? false
class ? Zip::ZipEntry
Uncaught exception: undefined method `get_entry' for mimetype:Zip::ZipEntry
/Users/.../app/lib/zip_rename.rb:45:in `block in renaming_zip'
...
with 2nd try (uncommented), I get this error:
Uncaught exception: No such file or directory - (mimetype, #mimetype-new)
/Users/.../app/lib/zip_rename.rb:48:in `rename'
/Users/.../app/lib/zxp_rename.rb:48:in `block in renaming_zip'
...
mimetype is a file ? false
class ? Zip::ZipEntry
new filename #mimetype-new
with 3rd try( uncommented), I get this error:
mimetype is a file ? false
class ? Zip::ZipEntry
new filename #mimetype-new
Uncaught exception: undefined method `rename' for mimetype:Zip::ZipEntry
/Users/.../app/lib/zip_rename.rb:49:in `block in renaming_zip'
...
To rename the entry call rename on the entry.
1st Attempt fails because you are calling get_entry on entry, it should be on ZipFile.
2nd Attempt fails because the code ends the string with double-quotes.
new_filename = entry.name.gsub('mimetype', '#mimetype-new')
3rd Attempt fails because the object is mimetype:Zip::ZipEntry and is not Zip::ZipEntry
The correct way to do it is
new_filename = "#mimetype-new"
Zip::ZipFile.open(zip_file).each do |zipfile|
files = zipfile.select(&:file?)
files.each do |file|
if entry.name == "mimetype"
entry.rename(entry.name, new_filename)
end
end
end
To rename file inside zip with rubyzip:
require 'zip'
old_name = 'mimetype'
new_filename = '#mimetype-new'
Zip::ZipFile.open(zip_file_path).each do |zipfile|
files = zipfile.select(&:file?)
file = files.find{|f| f.name == old_name}
zipfile.rename(file.name, new_filename) if file
end

Resources