2012-04-26 1 views
0

그래서 우리는 우리가 얻는 보고서를 구성하기위한 파일 설정을하기 위해 다음과 같은 작은 스크립트를 가지고 있습니다.Ruby를 사용하여 큰 디렉토리 시스템 자동화

#This script is to create a file structure for our survey data 

require 'fileutils' 

f = File.open('CustomerList.txt') or die "Unable to open file..." 
a = f.readlines 
x = 0 

while a[x] != nil 

    Customer = a[x] 
    FileUtils.mkdir_p(Customer + "/foo/bar/orders") 
    FileUtils.mkdir_p(Customer + "/foo/bar/employees") 
    FileUtils.mkdir_p(Customer + "/foo/bar/comments") 
    x += 1 

end 

모든 것이 while 전에 작동하는 것 같다,하지만 난 점점 계속 :

'mkdir': Invalid argument - Cust001_JohnJacobSmith(JJS) (Errno::EINVAL) 

CustomerList.txt에서 첫 번째 라인이 될 것이다. 문자열로 간주되기 위해 배열 항목에 뭔가를해야합니까? 변수 유형이나 다른 것이 일치하지 않습니까?

미리 감사드립니다.

+0

왜 고객은 일정합니까? – inger

+0

은 첫 번째 줄의 "Cust001_JohnJacobSmith (JJS)"입니까? – inger

+0

예, 파일의 첫 줄입니다. – JHStarner

답변

1

은 나를 위해 일한 다음 :

IO.foreach('CustomerList.txt') do |customer| 
    customer.chomp! 
    ["orders", "employees", "comments"].each do |dir| 
    FileUtils.mkdir_p("#{customer}/foo/bar/#{dir}") 
    end 
end 

을 데이터과 같이 :

$ cat CustomerList.txt 
Cust001_JohnJacobSmith(JJS) 
Cust003_JohnJacobSmith(JJS) 
Cust002_JohnJacobSmith(JJS) 

몇 가지가 더 루비 방법처럼 만들기 위해 :

사용을 차단을 열어 파일을 반복하거나 배열을 반복하여 파일을 닫거나 배열에 직접 액세스 할 필요가 없습니다.

@inger에서 언급했듯이 로컬 바는 소문자 인 고객으로 시작합니다.

문자열에서 변수의 값을 사용하려면 # {}은 +로 연결하는 것보다 rubinic이 더 깁니다.

또한 chomp를 사용하여 후미 줄 바꿈을 제거했습니다. (메서드 이름에 후행!으로 표시되는 var가 바뀌어)

+0

잘 수행했습니다. 정말 고마워. 루비가 곧 읽을 책이 필요할 것 같습니다. – JHStarner