"unless" with Regex

I have a series of directories inside of a directory that I am trying to
read out
of…the directories are named according to years. I want to do a regex
that ignores anything EXCEPT 4 digit numbers
(as in a proper year)…I tried using the “unless” option but doesnt
seem to be working:

unless !/\d{4}/

Here is the code-snippet

Dir.foreach("/Volumes/gac_repo/video") do |year|
#unless the folder name isnt a 4 digit number…
unless !/\d{4}/
#print the name of the folder which hopefully should just be years
puts “The folder name is: #{year}”
#Do stuff to the files inside of the folders
Dir.foreach("/Volumes/gac_repo/video/#{year}") do |name|
puts “The file name is #{name}”
end
end
end

This will give you exactly 4 digits:

if year =~ /^\d{4}$/

That worked perfectly thank you…as a follow up, for some reason the
filename “.” and “…” are still showing their pesky heads through:

I tried elimating them using the same syntax but didnt work:

if name != /^.{1,}$/

here is the whole code-snippet:

Dir.foreach("/Volumes/gac_repo/video") do |folder_names|
if folder_names =~ /^\d{4}$/
year = folder_names
puts “The folder name is: #{year}”
Dir.foreach("/Volumes/gac_repo/video/#{year}") do |name|
if name != /^.{1,}$/
puts “The file name is #{name}”
end
end
end
end

Dir.foreach yields one name at a time to the block, so using a block
variable called folder_names(plural) is confusing and therefore should
be shunned. In addition, the names that are yielded are known as
“filenames” regardless of whether they are actually the names of files
or directories. Dir.foreach() is defined to yield all the names in a
directory–files and directories–including hidden files, which are
filenames whose names start with a dot.

There is a method called Dir.glob() which yields all the filenames in a
directory–omitting the hidden files by default. Dir.glob() will also
return full paths if you give it a full path as the starting point of
the search. If the starting point is a relative path, then Dir.glob()
will yield names tacked onto the end of the relative path.

On Fri, Dec 28, 2012 at 7:04 PM, Pierre-Andre M. [email protected]
wrote:

Dir.foreach(“/Volumes/gac_repo/video”) do |folder_names|
if folder_names =~ /^\d{4}$/
year = folder_names
puts “The folder name is: #{year}”
Dir.foreach(“/Volumes/gac_repo/video/#{year}”) do |name|
if name != /^.{1,}$/
puts “The file name is #{name}”
end
end
end
end

Wondering if I understand this, wouldn’t this give you what you want?

path = “/Volumes/gac_repo/video”
years_pattern = “[12][0-9][0-9][0-9]”

Dir.glob(“#{path}/#{years_pattern}/”) do |folder|
puts “The folder name is #{folder}”
Dir.glob(“#{folder}*”) do |file|
puts “The file name is #{file}” if File.file?(file)
end
end

The first Dir.glob will provide the directories that make years
assuming they are all 4 digit years > year 1000 (safe bet if they’re
videos).

The second Dir.glob will give all non-hidden (non-dot) files,
directoires, named pipes, etc, which is why you may want the if
modifier File.file?, which returns true only for actual files, not
directories or named pipes, etc. (If that is not true, adjust
accordingly.)

Hi,

that “!=” is the wrong method. It’s an inequality check (as you
problably know). But you want to check if the string does not match a
certain pattern, which is done with !~.

But why make it so complicated? Just use something like

next if %w[. …].include? name