Tom Cloyd
5/27/2009 10:57:00 PM
Bobby Sutter wrote:
> Hi,
>
> I am a beginner with very basic knowledge of ruby, but I would like to
> create a script that would do the following:
>
> 1) Prompt to input disc
> 2) Create a new sequential folder within a specified directory
> 3) Copy all files from disc into said folder
> 4) Eject disc
> 5) Prompt to input another disc
>
> Essentially, I must copy the data off of ~1000 CDs and put the data into
> a separate folder for each disc. I would be doing this in a linux
> environment (ubuntu or debian).
>
> Does anyone have any tips on where to start? Can this even be done with
> ruby? Thanks!
>
Beginner? Me too, but I may know a bit more than you at this point. I
know that the first time I tried to do something like this I ran into a
few surprises. So...I'll do for you what others here have done for me -
an act of generosity that is rather often characteristic of this list -
throw some code at you.
While the following code is rudimentary, it does meet your
specifications - at least on my Kubuntu Linux OS. You may have to look
up some things to make complete sense of it - or ask more questions.
Hope this is useful.
# cds_copy.rb
def main
cd_source = "/media/cdrom" # CDs read from here
dirbase = "/home/tomc/Ruby-work/a-test" # new dirs created here
dirstart = 0 # dir counter (for name creation)
while true # loop forever until told to stop
# Prompt to input disc
puts "Load CD. Press enter when loaded. Enter 'x' to exit."
gets # input defaults to '$_'
exit if /^x/ =~ $_
system( "mount '" + cd_source + "'" )
# Create a new sequential folder within a specified directory
foldernew = ( 'CD' + (dirstart.to_i + 1).to_s )
dirstart += 1
# Copy all files from disc into new folder
FileUtils.cp_r(cd_source, foldernew) # recursive copy
# Eject disc
system( "eject '" + cd_source + "'" )
end
end
require "fileutils"
main
# end file
Tom C.
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~