Use PowerShell to Copy Only Folders that Contain Files

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to copy only folders that contain files.

Microsoft Scripting Guy, Ed Wilson, is here. In some respects, it seems like I have been using Windows PowerShell for a long time. In other respects, it seems like the journey begun only a short while ago. I mean, after writing five books about Windows PowerShell, it would seem that I know the product pretty well. And yet, I continue to amaze myself with the power of Windows PowerShell.

What am I talking about? Well, a few months ago, I created a whole bunch of folders based on dates in preparation for the Scripting Wife’s and my trip to Europe. Unfortunately, due to certain time issues, I did not always upload pictures every day into the folders. This caused some folders to not have pictures in them. Now when you are talking about 40 or so folders, it is annoying to click and not see anything. So I decided to delete folders that were not being used. At first I was going to do it by hand, but that was painful. I thought the code to clean up the empty folders might be too difficult to write, but in the end, it was not.

Delete empty folders

I opened the Windows PowerShell ISE, and created two variables to hold the source path and to hold the destination path. These two variables are shown here (note that you will not have these directories unless you have created them):

$path = ‘C:datamypics2012’

$destination = ‘e:datamypicsarchive2012’

Now I use the Get-ChildItem cmdlet to collect all of my directories. I use the Directory switch that is available in Windows PowerShell 3.0 to do this. I send the directory objects down the pipeline. This is shown here.

Get-ChildItem $path -Directory |

Now I use the Foreach-Object cmdlet to work with each directory object. I call the EnumerateFiles method from the DirectoryInfo object to see if there are any files. I send the results to Measure-Object to count the files. I am sure this code could be cleaner, but it works:

Foreach-Object  {if (($_.enumeratefiles() | measure).count -gt 0)

If there are files, I copy the folder (and files) to the destination as shown here:

Copy-Item -path $_.fullname -Destination $destination -Recurse}

That is it. It did not take me very long at all to create the script. And it saved a whole lot of clicking. Here is the competed CopyOnlyFoldersWithFiles script.

CopyOnlyFoldersWithFiles.ps1

$path = ‘C:datamypics2012’

$destination = ‘e:datamypicsarchive2012’

Get-ChildItem $path -Directory |

Foreach-Object  {if (($_.enumeratefiles() | measure).count -gt 0)

  {

   Copy-Item -path $_.fullname -Destination $destination -Recurse}

  }

Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy