Newbie First Script - Showing empty Folders

Hello Internet!

I’m a PLC-programmer (structured text language) for industrial machines and I have little experiance with C#.

And now I would like to wirte my first powershell-script.
What do I need? Is PowerShell ISE the developer enviroment that I have to use for this?
I would like have script doing following:

  • Checking folders and subfolders for any content/files and when they are empty, signe a diffrent folder-icon to it.

Why you ask? Because we use certain folder-structions for instance projects. And each project has diffren size and diffrent documents. but we would like to use always the same folder-struction.

Is this possilbe with a powershell-script?

Welcome!

This sounds like a perfect starter project for PowerShell. Since it sounds like you’re just starting out, I have a couple of recommendations for you.

First, I recommend checking out VSCode as your development environment. This gives you the capability to leverage PowerShell 7 instead of the older PowerShell 5 that comes with Windows, and will set you up for when you’re ready for more advanced capabilities.

Second is to install PowerShell 7 and use that, as it gives you the latest features.

Third, if you’re looking for a PowerShell intro/overview, I highly recommend Learn PowerShell in a Month of Lunches. It’s a really solid foundational understanding of how PowerShell works and how to use it. Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS: Plunk, Travis, Petty, James, Leonhardt, Tyler: 9781617296963: Amazon.com: Books

And finally, this forum is a great place to ask your questions. Write something, give it a try, post your code. We are all happy to help you troubleshoot or make improvements.

2 Likes

Thanks alot Darwin!

Are both tools for free?

How about spending some effort yourself and figure things out by yourself? :smirk: :+1:t3:
It would have taken you probably less than a minute to use your prefered internet search engine and see for yourself.

1 Like

you’re right. I was not aware that this could be disproportional.
Every forum is diffrent I guess.

Try this

# Define the path to the root folder
$rootFolder = "C:\Path\To\Your\RootFolder"

# Define the path to the icon file for empty folders
$emptyFolderIcon = "C:\Path\To\EmptyFolderIcon.ico"

# Function to check if a folder is empty
function Is-FolderEmpty {
    param (
        [string]$folderPath
    )
    $items = Get-ChildItem -Path $folderPath -Recurse -File
    return $items.Count -eq 0
}

# Function to assign a different icon to an empty folder
function Set-EmptyFolderIcon {
    param (
        [string]$folderPath,
        [string]$iconPath
    )
    $desktopIniPath = Join-Path -Path $folderPath -ChildPath "desktop.ini"
    $content = @"
[.ShellClassInfo]
IconResource=$iconPath,0
"@
    Set-Content -Path $desktopIniPath -Value $content -Force
    attrib +h +s $desktopIniPath
}

# Check folders and subfolders
$folders = Get-ChildItem -Path $rootFolder -Recurse -Directory
foreach ($folder in $folders) {
    if (Is-FolderEmpty -folderPath $folder.FullName) {
        Set-EmptyFolderIcon -folderPath $folder.FullName -iconPath $emptyFolderIcon
    }
}

# Check the root folder itself
if (Is-FolderEmpty -folderPath $rootFolder) {
    Set-EmptyFolderIcon -folderPath $rootFolder -iconPath $emptyFolderIcon
}