2011-01-10 4 views
0

내가 여기에 여기에 나타났습니다 몇 가지 스크립트를 묶어하려고합니다 link text 사용자가 디렉토리 문자와 파일 확장명 주어진 문자를 이름을 바꾸거나 이름을 바꿀 수있는 기본 스크립트를 얻으려고 .빠른 루비 일괄 renamer

나는 모두 함께 묶는 데 어려움을 겪고 있습니다. 이것은 내가 지금까지있는 곳이다.

require 'fileutils' 

define renamer(strip, stripdetails) 
# So this is a strip function. 

    def strip(str,char) 
    new_str = "" 
    str.each_byte do |byte| 
     new_str << byte.chr unless byte.chr == char 
    end 
    new_str 
end 
# and then retrieve details from user. 

#Get directory of files to be changed. 
def stripdetails(strip myname) 
puts "Enter Directory containing files" 
STDOUT.flush 
oldname = gets.chomp 
puts "what characters do you want to remove" 
str = gets.chomp 
puts "what file extension do files end in?" 
fileXt = gets.chomp 
end 

#And I found this from stackoverflow(I don't have enuff credits to post another hyperlink) 
old_file = "oldname" 
new_file = strip(oldname,str) 
FileUtils.mv(old_file, new_file) 
+0

질문을 편집하고 작동하지 않는 사항을 설명하십시오. 어떤 오류가보고 있습니까, 아니면 예상대로 작동하지 않습니까? –

답변

3

다음은 코드 리팩터링입니다. 귀하의 질문이나 코드에서 완전히 명확하지는 않지만, 디렉토리의 각 파일 이름에서 주어진 문자를 제거하려고한다고 가정합니다.

블로그 게시물에서 복사 한 strip() 메서드는 내장 된 tr() 메서드를 제대로 구현하지 않았기 때문에 완전히 필요하지 않습니다.

#Given a directory, renames each file by removing 
#specified characters from each filename 

require 'fileutils' 

puts "Enter Directory containing files" 
STDOUT.flush 
dir = gets.chomp 
puts "what characters do you want to remove from each filename?" 
remove = gets.chomp 
puts "what file extension do the files end in?" 
fileXt = gets.chomp 

files = File.join(dir, "*.#{fileXt}") 
Dir[files].each do |file| 
    new_file = file.tr(remove,"") 
    FileUtils.mv(file, new_file) 
end 
+0

네, 그렇게하려고했습니다! 스트립 광고 tr에 관한 정보 주셔서 감사합니다. 나는 책을 읽고 온라인 코드를 검색하는 순간을 배우고 있습니다. 코드 또는 조언이 항상 온라인 또는 정확합니다. 귀하의 도움을 주셔서 대단히 감사합니다. – sayth

+0

내가 코드를 exceute하고 위치를 묻는 경우 디렉토리 위치를 붙여 넣을 수 없으므로 직접 입력해야합니다. 그 주위에 어쨌든 있습니까? – sayth

0

이 프로그램은 stripdetails 메소드를 호출하지 않습니다. 코드가 동일한 범위에서 실행되도록 "변경 될 파일의 ​​디렉터리 가져 오기"블록에서 def stripdetails..end 줄을 제거해보십시오.