Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Saturday, March 23, 2024

Replace characters in folder names using Windows PowerShell

This article describes a method to rename folders by replacing a character with another. In this use case we have a folder full of subfolders which are named in a particular pattern with a date followed by the description of the folder. The words in the folder names are separated by the hyphen ("-') character.  Our objective is to keep the date part intact while replacing all the hyphens after the data with whitespace characters.

The before and after state of the folder names are illustrated in the screenshot below.

Renamed folders with hyphens replaced with whitespace characters

For this example, we consider that our bunch of folders are in a local drive path named "C:\Users\SampleUser\Desktop\2006".

Open the Windows PowerShell and change directory to "C:\Users\SampleUser\Desktop\2006". Next run the following script at the prompt. Remember to set the value of $directoryPath below to the appropriate path according to your situation:

# Set the directory path where you want to rename folders
$directoryPath = "C:\Users\SampleUser\Desktop\2006"

# Get a list of all folders in the specified directory
$folders = Get-ChildItem -Path $directoryPath -Directory

# Loop through each folder
foreach ($folder in $folders) {
    # Check if folder name is longer than 10 characters and contains '-'
    # character at position 11
    if ($folder.Name.Length -ge 11 -and $folder.Name.Substring(10, 1) `
    -eq "-") {
        # Construct the new folder name by replacing '-' with a blank
        # space starting at character number 11
        $newName = $folder.Name.Substring(0, 10) + `
        ($folder.Name.Substring(10) -replace '-', ' ')
       
        # Get the full path of the old and new folder names
        $oldPath = Join-Path -Path $directoryPath -ChildPath $folder.Name
        $newPath = Join-Path -Path $directoryPath -ChildPath $newName
       
        # Rename the folder
        Rename-Item -Path $oldPath -NewName $newName -ErrorAction SilentlyContinue
        Write-Host "Renamed folder: $($folder.Name) to $($newName)"
    }
}

On successful execution of this script, you should see an output like this.

Renamed folder: 2019-01-16-Travel-to-France to 2019-01-16 Travel to France
Renamed folder: 2019-03-07-Meijer to 2019-03-07 Meijer
Renamed folder: 2019-05-04-National-Train-Day to 2019-05-04 National Train Day
Renamed folder: 2019-05-18-Woodland-Park to 2019-05-18 Woodland Park
Renamed folder: 2019-05-26-Maumee-Bay-State-Park to 2019-05-26 Maumee Bay State Park
Renamed folder: 2019-07-05-African-Safari-Wildlife-Park to 2019-07-05 African Safari Wildlife Park

Now you are done.


Sunday, September 24, 2023

Find files with path lengths exceeding CD/DVD restrictions using Windows PowerShell

If you have been burning CDs or DVDs for your personal archives or any other purposes, you already know it allows file paths only up to 255 characters. If you are burning files in bulk it an often difficult to ensure that your file path lengths confirm to the prescribed limits. Or if you are like me, then you probably want to limit the path length to 100-150 characters for manageability and protection against burn failures.

For this example, we consider a bunch of subfolders and files in a local drive path named "D:\MUSIC-Full-Backup\BACKUP0933\ARCHIVE05". We intend to burn the content inside this folder on the root of a DVD disc. For this purpose, we want to know which files here breach the 100-character limit of path length so we can take appropriate action to correct this.

Open the Windows PowerShell and change directory to "D:\MUSIC-Full-Backup\BACKUP0933\ARCHIVE05". Next run the following command at the prompt:

cmd /c dir /s /b *.* |?$curPath="D:\MUSIC-Full-Backup\BACKUP0933\ARCHIVE05"; $_.length-$curPath.length -gt 100 }

Here we are deducting the length of the current path from the full path of each file as it will not be part of the patch when it is burned the DVD. This should list all files in the folder which exceeds the length of 100 characters. An example output is below.

D:\MUSIC-Full-Backup\BACKUP0933\ARCHIVE05\Greatest Collection of the Century\JD YNK ULEATOBS NKY ULEATOBS\ARH OKIKIS VS LSNEIA LYTFTUREB JD KYN MAHUSP IXM.mp3
D:\MUSIC-Full-Backup\BACKUP0933\ARCHIVE05\Greatest Collection of the Century\JD YNK ULEATOBS NKY ULEATOBS\REMAD NO VS UHJT EINM ARB ILIONV TEEHM IMX FT SPEENAD.mp3
D:\MUSIC-Full-Backup\BACKUP0933\ARCHIVE05\Greatest Collection of the Century\JD YNK ULEATOBS NKY ULEATOBS\UHTJE HULBA IADY RESROPIVSEG NRETAC IMX.mp3

Now you can go to the individual folder and plan your strategy for shortening the path lengths.