Solved

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

Posted on 2016-09-21
12
58 Views
Last Modified: 2016-09-24
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
0
Comment
Question by:K B
12 Comments
 
LVL 17

Expert Comment

by:xtermie
ID: 41809079
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
 
LVL 2

Expert Comment

by:Brad99
ID: 41809142
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
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 41809165
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 52

Expert Comment

by:Joe Winograd, EE MVE
ID: 41809296
> 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
 
LVL 45

Expert Comment

by:aikimark
ID: 41809383
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
 
LVL 52

Expert Comment

by:Joe Winograd, EE MVE
ID: 41809421
> 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
 
LVL 52

Expert Comment

by:Joe Winograd, EE MVE
ID: 41809464
> 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
 
LVL 45

Expert Comment

by:aikimark
ID: 41809622
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
 
LVL 52

Expert Comment

by:Joe Winograd, EE MVE
ID: 41809632
> 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
 
LVL 52

Expert Comment

by:Joe Winograd, EE MVE
ID: 41809713
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
 
LVL 6

Author Closing Comment

by:K B
ID: 41813928
Great responses!
0
 
LVL 52

Expert Comment

by:Joe Winograd, EE MVE
ID: 41813940
> 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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now