Rename: Remove illegal chars & trim to 255 chars

English support forum

Moderators: Hacker, petermad, Stefan2, white

Post Reply
Ruriko
Junior Member
Junior Member
Posts: 19
Joined: 2012-08-26, 12:50 UTC

Rename: Remove illegal chars & trim to 255 chars

Post by *Ruriko »

I want rename my files by removing all illegal characters and also trim it if longer than 255 characters. Is this possible in one go? Please tell me the method
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Multi Rename Tool: remove illegal chars, trim 255 length

Post by *Stefan2 »

You don't have to "rename files by removing all illegal characters"
because it is not allowed for files to have "illegal characters".
So nothing to do here.

Also, if you have file names longer then 255 chars,
it is not easy to process them in an Win32 environment.
Good results were reported by utilizing a Linux live boot-CD.

But you can try TCs' MRT also by using a name mask like [N1-250]



MRT=Multi Rename Tool 
User avatar
milo1012
Power Member
Power Member
Posts: 1158
Joined: 2012-02-02, 19:23 UTC

Re: Remove illegal chars & trim

Post by *milo1012 »

Ruriko wrote:removing all illegal characters
Which characters do you mean?
Windows forbidden are

Code: Select all

\ / : * ? " < > |
Actually no files can exist with these, so I wonder what you mean by removing them.
Some remote file system where they are allowed?
You could use search and replace in the MRT then, like
search:

Code: Select all

\|/|:|*|?|"|<|>||
(not sure if the last one would actually work).
Ruriko wrote:trim it if longer than 255 characters
There is basically no file system that can have such long names above 255.
http://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits
If you are referring to full path names (also sometimes named "full file names"),
yes, there is a limit of 259 characters ("MAX_PATH"),
but you can't use the [N1-250] mask that way, you'd need to shorten the whole path,
which depends on your path structure though.
TC plugins: PCREsearch and RegXtract
kes
Junior Member
Junior Member
Posts: 19
Joined: 2007-08-03, 03:10 UTC

Post by *kes »

Struggling with this issue. I want to shorten all the paths to less than 259 characters and cannot find a way to do this.
User avatar
milo1012
Power Member
Power Member
Posts: 1158
Joined: 2012-02-02, 19:23 UTC

Post by *milo1012 »

kes wrote:Struggling with this issue. I want to shorten all the paths to less than 259 characters and cannot find a way to do this.
I don't think there's an automatic way, at least not when the root of the problem isn't caused by a single overlong dir name.
It depends on your path structure where you'd modify (shorten) things.
You need to group such problematic files as the path structure requires it, and rename them separately.

I'd use the FilenameChrCount plug-in to check for paths > 259 (use the first field from that plug-in) in the first place.
Search them with Alt+F7 by activating the plug-in field and OP > and value 259.
Now there are several ways.
If such paths have a long file name (i.e. the actual file name after all path/dir structures is quite long),
you could simply shorten this by using [N1-32] and similar things in the MRT mask field.
If some parent dir name is quite long you'd select only those dir names for MRT and shorten them just like above.

If you don't want to or just can not shorten individual names you need to move some dirs to a 'higher' level in the path structure.
So like I said: there is probably no automatic way, as it depends on your specific requirements.
TC plugins: PCREsearch and RegXtract
kes
Junior Member
Junior Member
Posts: 19
Joined: 2007-08-03, 03:10 UTC

Post by *kes »

milo1012 wrote:
kes wrote:Struggling with this issue. I want to shorten all the paths to less than 259 characters and cannot find a way to do this.
I don't think there's an automatic way, at least not when the root of the problem isn't caused by a single overlong dir name.
It depends on your path structure where you'd modify (shorten) things.
You need to group such problematic files as the path structure requires it, and rename them separately.

I'd use the FilenameChrCount plug-in to check for paths > 259 (use the first field from that plug-in) in the first place.
Search them with Alt+F7 by activating the plug-in field and OP > and value 259.
Now there are several ways.
If such paths have a long file name (i.e. the actual file name after all path/dir structures is quite long),
you could simply shorten this by using [N1-32] and similar things in the MRT mask field.
If some parent dir name is quite long you'd select only those dir names for MRT and shorten them just like above.

If you don't want to or just can not shorten individual names you need to move some dirs to a 'higher' level in the path structure.
So like I said: there is probably no automatic way, as it depends on your specific requirements.
Thanks for the link to plugins. That will be useful. I still think that some options, as with when copy/move encounters duplicate file names, could be given. Truncate file name, truncate folder name, truncate parent folder name, etc. Don't know how useful they would be but at least it is some options/solutions to the problem.
Zom-B
Junior Member
Junior Member
Posts: 36
Joined: 2006-11-20, 17:46 UTC

Post by *Zom-B »

Same problem here. I have several directories in a main directory (total path length is quite short) but the directories have different lengths. In those directories are files with ridiculously long names. All extensions are exactly 4 long (e.g. ".JPG")

What I need is something like:

[N1-{length([=tc.path])-255}]
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

"ShortenFilename.vbs" (Script Content Plugin)

Post by *Stefan2 »

2Zom-B

if you really want, you can utilize a content plugin with a script in MRT to shorten the file names.
But that could lead to duplicate names.
EDIT: But then...
ghisler wrote:When you get the warning, you can choose to rename anyway - TC then skips the duplicates.
Then just change the name slightly, e.g. by appending an "a" or "2" and restart renaming.
http://ghisler.ch/board/viewtopic.php?p=231316#231316



How-to:

0. Do a backup first
1. install the plugin, create the script and configure the plugin (see below script (code) for instructions)
2. utilize TCs' branch view (if wanted, to work on sub folders)
3. select the files and launch MRT
4. for "Rename mask: file name" enter: [=script.Result3]
5. check the result list, maybe click at Start!
6. good luck...






EDIT, v0.002: since you don't take care on exact file names, I have added a function
to add an random string to all modified names, so all are unique now (see '4.) 'Test with MRT:)

But with above mentioned tip of Ghisler, this should not be really need.


"ShortenFilename.vbs" (Script for Script Content Plugin 0.2)

Code: Select all

'//Found at http://ghisler.ch/board/viewtopic.php?p=299013#299013

'// Description:
'*** Script for Script Content Plugin 0.2
'*** http://www.totalcmd.net/plugring/script_wdx.html
'*** http://wincmd.ru/plugring/script_wdx.html
'*** In default var "filename" is the current file incl. full path
'Some more information: http://ghisler.ch/board/viewtopic.php?p=206065#206065

'// Script Info
' 2015-08-19 - v0.001 by Stefan : initial
' 2015-08-19 - v0.002 by Stefan : add random string to prevent duplicate file names

'// Purpose:
'Bearbeite jeden Dateinamen und kürze die Länge, falls nötig.
'(Work on each filename and shorten them to a wanted length)

'// Steps:
'1.)
' Nach Installation des obigen Plugins im Installationsverzeichnis des Plugins 
' eine Datei namens "ShortenFilename.vbs" mit folgendem Inhalt anlegen:
'(After installing the above plugin, create a plain text file
' with name "ShortenFilename.vbs" in the script folder, with following content:)

'//########################################## Script start:

vOrgName = filename
'---

'//Get a random string to prevent duplicate file names:
'vRnd1 = 0 :  Randomize : vRnd1 = int( (99-1+1) * Rnd +1) ' << not very reliable!
 Set TypeLib = CreateObject("Scriptlet.TypeLib") : vRnd2 = mid(TypeLib.Guid,2,2)
'---

If InStr(vOrgName, "." ) Then
	vBase = left(vOrgName , InstrRev( vOrgName, ".") - 1)
	vExte = mid( vOrgName , InstrRev( vOrgName, ".") + 1)
	'msgbox "Base: " & vBase & "  <DOT>  " & vExte
Else
	vBase = vOrgName
End If
'---

'//Call the wanted functions and modify the file name as wanted for the output:
'Call ShortenLength() function:
vShortName = ShortenLength(vBase)
'---

'//The functions to modify the file names:
Function ShortenLength(vInputStr)
vMaxLengthToWorkOn = 99 'only work on files of this length or more (set to 255 if wanted)
vKeepMinimumLength = 25 'keep at least this minimum length after shorting
	If len(vInputStr) > vMaxLengthToWorkOn Then 
		vNewStrLen    = (len(vInputStr) - vMaxLengthToWorkOn + vKeepMinimumLength)
		'                                              vRnd2 will add a random string 
		'                                              (just remove it, if not wanted):
		ShortenLength = left(vInputStr, vNewStrLen)     & "_(" & vRnd2 & ")" 
	Else
		ShortenLength = vInputStr
	End If
End Function 'ShortenLength(vInputStr)
'---
Function RemoveInvalidChars(vInputStr) 
    RemoveInvalidChars = vInputStr
    MyArray = Array("<", ">", "|", "/", "*", "", "?", ":", """")
    For X = 0 To UBound(MyArray)
        RemoveInvalidChars = Replace(RemoveInvalidChars, MyArray(X), "_", 1)
    Next 
End Function 'RemoveInvalidChars(vInputStr) 
'---

'//Modification done. Give the result(s) back to TC
content  = vOrgName   '  in TC use [=script.Result]  and remove [E] from extension field
content1 = vBase      '  in TC use [=script.Result1] 
content2 = vExte      '  in TC use [=script.Result2] 
content3 = vShortName '  in TC use [=script.Result3] 
content4 = "script.Result4 is not used yet"
content5 = vBase & "_bak" 
content6 = ""
content7 = ""
content8 = ""
content9 = ""
'//########################################## Script End

'2.)
'Anschließend die script.ini editieren:
'(Afterwards modify the script.ini)
	 '[Script]
	 'Section=ShortenFilename

	 '[ShortenFilename]
	 'LongName=0
	 'ParseDirs=0
	 'Script=ShortenFilename.vbs

'3.)
 'Und zuletzt benutzerdefinierte Spalten anlegen mit Feldinhalt: 
 ' [=script.Result], [=script.Result1], [=script.Result2], ..., [=script.Result9]
 ' Bzw. diese Parameter im MUT (Multi Umbenenn Tool) benutzen
 '(At the end, use the result as 'User Definied Columns' or with MRT 'MultiRenameTool')

'4.)
'Test with MRT:

'FROM ORIGINAL:
'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.ext
'ABC1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.ext
'ABC1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567899.ext
'ColorRules.tpl
'ConfigProfiles.xml
'DEFGHIJK1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.ext

'TO SHORTEN LENGTH , v0.001:
'12345678901234567890123456.ext
'ABC12345678901234567890123456.ext
'ABC12345678901234567890123456 <78901234567890123456789012345678901234567890123456789012345678901234567899.ext not renamed due to duplicate>
'ColorRules.tpl
'ConfigProfiles.xml
'DEFGHIJK12345678901234567890123456.ext

'TO SHORTEN LENGTH , v0.002:
'12345678901234567890123456_(2C).ext
'ABC12345678901234567890123456_(7B).ext
'ABC12345678901234567890123456_(44).ext
'ColorRules.tpl
'ConfigProfiles.xml
'DEFGHIJK12345678901234567890123456_(DF).ext

'-------------

' BONUS:
'
'In an second step you can re-load the list by clicking the Reload button at the bottom of the MRT.
'And this time perform a search&replace with regular expression 
'to remove the random string from all file not beeing a duplicate:
'
'Search: (.+)_\(..\)
'Replace; $1
'[X]RegEx
'
'Some files can't be renamed due to duplicate name, but now that's for good. That are the duplicate one.
'
'12345678901234567890123456.ext
'ABC12345678901234567890123456.ext
'ABC12345678901234567890123456_(44).ext
'ColorRules.tpl
'ConfigProfiles.xml
'DEFGHIJK12345678901234567890123456.ext


'<EOF>


 
Zom-B
Junior Member
Junior Member
Posts: 36
Joined: 2006-11-20, 17:46 UTC

Post by *Zom-B »

Don't worry, all long files start with a unique 7-digit number. Although the script may be useful for others without this luxury.

[edit] First I couldn't find a Script Content Plugin but found it on a 3rd party site now.

[edit2] Can't install it. Error: only for 32-bit.
Post Reply