title: Batch file: Loop through File names that contain a certain word and clear those folders' files IF older than X days
body: <p>I am trying to write a batch script to clean up old data in a compile directory. It must search in a certain head directory. There, it should delete files older than 7 days but ONLY in subfolders with a name that contains the word &quot;compile&quot;, f.i. compile335, compile336...</p>
  <p>This is what I came up with so far, but it is not working properly:</p>
  <pre><code>REM Remove files older than 7 days in compile directories of headDirectory
  cd C:\headDirectory     
  FOR /d /r . %%d IN (%Compile%\*) DO forfiles /p %%d /s /m *.* /d -7 /c &quot;cmd /c Del @path&quot;     
  </code></pre>
  <p>Can you tell me how I should change my script?</p>
  <h1>Edit:</h1>
  <p>Maybe my question was not clear enough. I Want something that does this:</p>
  <pre><code>forfiles -p &quot;C:\Whatever\Compile1&quot; -s -m *.* -d -7 -c &quot;cmd /c del @path&quot; 
  forfiles -p &quot;C:\Whatever\CompileFoo&quot; -s -m *.* -d -7 -c &quot;cmd /c del @path&quot;
  forfiles -p &quot;C:\Whatever\CompileBar&quot; -s -m *.* -d -7 -c &quot;cmd /c del @path&quot; 
  forfiles -p &quot;C:\Whatever\Compile23&quot; -s -m *.* -d -7 -c &quot;cmd /c del @path&quot;
  </code></pre>
  <p>But in one single loop statement that tackles all compile folders in that directory, but leaves the other folders untouched.</p>