Solved

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

Posted on 2016-09-21
12
45 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
> 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
Comment Utility
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 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
> 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
> 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
Comment Utility
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 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
> 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 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
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 5

Author Closing Comment

by:K B
Comment Utility
Great responses!
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
> 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article will help you understand what HashTables are and how to use them in PowerShell.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

8 Experts available now in Live!

Get 1:1 Help Now