Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:57:34+00:00 2026-06-15T15:57:34+00:00

I’ve written a Powershell script that would periodically delete folders on my machine. The

  • 0

I’ve written a Powershell script that would periodically delete folders on my machine.
The algorithm is as follows:

  • Drill down into each directory structure to the lowest subfolders
  • Check the creation date of the subfolder
  • If it’s 14 days old, or older, delete it
  • LOG EVERYTHING (not part of the algorithm, just good practise)

When running, it operates exactly as expected…

… Except it throws the following, non-terminating exception:

Get-ChildItem : Could not find a part of the path 'C:\foo\baz'.
At C:\src\CoreDev\Trunk\Tools\BuildClean script\buildclean.ps1:55 char:15
+     Get-ChildItem <<<<  -recurse -force |
    + CategoryInfo          : ReadError: (C:\foo\baz:String) [Get-ChildItem],
   DirectoryNotFoundException
    + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChil
   dItemCommand

Why is this happening? More importantly, how can I remove it, and will it cause an issue?

The script is as follows:

# folderclean.ps1

# This script will remove each leaf node of a directory, provided that leaf is over
# 14 days old.

# CONSTANT DECLARATIONS
# testing (run on my local machine)
$proj_loc = "C:\foo", "C:\bar"
$logpath = "C:\Logs\BuildClean\$(Get-Date -format yyyyMMdd).log"

function Write-ToLogFile {
    param ([string]$stringToWrite)

    Add-Content $logpath -value $stringToWrite
}

# Function to check if a folder is a leaf folder.
#   First, retrieve the directory $item is pointing to
#   Then, create a list of children of $item that are folders
#   If this list is either empty or null, return $true
#   Otherwise, return $false
function Folder-IsLeaf($item) {
    $ary = Get-ChildItem $item -force | ?{ $_.PSIsContainer }
    if (($ary.length) -eq 0 -or $ary -eq $null) {
        return $true
    }

    return $false
}

# Deletes leaf folders that are older than a certain threshhold.
#   Get a list of children of the folder, where each child is a folder itself and 
#       was created over 14 days ago and the folder is a leaf
#   For each of these children, delete them and increment $folderCount
#   Get a list of children of the folder, where each child is a folder itself and 
#       was last modified over 14 days ago and the folder is a leaf
#   For each of these children, delete them and increment $folderCount
function Remove-LeafFolders($path) {
    $createdCount = 0
    $modifiedCount = 0

    Write-ToLogFile "Operation started at $(Get-Date -format "dd/MM/yyyy hh:mm:ss.fff")"
    Write-ToLogFile "Looking in $proj_loc"
    Write-ToLogFile ""
    $start = $(Get-Date)

    $proj_loc | 
    Get-ChildItem -recurse -force | 
    ?{
        $_.PSIsContainer -and ($_.CreationTime).AddDays(15) -lt $(Get-Date) -and $(Folder-IsLeaf $_.FullName) -eq $true
    } | %{
        $formattedDate = $($_.CreationTime).ToString("dd/MM/yyyy hh:mm:ss");
        Write-ToLogFile "Folder $($_.FullName) is being removed; created: $formattedDate"
        Remove-Item $_.FullName -recurse;
        $createdCount += 1
    }

    $end = $(Get-Date)
    $elapsed = $end - $start
    Write-ToLogFile "Operation completed at $(Get-Date -format "dd/MM/yyyy hh:mm:ss.fff")."
    Write-ToLogFile "Folders removed: $createdCount"
    Write-ToLogFile "Time elapsed: $(($elapsed).TotalMilliseconds) ms"
    Write-ToLogFile "-------------------------------"
}

Remove-LeafFolders($proj_loc)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:57:36+00:00Added an answer on June 15, 2026 at 3:57 pm

    I found this other StackOverflow question, and, after looking through the answer, I realised that the problem was the pipeline. So, I changed my code as follows:

    ...
    $leafList = $proj_loc | 
        Get-ChildItem -recurse -force | 
        ?{
    
            $_.PSIsContainer -and ($_.CreationTime).AddDays(15) -lt $(Get-Date) -and $(Folder-IsLeaf $_.FullName) -eq $true
        }
    
        Foreach ($folder in $leafList)
        {
            $formattedDate = $($folder.CreationTime).ToString("dd/MM/yyyy hh:mm:ss");
            Write-ToLogFile "Folder $($folder.FullName) is being removed; created: $formattedDate"
            Remove-Item $folder.FullName -recurse;
            $createdCount += 1
        }
    ...
    

    I created a few local folders and screwed around with them. No exceptions cropped up, so this appears to have worked:

    Operation started at 10/12/2012 05:16:18.631
    Looking in C:\foo C:\bar
    
    Folder C:\foo\baz is being removed; created: 09/01/2010 02:00:00
    Folder C:\bar\baz3\recursion is being removed; created: 01/01/2008 01:00:00
    Operation completed at 10/12/2012 05:16:18.748.
    Folders removed: 2
    Time elapsed: 33.0033 ms
    -------------------------------
    Operation started at 10/12/2012 05:41:59.246
    Looking in C:\foo C:\bar
    
    Folder C:\foo\baz2\NewFolder is being removed; created: 10/10/2010 10:10:10
    Folder C:\bar\baz3\barbar is being removed; created: 20/11/2012 05:37:38
    Operation completed at 10/12/2012 05:41:59.279.
    Folders removed: 2
    Time elapsed: 21.0021 ms
    -------------------------------
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.