How to use System IO in powershell to get the folder name without qualifiers

315 views Asked by At

My purpose is to recurse through folders and rename picture files by the Date Taken - Folder Name.

The code works if I run it in the directory with the photos in it by using:

[string]$newDirectory = (Split-Path $Path -Leaf)

In the example below, how would I get only "Windows." Any other solutions would be helpful. Thank-you.

Get-Item: BaseName : explorer Mode : -a--- Name : explorer.exe Length : 2871808 DirectoryName : C:\Windows Directory : C:\Windows IsReadOnly : False Exists : True FullName : C:\Windows\explorer.exe Extension : .exe CreationTime : 27.04.2011 17:02:33 CreationTimeUtc : 27.04.2011 15:02:33 LastAccessTime : 27.04.2011 17:02:33 LastAccessTimeUtc : 27.04.2011 15:02:33 LastWriteTime : 25.02.2011 07:19:30 LastWriteTimeUtc : 25.02.2011 06:19:30 Attributes : Archive

1

There are 1 answers

4
Keith Hill On

You can do this directly with PowerShell commands - no need to drop down to .NET:

Get-ChildItem *.jpg -Recurse | 
    Rename-Item -NewName {Join-Path $_.Directory "$($_.Directory.Name) - $($_.CreationTime.ToString('yyyyMMdd-HH-mm-ss'))$($_.extension)"} -WhatIf

Adjust the date/time format string 'yyyyMMdd-HH-mm-ss' to your liking but avoid using : in the time part of the name.