Class: File

Inherits:
Object show all
Defined in:
lib/kaki/utils/check_fname_overlapping.rb

Class Method Summary collapse

Class Method Details

.check_fname_overlapping(fname) ⇒ String

ファイル名が重複していれば (1) などを付加して返す

Returns:


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/kaki/utils/check_fname_overlapping.rb', line 4

def self.check_fname_overlapping(fname)
  return fname unless File.exist?(fname)
  dir = File.dirname(fname)
  base = File.basename(fname)
  m = /(.+)(\..+)$/.match(base)
  result, suffix = m ? [m[1], m[2]] : [base, ""]
  
  begin
    result = if (m1 = /(.+)\((\d+)\)$/.match(result))
      m1[1] + "(#{m1[2].to_i + 1})"
    else
      result + "(1)"
    end
  end while File.exist?(r = File.join(dir, result + suffix))
  r
end