Directory Resource

Hey Community,

I am trying to delete a ton of directories located under C:\Windows\Temp.

There are a bunch of directories that start off with:


under the C:\Windows\Temp.

I am trying to use the 'directory resource' to remove all of the chef created directories but having a problem.

I know the following code works to delete one directory, but need to know how to make it delete any directory that starts with "chef."

directory "C:\\Windows\\Temp\\chef20210226-1000-1clkm9o" do
   recursive true
   action :delete

Thank you in advance!

I'd go with something like that:

Dir.glob("c:/Windows/Temp/chef*").each do |target|
  directory target do 
     recursive true
     action :delete

The code above does a listing of existing directories matching the full path (ruby works properly with forward slashes, you can avoid the double backslash on windows and use forward slashes) and loop over them, for each iteration the directory matched will be stored in the variable target.
Each iteration will define a directory resource to this target directory to delete it.

The only drawback is that the listing is done at compile time to define the resources, any temporary directory created during another resoruce convergence time will be skipped and cleaned only in the next run of chef.

Now, the real problem is: what is creating those directories ? That sounds like code in one of your cookbook is not properly cleaning up after itself.

1 Like