Avatar of K B
K B
Flag for United States of America asked on

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
VBAPowershell

Avatar of undefined
Last Comment
Joe Winograd

8/22/2022 - Mon
Anastasia D. Gavanas

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
Brad99

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
ASKER CERTIFIED SOLUTION
SubSun

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Joe Winograd

> 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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
aikimark

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.
Joe Winograd

> 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
Joe Winograd

> 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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
aikimark

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.)
Joe Winograd

> 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.
Joe Winograd

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
K B

ASKER
Great responses!
Joe Winograd

> 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