How to Standardize Filenames with PowerShell

Posted: August 30, 2020  |  Categories: PowerShell

Per request of community members, I recently, or not that recently, started to add the SVG files on my Microsoft Integration, Azure, Office 365, and much more Stencils Pack for Visio. However, this new feature brought me some new challenges:

  • How to easily manage and organize all these files;
  • How to detect duplicates;

To solve part of the first topic, I decide to manually create a folder called “SVG” for each group of stencils that exist in this package: Azure, Enterprise Integration, IoT, AI and Machine Learning, Databases and Analytics and so on.

And to solve the second part of the first topic and at the same time, the second challenge, I decided to automate the process by building a simple PowerShell script that allows me to:

  • Standardize all SVG filenames available in all subfolders by:
    • Not using spaces. Some software will not recognize file names with spaces, and file names with spaces must be enclosed in quotes when using the command line. Having spaces in URL’s are also not a good experience and should be avoided. For all these reasons I decided to remove all spaces and replace it will ‘-‘ (dash);
    • Names are in Camel case, where the first letter of each section of text is capitalized (of course respecting the line above), e.g., File-Name.svg
    • Having extension in lowercase, e.g., *.svg
  • List all the detected duplicate files.
    • I didn’t want to delete the duplicate files automatically because I want to be able to validate and decide if that indeed they are the same file or with is the most recent version of the representation.

The result was this small master piece:

#########################################################
#                                                       #
# Standardize SVG file names (CamelCase without spaces) #
# Author: Sandro Pereira                                #
#                                                       #
#########################################################

[String]$location = Get-Location

$files = Get-ChildItem $location -recurse -force -Filter *.svg
foreach($file in $files)
{
    $textInfo = (Get-Culture).TextInfo
    $newname = $textInfo.ToTitleCase([String]$file.Name.ToLower()).Replace(" ", "-").Replace(".Svg",".svg")
    Try
    {
        Rename-Item -Path $file.PSPath $newname -ErrorAction 'Stop'
    }
    Catch
    {
        Write-Host $file.PSPath
    }
}

Download

THIS POWERSHELL IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND.

Standardize SVG Filenames with PowerShellStandardize SVG Filenames with PowerShell
GitHub

Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc. He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.

1 thought on “How to Standardize Filenames with PowerShell”

Leave a Reply

Your email address will not be published. Required fields are marked *

turbo360

Back to Top