Solved

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

Posted on 2016-09-21
12
72 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 18

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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 54

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 54

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 54

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 54

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 54

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 8

Author Closing Comment

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Use Windows Task Scheduler to print a Word document weekly so your printer ink won't dry out.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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