Scan and Copy Redux

A co-worker pointed out that Scan and Copy did not work all that well on XP. Not a real issue, since I had used it to back up win2k machines in prep for a XP roll out. Well, anyways, I made the few changes to XP it, as well as adding in some more functionality.

There are now 3 command line options

  • -name Names the target directory, which will be created if it does not exist
  • -target Gives a target directory to place the backup folder in.
  • A list of file extensions to look for
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#Stuff we need
require 'getoptlong'
require 'ftools'
 
#Get the command line options
#Required command lines
# --name 'user name'
# a list of extensions to search for
# -- target 'path'
# Target directory to put stuff into
 
@backed = 0
@collisions = 0
@totfiles = 0
@filetype = Array.new
 
opts = GetoptLong.new(
      ["--name", "-n", GetoptLong::REQUIRED_ARGUMENT],
      ["--target","-t",GetoptLong::OPTIONAL_ARGUMENT],
      ["--help","-h",GetoptLong::OPTIONAL_ARGUMENT])
 
#Parse the options, assigning them to the correct variables.
opts.each do |opt, arg|
  case opt
    when  "--name"
      @name = arg.to_s
    when "--target"
      @target = arg.to_s
    end
end
 
for ext in ARGV
  @filetype << ext
end
 
 
if @target.nil?
  @target = "y\:\\backups\\"
end
 
@path = @target + @name
 
#let's tell everyone what we are doing...
  puts ""
  puts "Copying to..."
  puts @path
  puts "Looking for..."
  puts @filetype
  puts ""
 
#Do basic checking to insure that everything we need is there
 
#need to improve logic here - 
  rescue if lower level directorys do not exist!
#Create base path if it does not exist?
 
unless File.exist?(@path)
    unless File.exist?(@target)
      Dir.mkdir(@target)
    end
      Dir.mkdir(@path)
end
 
  #Start main process of program
  Dir.chdir("c:\\")
  Dir['/**/**'].each do |item|
    @totfiles = @totfiles + 1
    unless(FileTest.directory?(item))
      if @filetype.include?(File.extname(item))
        if FileTest.exist?(@path + "\\" + File.basename(item))
          p = @path + "\\" + File.basename(item, ".*") + "_"+ 
            rand(100000000).to_s + "_" + File.extname(item)
          File.copy item, p
          printf("C")
          @collisions = @collisions + 1
          @backed = @backed + 1
          #Duplicate File Error.
        else
          p = @path + "\\" + File.basename(item)
          File.copy item, p
          #Backed up file
          printf("B")
          @backed = @backed + 1
        end
      else
        printf(".")
      end
    end
  end
 
 
puts ""
puts "Checked #{@totfiles} Files."
puts "#{@backed} Files backed up with #{@collisions} filename collisions"

Leave a Reply