Rename Files based on directory name with Powershell

I found this script but it's not working for me. I need to rename all of the files under the directories to be test@test.com.1.pst but I think the @ is causing issues. Can you help?

https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Rename-Files-b1268678

Directory Name & Files
C:\temp\test\test@test.com
test.pst
abc.pst
123.pst

########################################################### 
# AUTHOR  : Marius @ Hican - http://www.hican.nl - @hicannl 
# DATE    : 23-04-2012 
# COMMENT : This script renames all .jpg files to the 
#           name of the .jpg parent folder recursively, 
#           extended with an increasing number. 
#           Put the script in the root of the folders' 
#           parent folder. 
#           NOTE: This script can also be used for renaming 
#           other / multiple files, just adjust the filter! 
########################################################### 
$path = Split-Path -parent $MyInvocation.MyCommand.Definition 
 
Function renameFiles 
{ 
  # Loop through all directories 
  $dirs = dir $path -Recurse | Where { $_.psIsContainer -eq $true } 
  Foreach ($dir In $dirs) 
  { 
    # Set default value for addition to file name 
    $i = 1 
    $newdir = $dir.name + "." 
    # Search for the files set in the filter (*.pst in this case) 
    $files = Get-ChildItem -Path $dir.fullname -Filter *.pst -Recurse 
    Foreach ($file In $files) 
    { 
      # Check if a file exists 
      If ($file) 
      { 
        # Split the name and rename it to the parent folder 
        $split    = $file.name.split(".pst") 
        $replace  = $split[0] -Replace $split[0],($newdir + $i + ".pst") 
 
        # Trim spaces and rename the file 
        $image_string = $file.fullname.ToString().Trim() 
        "$split[0] renamed to $replace" 
        Rename-Item "$image_string" "$replace" 
        $i++ 
      } 
    } 
  } 
} 
# RUN SCRIPT 
renameFiles 
"SCRIPT FINISHED"

Open in new window

LVL 1
vpetersenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
From testing it handles folder names with "@" just fine.
I made a few adjustments anyway, mainly to clear up things that weren't really needed.  I commented out some lines (and added a couple inline comments), which can now be deleted to clean up the script.
########################################################### 
# AUTHOR  : Marius @ Hican - http://www.hican.nl - @hicannl 
# DATE    : 23-04-2012 
# COMMENT : This script renames all .jpg files to the 
#           name of the .jpg parent folder recursively, 
#           extended with an increasing number. 
#           Put the script in the root of the folders' 
#           parent folder. 
#           NOTE: This script can also be used for renaming 
#           other / multiple files, just adjust the filter! 
########################################################### 
$path = Split-Path -parent $MyInvocation.MyCommand.Definition 
 
Function renameFiles 
{ 
  # Loop through all directories 
  $dirs = dir $path -Recurse | Where { $_.psIsContainer -eq $true } 
  Foreach ($dir In $dirs) 
  { 
    # Set default value for addition to file name 
    $i = 1 
    $newdir = $dir.name + "." 
    # Search for the files set in the filter (*.pst in this case) 
    $files = Get-ChildItem -Path $dir.fullname -Filter *.pst -Recurse 
    Foreach ($file In $files) 
    { 
      # Check if a file exists 
      If ($file) 
      { 
        # Split the name and rename it to the parent folder 
        #$split    = $file.basename #-replace "\.pst$"    #no need to split, you're replacing the entire name
        $replace  = ($newdir + $i + $file.extension)
 
        # Trim spaces and rename the file 
        #$image_string = $file.fullname.ToString().Trim()  #the fullname is already a string, and it can't have leading or trailing spaces
        "$($file.name) renamed to $replace" 
        Rename-Item $file.fullname $replace
        $i++ 
      } 
    } 
  } 
} 
# RUN SCRIPT 
renameFiles 
"SCRIPT FINISHED"

Open in new window


Why do you think it's not working?  Any errors?
The way the script is written, it has to be placed in the parent folder of the folder containing the .PST files.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vpetersenAuthor Commented:
I just had to force a path for it and it worked fine. Not sure what was going on with running it from the root folder. Thanks for updating it for me!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.