rename files in bulk leaving only last 32 characters (not including dot file extension)

Regardless of if the filename is 10 characters or 100 characters long.

in Powershell preferably.

Thank you very much.

K.B.


ex.

BEFORE: contoso.contoso.spreadsheet.local.renameme.Data_Dump.csv
AFTER: dsheet.local.renameme.Data_Dump.csv
LVL 8
K BAsked:
Who is Participating?
 
SubsunCommented:
Try..
GCI C:\Folder | %{
$Ext = $_.Extension
$Name = $_.Name.TrimEnd($Ext)
 If ($Name.Length -gt 32){
  Ren $_.FullName -NewName "$($Name.substring($Name.Length - 32))$Ext"
  }
}

Open in new window

0
 
xtermieCommented:
You can use the sample code here
https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Rename-Files-b1268678
and make an amendment to rename to the last (right) 32 characters if the length of the file is >32 characters
0
 
Brad99Commented:
not in powershell, but also as a quick work around i recommend the total commander application which also offers the abilitiy to multirename files and/or folders and the nice feature of preview of the results what it would like after starting the operation based on dozen different options.
rename.JPG
0
WEBINAR: 10 Easy Ways to Lose a Password

Join us on June 27th at 8 am PDT to learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees. We'll cover the importance of multi-factor authentication and how these solutions can better protect your business!

 
Joe Winograd, Fellow&MVEDeveloperCommented:
> in Powershell preferably

I don't code in PowerShell, but if you're willing to consider a different scripting language, here's an AutoHotkey script that does it:

SourceFolder:="c:\folder where your files are"
SourceFiles:=SourceFolder . "\*.*"
RenamedCount:=0
NotRenamedCount:=0
Loop,Files,%SourceFiles%
{
  CurrentFile:=A_LoopFileFullPath
  SplitPath,CurrentFile,,FileFolder,FileExt,FileNameNoExt
  If (StrLen(FileNameNoExt)<33)
  {
    NotRenamedCount:=NotRenamedCount+1
    Continue ; leave it alone if it is already 32 chars or fewer
  }
  Else
  {
    RenamedCount:=RenamedCount+1
    StringRight,NewFileNameNoExt,FileNameNoExt,32 ; get last 32 chars not including extension
    NewFile:=FileFolder . "\" . NewFileNameNoExt . "." . FileExt ; create new file name
    FileMove,%CurrentFile%,%NewFile% ; rename file
    If (ErrorLevel<>0)
    {
      MsgBox,4112,Fatal Error,Error Level %ErrorLevel% trying to rename:`n%CurrentFile%`nto:`n%NewFile%
      ExitApp
    }
  }
}
TotalCount:=RenamedCount+NotRenamedCount
MsgBox,4096,Finished,Source Folder: %SourceFolder%`nTotal files=%TotalCount%   Renamed=%RenamedCount%   Not renamed=%NotRenamedCount%
ExitApp

Open in new window


Just change the first line of code to have the folder name. When it finishes, it gives a dialog with the source folder name, the total number of files, the number of files renamed, and the number of files not renamed (because they were already 32 chars or fewer):

RenameLast32Chars
If you're not familiar with AutoHotkey, this EE article will get you started:
AutoHotkey - Getting Started

Regards, Joe
0
 
aikimarkCommented:
Since you want the right-most characters of the basename and only want to rename files (not directories), you should try this version of subsun's PS script:
GCI "c:\users\mark" | ?{!$_.PsIsContainer -and ($_.basename.length -gt 32)} |
   %{ren -path $_.fullname -newname $($_.basename.substring($_.basename.length - 32, 32) + $_.extension)}

Open in new window


Note: The rename should be inside a Try...Catch structure, since it is possible to have a file name collision with this shortening process.
1
 
Joe Winograd, Fellow&MVEDeveloperCommented:
> Note: The rename should be inside a Try...Catch structure, since it is possible to have a file name collision with this shortening process.

Excellent point, aikimark! I hadn't thought of that, although my AutoHotkey script will catch that with the ErrorLevel check. But instead of terminating the script at that point, it's better to continue. Here's a revised script that does not exit, but instead counts the number of rename errors:

SourceFolder:="c:\folder where your files are"
SourceFiles:=SourceFolder . "\*.*"
ErrorCount:=0
RenamedCount:=0
NotRenamedCount:=0
Loop,Files,%SourceFiles%
{
  CurrentFile:=A_LoopFileFullPath
  SplitPath,CurrentFile,,FileFolder,FileExt,FileNameNoExt
  If (StrLen(FileNameNoExt)<33)
  {
    NotRenamedCount:=NotRenamedCount+1
    Continue ; leave it alone if it is already 32 chars or fewer
  }
  Else
  {
    StringRight,NewFileNameNoExt,FileNameNoExt,32 ; get last 32 chars not including extension
    NewFile:=FileFolder . "\" . NewFileNameNoExt . "." . FileExt ; create new file name
    FileMove,%CurrentFile%,%NewFile% ; rename file
    If (ErrorLevel<>0)
      ErrorCount:=ErrorCount+1
    Else
      RenamedCount:=RenamedCount+1
  }
}
TotalCount:=ErrorCount+RenamedCount+NotRenamedCount
MsgBox,4096,Finished,Source Folder: %SourceFolder%`nTotal files=%TotalCount%   Errors=%ErrorCount%   Renamed=%RenamedCount%   Not renamed=%NotRenamedCount%
ExitApp

Open in new window

Here's what the revised closing dialog looks like:

RenamedLast32CharsV2
Thanks to aikimark for that good catch! Regards, Joe
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
> BEFORE: contoso.contoso.spreadsheet.local.renameme.Data_Dump.csv
> AFTER: dsheet.local.renameme.Data_Dump.csv

I just noticed that the AFTER is wrong in the example in the original question. It should be:

AFTER: adsheet.local.renameme.Data_Dump.csv

That's 32 chars — original had just 31. Regards, Joe
0
 
aikimarkCommented:
You can also check for the existence of the new file name in place of the error catch.  I don't think we can get rid of the error trapping, since there are other factors that might interfere with the rename (file open in another application, file no longer exists, etc.)
1
 
Joe Winograd, Fellow&MVEDeveloperCommented:
> You can also check for the existence of the new file name in place of the error catch.

I thought of that, but am happy with the ErrorLevel check (or a Try-Catch pair), although keeping a count of existing files before trying the rename is an interesting idea.
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
I decided that I like the idea of checking if the shortened file name already exists. Here's the revised code:

SourceFolder:="c:\folder where your files are"
SourceFiles:=SourceFolder . "\*.*"
ExistsCount:=0
ErrorCount:=0
RenamedCount:=0
NotRenamedCount:=0
Loop,Files,%SourceFiles%
{
  CurrentFile:=A_LoopFileFullPath
  SplitPath,CurrentFile,,FileFolder,FileExt,FileNameNoExt
  If (StrLen(FileNameNoExt)<33)
  {
    NotRenamedCount:=NotRenamedCount+1 ; already 32 chars or fewer
    Continue
  }
  Else
  {
    StringRight,NewFileNameNoExt,FileNameNoExt,32 ; get last 32 chars not including extension
    NewFile:=FileFolder . "\" . NewFileNameNoExt . "." . FileExt ; create new file name
    IfExist,%NewFile%
    {
      ExistsCount:=ExistsCount+1 ; new file name already exists
      Continue
    }
    FileMove,%CurrentFile%,%NewFile% ; rename file
    If (ErrorLevel<>0)
      ErrorCount:=ErrorCount+1 ; unknown rename error
    Else
      RenamedCount:=RenamedCount+1 ; rename successful
  }
}
TotalCount:=RenamedCount+NotRenamedCount+ExistsCount+ErrorCount
MsgBox,4096,Finished,
(
Source Folder: %SourceFolder%
Total files=%TotalCount%   Renamed files=%RenamedCount%
Not renamed because file name already 32 characters or fewer=%NotRenamedCount%
Not renamed because shortened file name already exists=%ExistsCount%
Not renamed because of unknown rename error=%ErrorCount%
)
ExitApp

Open in new window

And here's the revised closing dialog:

RenamedLast32CharsV3
Regards, Joe
0
 
K BAuthor Commented:
Great responses!
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
> Great responses!

Thanks for the compliment — happy to help! Btw, if you don't know how to accept multiple responses, this EE support article explains it:
http://support.experts-exchange.com/customer/portal/articles/608596-how-do-i-accept-multiple-comments-as-my-solution-?b_id=44

Regards, Joe
1
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.

All Courses

From novice to tech pro — start learning today.