PowerShell: Renaming files/folders to a new date format in a single line of code

Summary:

Renaming folders from a date format DD/MM/YY to YYYY/MM/DD using PowerShell in a single line of PowerShell

Issue

The previous incarnation of this script was multiple lines of PowerShell, however using piping this can be concatenated into a single line of code (bar parameters and logging)

Resolution

Usage Instructions

# Adjust the following parameters to your needs below:
# $strDirectoryPath	# Root of target folder, default is directory where script
#			  is located.
# $strDelimiter 	# Delimiter to split date by (e.g. "/")
# $strRegEx 		# RegEx for date format ("##_##_##_" e.g. "01_01_15_")
# $strLogPath		# Path to log file, default is directory where script is located.

Source Code

# Script to re-format date syntax for folders
# Copyright (C) 2015  Adil Dean
# Script Name: ReformatFolderDates.ps1
# Script Version: 1.1
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see .
#
# ============================================================================
# Change History:
# 0.0.1 - 03/05/2015 - Adil Dean
#	- Script created
# 0.0.2 - 03/05/2015 - Adil Dean
#	- Add rename folders functionlity
#	- Add transaction logging
#	- Clean up messages to log
# 0.0.3 - 03/05/2015 - Adil Dean
#	- Add conformation prompt
# 1.0 - 03/05/2015 - Adil Dean
#	- Release version
#	- Added $strDelimiter to regex contstruction
#	- Updated usage instructions to include log
# 1.1 - 04/05/2015
#	- Convert to one line of PowerShell
#
# ============================================================================
# Known Bugs/Feature requests:
# - No validation of parameters,invalid entries will cause script to fail
# - Script only caters for years starting with 20
# - Subfolders are not traversed
# ============================================================================
# Requirements:
# - Script is not signed (requires necessary execution policy)
# Usage Instructions:
# Adjust the following parameters to your needs below:
# $strDirectoryPath # Root of target folder, default is directory where script
#					  is located.
# $strDelimiter # Delimiter to split date by (e.g. "/")
# $strRegEx 	# RegEx for date format ("##_##_##_" e.g. "01_01_15_")
# $strLogPath	# Path to log file, default is directory where script is located.
#
# ============================================================================

# Initialize Parameters/Variables
$strDirectoryPath = split-path -parent $MyInvocation.MyCommand.Definition	# Root of target folder
$strDelimiter = "_"								# Delimiter to split date by (e.g. "/")

$strRegEx = "^\d{2}"+$strDelimiter+"\d{2}"+$strDelimiter+"\d{2}"+$strDelimiter	# RegEx for date format ("##_##_##_" e.g. "01_01_15_")
$strLogPath = $strDirectoryPath + "\RenameLog.log"				# Path to log file

$aSplitFileName = @()
$strNewFolderName = $null

Clear-Host

Write-Host "##################################################################"
Write-Host "Please check the following information is correct before proceding"
Write-Host "Root of target folders is: "$strDirectoryPath -ForegroundColor Yellow -BackgroundColor Blue
Write-Host "RegEx being applied is: "$strRegEx -ForegroundColor Yellow -BackgroundColor Blue
Write-Host "Delimiter to split date is: "$strDelimiter -ForegroundColor Yellow -BackgroundColor Blue
Write-Host "Log will be saved to: "$strLogPath -ForegroundColor Yellow -BackgroundColor Blue
Write-Host "##################################################################"
Write-Host "If the above information is not correct press Ctrl+C to quit" -ForegroundColor Yellow -BackgroundColor Blue
Write-Host "or the enter key to continue" -ForegroundColor Yellow -BackgroundColor Blue
PAUSE

Start-Transcript $strLogPath -Force

Get-ChildItem -Path $strDirectoryPath -Directory | Where-Object {$_ -match $strRegEx} | Rename-Item -newName {
	$aSplitFileName = $PSItem.Name.Split($strDelimiter)
	Write-Host "Original folder name: "$PSItem.Name
	$_.Name -replace $strRegEx,("20" + $aSplitFileName[2] + $strDelimiter + $aSplitFileName[1] + $strDelimiter + $aSplitFileName[0] + $strDelimiter)
	# Output new folder name
	$strNewFolderName = $PSItem.Name -replace $strRegEx,("20" + $aSplitFileName[2] + $strDelimiter + $aSplitFileName[1] + $strDelimiter + $aSplitFileName[0] + $strDelimiter)
	Write-Host "New folder name: "$strNewFolderName
}


Stop-Transcript
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s