Manas
Manas

Reputation: 177

Chef: Count the number of files in a folder

I am trying to get the count of files in a folder and skip or execute the further resources based on the count.

file 'C:\Users\Desktop\Chef-file\count.txt' do
            dir = 'C:\Users\Desktop\Chef-Commands'
                count = Dir[File.join(dir, '**', '*')].count { |file| File.file?(file)}
              content count
            end

But getting the following error

Chef::Exceptions::ValidationFailed: Property content must be one of: String, nil!  You passed 0.

I am pretty new to chef and ruby so was wondering how to fix/solve this problem.

Once the count is obtained, how to check its value in other resources?

Also would like to know if this is the right approach.

Thanks

Upvotes: 1

Views: 571

Answers (1)

Roland
Roland

Reputation: 1426

count seems to be 0 (Fixnum).

You may wanna try:

file 'C:\Users\Desktop\Chef-file\count.txt' do
  dir = 'C:\Users\Desktop\Chef-Commands'
  count = Dir[File.join(dir, '**', '*')].count { |file| File.file?(file)}
  content count.to_s
end

Upvotes: 2

Related Questions