Reputation: 21
I have a text file list of 56 folders that includes the full paths to a set of folders. I need to rename the folder at the end of the path. Example:
Original: \this folder\needs to\move
New: \this folder\needs to\move.moved
I am totally new at powershell and trying to learn. I thought this might be a good way to start. Any help would be greatly appreciated.
Upvotes: 0
Views: 307
Reputation: 2308
# Get the content of the list
# in this case, a text file with no heading, and one path per line
$listContent = Get-Content $ENV:USERPROFILE\Desktop\list.txt
# Loop over each child folder and rename it
foreach($line in $listContent)
{
# check if the current path is valid
$pathTest = Test-Path -Path $line
if($pathTest -eq $True)
{
Write-Output "`nOld path: $($line)"
$newName = $line + ".moved"
Write-Output "New name: $newName"
try
{
# on success, write out message
Rename-Item -Path $line -NewName $newName -Force
# split the string from the file and get the data after the last \ for readability
Write-Output "`nSuccessfully changed directory name $($line.split('\')[-1]) to $newName"
}
catch
{
# on error, throw first error in the Error array
throw $Error[0]
}
}
else {
Write-Output "$($line) is not a valid path"
}
}
Write-Output "`nEnd of script!"
Upvotes: 1