2010-07-20 4 views

답변

6

같은 if exists? then file.name = "1-"+file.name 또는 뭔가처럼

아마 이런 일이 당신을 위해 작동 :

origin = '/test_dir' 
destination = '/another_test_dir' 

Dir.glob(File.join(origin, '*')).each do |file| 
    if File.exists? File.join(destination, File.basename(file)) 
    FileUtils.move file, File.join(destination, "1-#{File.basename(file)}") 
    else 
    FileUtils.move file, File.join(destination, File.basename(file)) 
    end 
end 

안부를.

3

위의 코드는 작동하지만 약간의 실수는 입니다.이 파일은 원본 폴더/또는 하위 폴더에서 파일이 종료되는지 확인합니다.이 파일은 이미 있으므로 읽기 때문에 사용하지 않습니다. 파일이 대상 폴더에 이미 있는지 확인해야합니다. 이 구문 때문에 "else"는 절대로 실행되지 않습니다. 모든 파일의 이름은 "1-filename"과 같습니다. 올바른 사용 가능

if File.exists? File.join(destination, File.basename(file)) 
관련 문제