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
GCI "c:\users\mark" | ?{!$_.PsIsContainer -and ($_.basename.length -gt 32)} |
%{ren -path $_.fullname -newname $($_.basename.substring($_.basename.length - 32, 32) + $_.extension)}
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
Here's what the revised closing dialog looks like: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
And here's the revised closing dialog:
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