Walk through directory structure, look at first file, rename dirin consequence.

A

Aldric Giacomoni

This is pretty ugly and doesn't work.. Can't figure out why, but it
doesn't actually go through the main loop more than once. I'm a little
stuck here.. Any help is welcome :)

def keep_iterating_until_not_directory()
Dir.glob('*') do |filename|
if File.directory?(filename)
Dir.chdir(filename)
keep_iterating_until_not_directory()
end
break
end
end

def come_back_to_nycom
Dir.chdir("..") while Dir.pwd != "C:/Documents and
Settings/username/My Documents/NYCOM"
end

Dir.glob('*') do |filename|
if File.directory?(filename)
keep_iterating_until_not_directory()
end
puts Dir.pwd
image = DICOM::DObject.new(Dir.glob('*')[0], verbose=false, library=lib)
come_back_to_nycom()
if image.get_raw("0008,1090") == "HDI 5000"
File.rename(filename, "HDI5000" + filename)
end
end
 
E

Einar Magnús Boson

This is pretty ugly and doesn't work.. Can't figure out why, but it
doesn't actually go through the main loop more than once. I'm a
little stuck here.. Any help is welcome :)

def keep_iterating_until_not_directory()
Dir.glob('*') do |filename|
if File.directory?(filename)
Dir.chdir(filename)
keep_iterating_until_not_directory()
end
break
end
end

def come_back_to_nycom
Dir.chdir("..") while Dir.pwd != "C:/Documents and Settings/username/
My Documents/NYCOM"
end

Dir.glob('*') do |filename|
if File.directory?(filename)
keep_iterating_until_not_directory()
end
puts Dir.pwd
image = DICOM::DObject.new(Dir.glob('*')[0], verbose=false,
library=lib)
come_back_to_nycom()
if image.get_raw("0008,1090") == "HDI 5000"
File.rename(filename, "HDI5000" + filename)
end
end


something I wrote a while back that works:

def getFilesInDirectory(dir)
Dir[File.join(dir, "*")].collect do |f|
if File.directory?(f) then
getFilesInDirectory(f)
else
f
end
end.flatten
end

then you can use it like:
getFilesInDirectory(Dir.pwd).each do |f|
puts f
end

einarmagnus
 
A

Aldric Giacomoni

Einar said:
This is pretty ugly and doesn't work.. Can't figure out why, but it
doesn't actually go through the main loop more than once. I'm a
little stuck here.. Any help is welcome :)

def keep_iterating_until_not_directory()
Dir.glob('*') do |filename|
if File.directory?(filename)
Dir.chdir(filename)
keep_iterating_until_not_directory()
end
break
end
end

def come_back_to_nycom
Dir.chdir("..") while Dir.pwd != "C:/Documents and Settings/username/
My Documents/NYCOM"
end

Dir.glob('*') do |filename|
if File.directory?(filename)
keep_iterating_until_not_directory()
end
puts Dir.pwd
image = DICOM::DObject.new(Dir.glob('*')[0], verbose=false,
library=lib)
come_back_to_nycom()
if image.get_raw("0008,1090") == "HDI 5000"
File.rename(filename, "HDI5000" + filename)
end
end


something I wrote a while back that works:

def getFilesInDirectory(dir)
Dir[File.join(dir, "*")].collect do |f|
if File.directory?(f) then
getFilesInDirectory(f)
else
f
end
end.flatten
end

then you can use it like:
getFilesInDirectory(Dir.pwd).each do |f|
puts f
end

einarmagnus
I think I see what this does.. It's pretty neat. How would I use this to
only parse the first file of any given directory? I can't quite picture
this.

--Aldric
 
E

Einar Magnús Boson

Hi,
Einar said:
This is pretty ugly and doesn't work.. Can't figure out why, but =20
it doesn't actually go through the main loop more than once. I'm =20=
a little stuck here.. Any help is welcome :)

def keep_iterating_until_not_directory()
Dir.glob('*') do |filename|
if File.directory?(filename)
Dir.chdir(filename)
keep_iterating_until_not_directory()
end
break
end
end

def come_back_to_nycom
Dir.chdir("..") while Dir.pwd !=3D "C:/Documents and Settings/=20
username/ My Documents/NYCOM"
end

Dir.glob('*') do |filename|
if File.directory?(filename)
keep_iterating_until_not_directory()
end
puts Dir.pwd
image =3D DICOM::DObject.new(Dir.glob('*')[0], verbose=3Dfalse, =20
library=3Dlib)
come_back_to_nycom()
if image.get_raw("0008,1090") =3D=3D "HDI 5000"
File.rename(filename, "HDI5000" + filename)
end
end


something I wrote a while back that works:

def getFilesInDirectory(dir)
Dir[File.join(dir, "*")].collect do |f|
if File.directory?(f) then
getFilesInDirectory(f)
else
f
end
end.flatten
end

then you can use it like:
getFilesInDirectory(Dir.pwd).each do |f|
puts f
end

einarmagnus
I think I see what this does.. It's pretty neat. How would I use =20
this to only parse the first file of any given directory? I can't =20
quite picture this.

--Aldric

You only want the first file in every directory and every sub-=20
directory? is that it?

With this setup:
/dir1/file1
/dir1/dir11/file1
/dir1/dir11/file2
/dir1/dir12/file1
/dir1/dir12/file2

you only want /dir1/file1, /dir1/dir11/file1 and dir1/dir12/file1, is =20=

that correct?

I guess you could do something like this:

def getFilesInDirectory(dir)
dirs =3D []
files =3D []
Dir[File.join(dir, "*")].each do |f|
if File.directory?(f) then
dirs << f
elsif files.empty?
files =3D [f]
end
end
dirs.each do |dir|
files +=3D getFilesInDirectory(dir)
end
files
end


einarmagnus
 
A

Aldric Giacomoni

Einar said:
You only want the first file in every directory and every sub-
directory? is that it?

With this setup:
/dir1/file1
/dir1/dir11/file1
/dir1/dir11/file2
/dir1/dir12/file1
/dir1/dir12/file2

you only want /dir1/file1, /dir1/dir11/file1 and dir1/dir12/file1, is
that correct?

I guess you could do something like this:

def getFilesInDirectory(dir)
dirs = []
files = []
Dir[File.join(dir, "*")].each do |f|
if File.directory?(f) then
dirs << f
elsif files.empty?
files = [f]
end
end
dirs.each do |dir|
files += getFilesInDirectory(dir)
end
files
end


einarmagnus

That does exactly what I need! What a cool solution.. Thank you!

--Aldric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top