Smarter rename

Discussion & Support for xplorer² professional

Moderators: fgagnon, nikos, Site Mods

User avatar
pschroeter
Silver Member
Silver Member
Posts: 263
Joined: 2007 Jan 27, 00:46

Re: Smarter rename

Post by pschroeter » 2018 Dec 28, 20:14

nikos wrote:
2018 Dec 19, 12:11
the idea is that mass rename inserts text without the user's supervision, so it needs to be "governed", whereas a simple F2 rename is user-controlled. Anyway i'll add this, automatically removing illegal stuff :xmas:
As it sounds like you might be looking to improving mass renaming I want to mention again I desperately want Case changing and a Trim function. This seems to come up a lot with me.

User avatar
nikos
Site Admin
Site Admin
Posts: 14668
Joined: 2002 Feb 07, 15:57
Location: UK
Contact:

Re: Smarter rename

Post by nikos » 2018 Dec 29, 08:46

I think "trimming" (removing of spaces from everywhere?) will be covered by regular expression search and replace. In fact if you are trying to remove only spaces, you can already do it searching for " " and replacing with (nothing)

not sure how case changing works, how do you figure out which letters to capitalize if there are no spaces?

User avatar
pschroeter
Silver Member
Silver Member
Posts: 263
Joined: 2007 Jan 27, 00:46

Re: Smarter rename

Post by pschroeter » 2018 Dec 29, 18:01

By Trimming I mean removing any specified number of characters from the beginning or (usually) end of a string.

By case changing, I mean exactly what they do in a word processor like Microsoft Word. I assume you have one, usually they have options to Capitalize The First Letter of each word or selection, All CAPs, or all lowercase.

"not sure how case changing works, how do you figure out which letters to capitalize if there are no spaces?"

It's at the beginning of the string.

Situations like this come up a lot when I download files and images.

pj
Gold Member
Gold Member
Posts: 407
Joined: 2006 Jan 26, 14:01
Location: Florida

Re: Smarter rename

Post by pj » 2018 Dec 30, 00:43

awxRename has several "native" options (on the context menu) including upper, lower and sentence case. There's an advanced option to execute a Perl script against the names, too. I have it installed and also use F2 Mass Rename.

Hasn't been updated since 2006 but still works fine for me.
-----------------------------
PJ in (70's) FL

User avatar
pschroeter
Silver Member
Silver Member
Posts: 263
Joined: 2007 Jan 27, 00:46

Re: Smarter rename

Post by pschroeter » 2018 Dec 30, 01:14

Are you using Windows 10?
I have Rename Master it does so much more than I need that I don't feel like starting a separate program just to capitalize a few file names.

User avatar
nikos
Site Admin
Site Admin
Posts: 14668
Joined: 2002 Feb 07, 15:57
Location: UK
Contact:

Re: Smarter rename

Post by nikos » 2018 Dec 30, 07:59

wrt trimming, xplorer2 rename has the character range construct $[3:2] that specifies a starting position and a length. This can be used either for search and replace or entire name replace, meaning that you can trim both start and end

pj
Gold Member
Gold Member
Posts: 407
Joined: 2006 Jan 26, 14:01
Location: Florida

Re: Smarter rename

Post by pj » 2019 Mar 13, 02:04

I decided to have a go at a function to create folders from random strings copied to the clipboard (e.g. book names, movies, URLs) and automatically do two things:

1. Change the invalid characters for a predetermined list of substitutes.
2. Change the case to Title Case, since most of the time I'm grabbing the title of something and creating a new folder from it.

So, here's the code in VBScript. I know it's not AUTOIT or these other sexy things, but VBS is still pretty ubiquitous. As far as I know, most everyone has WSCRIPT on their system. All you need is NOTEPAD to make changes.

Copy the code into a new text file and name it "CreateNewFolder.vbs".
Create the following user command, put the user command onto the Toolbar and make merry with the new tool.

Code: Select all

>I:\MISC\wscript I:\MISC\CreateNewFolder.vbs "$P"
Of course, for your command line change the paths to suit your setup. WSCRIPT.EXE usually won't need a path as it'll be in the Windows folder, but just for fun, I put the script file and WSCRIPT.EXE onto a small (8GB) SD card that I don't use for photos anymore, put that into the SD slot of the laptop and created a RAMDRIVE equivalent tool repository. Never have to wait for a disk to spin back up to execute the tools, so it's a nice little boost.

OK, after all that preamble, here's my very simple script, with a little error checking to keep from doing too many stupid things:

Code: Select all

' VB Script Document
option Explicit

'===========================================================================================
' CreateNewFolder.vbs
' Requires XPLORER2 - run as a user command with the syntax:
'    wscript {path}\CreateNewFolder.vbs "$P"
'
' Uses the string captured in the clipboard and creates a folder in the current active pane.
' Features: 
'    - Removed invalid characters from the folder name and substitutes per a predetermined list
'    - Changes the folder name to Title Case
'    - Checks for same named folder before creating a new folder.
'===========================================================================================


' 	COMMON VARIABLES 
Dim fso, Wsh, RootFolder, DEBUGG

'  	MAIN
Dim MainFolder, fldr, objHTML, exists, f, CBText, FullFileName

'	strClean
Dim tempstr, charArray, tmpChar, changeTo, i

'===== SET GLOBALS =====
' Uncomment "true" to get messages during execution for debugging 
'    DEBUGG = True
    DEBUGG = False

'================================================================'
'     begin program                                                '
'================================================================'

   ' create common objects'
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set Wsh = WScript.CreateObject("WScript.Shell")

    RootFolder = "{BLANK}"
    If WScript.Arguments.Count > 0 Then 
    	RootFolder = WScript.Arguments.Item(0)

    	If DEBUGG Then  MsgBox  "RootFolder = " & RootFolder

    	Main
    	
    Else    ' SCRIPT ENDS WITHOUT EXECUTING
        MsgBox "NO VARIABLES ON COMMANDLINE FOR ROOTFOLDER"
    End If	
    
' CLEANUP    
	Set fso = Nothing
	Set Wsh = Nothing
'================================================================'

Sub Main

   	Set MainFolder = fso.GetFolder(RootFolder)

' Get clipboard text
	Set objHTML = CreateObject("htmlfile")
	CBText = objHTML.ParentWindow.ClipboardData.GetData("text")
	If CBText = "" Then
	   MsgBox "Nothing in CLIPBOARD"
	   Exit Sub
	End If
	If DEBUGG Then MsgBox CBText

	FullFileName = RootFolder & "\" & strClean (CBText)	
	If DEBUGG Then MsgBox "FullFileName = " & FullFileName
	
	exists = fso.FolderExists(FullFileName)

	If (exists) Then 
		MsgBox "Folder " & FullFileName & " already exists!", vbOKOnly+vbExclamation, "Folder Creation Error"
	Else	
		If DEBUGG Then 
			MsgBox "New folder is " & FullFileName & vbCRLF & "  (not created in DEBUGG mode)"
		Else
			Set f = fso.CreateFolder(FullFileName)
		End If	
	End If
' cleanup	
	Set MainFolder = Nothing
	Set f = NOTHING
	Set objHTML = Nothing

End Sub


Function strClean (strtoclean)
Dim charArray
tempstr = strtoclean
charArray = Array("?", "/", "\", ":", "*", """", "<", ">", "|") 

For Each tmpChar in charArray
	Select Case tmpChar
	Case "/", "\"
		changeTo = "-"
	Case ":"
		changeTo = " -"
	Case "<"
		changeTo = "["
	Case ">"
		changeTo = "]"
	Case Else
		changeTo = ""
	End Select
	
	tempstr = replace( tempstr, tmpChar, changeTo )
Next

strClean = TCase (tempstr) 	' = tempstr
End Function

Function TCase(strTextString)
'Convert string to Title Case

Dim arrTextItem, strTextNew, strSplitText, strSplitItem, y, x
	strSplitText = " '-"
	For y = 1 to len(strSplitText)
		strSplitItem = Mid(strSplitText,y,1)
		arrTextItem = Split(strTextString, strSplitItem)
		For x = 0 to Ubound(arrTextItem)
			If strSplitItem = "'" Then
				If Mid(arrTextItem(x),2,1) = " " Then
					strTextNew = strTextNew & strSplitItem & LCase(Left(arrTextItem(x),1)) & Right(arrTextItem(x),Len(arrTextItem(x))-1)
				Else
					strTextNew = strTextNew & strSplitItem & UCase(Left(arrTextItem(x),1)) & Right(arrTextItem(x),Len(arrTextItem(x))-1)
				End If
			Else
				If strSplitItem = "-" Then
					strTextNew = strTextNew & strSplitItem & UCase(Left(arrTextItem(x),1)) & Right(arrTextItem(x),Len(arrTextItem(x))-1)
				Else
					strTextNew = strTextNew & strSplitItem & UCase(Left(arrTextItem(x),1)) & LCase(Right(arrTextItem(x),Len(arrTextItem(x))-1))
				End If
			End If
		Next
		strTextString = Right(strTextNew,Len(strTextNew)-1)
		strTextNew = ""
	Next
	
	TCase = strTextString

End Function
Don't know if it handles Unicode or anything else, but let me know if it benefits you.

-----------------------------
PJ in (the most wonderful time of the year, late winter) FL

User avatar
johngalt
Silver Member
Silver Member
Posts: 294
Joined: 2008 Feb 10, 19:41
Location: 3rd Rock
Contact:

Re: Smarter rename

Post by johngalt » 2019 Mar 15, 01:38

nikos wrote:
2018 Dec 30, 07:59
wrt trimming, xplorer2 rename has the character range construct $[3:2] that specifies a starting position and a length. This can be used either for search and replace or entire name replace, meaning that you can trim both start and end
I did not know this. Thanks, Nikos!
Image

Image

User avatar
johngalt
Silver Member
Silver Member
Posts: 294
Joined: 2008 Feb 10, 19:41
Location: 3rd Rock
Contact:

Re: Smarter rename

Post by johngalt » 2019 Mar 15, 01:40

pj wrote:
2019 Mar 13, 02:04
I know it's not AUTOIT or these other sexy things, but VBS is still pretty ubiquitous. As far as I know, most everyone has WSCRIPT on their system. All you need is NOTEPAD to make changes.
2 things.

1) Awesome job, thank you!

2) VBS is by far sexier than having to have yet another executable / runtime executable installed on my machine to accomplish a simple task. VBS is nice - and the bonus about being able to edit in notepad definitely helps - good job!
Image

Image

EMathews3
Member
Member
Posts: 63
Joined: 2014 Aug 23, 12:54

Re: Smarter rename

Post by EMathews3 » 2019 Jul 04, 13:55

nikos wrote:
2018 Dec 30, 07:59
wrt trimming, mass rename has the character range construct $[3:2] that specifies a starting position and a length.
Yes, please add this to the $-token help. I always forget the syntax. Comma? Hyphen? Colon. Starting and ending position? Length.

EMathews3
Member
Member
Posts: 63
Joined: 2014 Aug 23, 12:54

Re: Smarter rename

Post by EMathews3 » 2019 Oct 29, 00:13

mass rename has the character range construct $[3:2] that specifies starting position and length
Could not find mention of this in the Help PDF. The documentation exists in the online manual, the program folder's version at x2Help.htm#ma_renwiz, and in this thread.

User avatar
nikos
Site Admin
Site Admin
Posts: 14668
Joined: 2002 Feb 07, 15:57
Location: UK
Contact:

Re: Smarter rename

Post by nikos » 2019 Oct 29, 15:40

the PDF help file is dead for a long time now, Narayan has moved to other pastures

EMathews3
Member
Member
Posts: 63
Joined: 2014 Aug 23, 12:54

Re: Smarter rename

Post by EMathews3 » 2019 Oct 30, 15:27

The new RegExp checkbox in 4.3.0.0 Ult has a hotkey conflict with the existing Rename button. It also fails to respond to its hotkey; a checkbox is supposed to toggle its checked/unchecked state when its hotkey fires. The obvious choice is to change the RE hotkey from Alt+R to Alt+E, because the Rename button has precedence of seniority.

Image

I use this dialog several times every day, without ever touching the mouse. Workflow is:
1. Select several files, press F2 for Mass rename
2. edit What to match, press Tab
3. edit Target name template, press Enter (Preview is the default button), review Old names / New names
[repeat from 2. as needed, which is not often]
4. press Alt+R (hotkey for Rename button), done.

The regression is that sometimes the Alt+R sends focus to the RE checkbox, which is unexpected, because the Rename button still shows its own hotkey as Alt+R. Now with *two* controls listening for Alt+R, whichever one gets fired is determined by which control has focus currently, relative to the dialog's tab order. As in:
- user presses Alt+R
- dialog walks the tab order, *beginning with the currently-focused control*, searching for a control with an accelerator of Alt+R
- when the first matching control is found, walking stops and that control gets focus; it is also supposed to behave as if it had been clicked

So:

Code: Select all

Control with Focus   | Responds to Alt+R
---------------------|------------------
Mode                 | RE checkbox
Preserve extension   | RE checkbox
RE checkbox          | Rename button
What to match        | RE checkbox
Target name template | Rename button
Preview button       | Rename button
Special tokens       | Rename button
Old name / New name  | Rename button
$-Token help         | Rename button
Rename button        | RE checkbox
Cancel button        | Rename button
Really, dialogs need to have zero hotkey conflicts.

BTW why are these buttons not capitalized? That's just weird and annoying. Button labels should be capitalized, especially when when the first letter of the first word is a hotkey.

Usability wish: the Enter key should advance the process to its next step, regardless of what step is current. As in, Preview should be the default button until the proposed renames are displayed. At that point, Rename should become the default button. However, if the proposed renames become stale for whatever reason, like user updates any of the input fields, then default should revert to the Preview button.

User avatar
nikos
Site Admin
Site Admin
Posts: 14668
Joined: 2002 Feb 07, 15:57
Location: UK
Contact:

Re: Smarter rename

Post by nikos » 2019 Oct 30, 17:34

ok I will change the accelerator key for RE checkbox

EMathews3
Member
Member
Posts: 63
Joined: 2014 Aug 23, 12:54

Re: Smarter rename

Post by EMathews3 » 2020 Apr 08, 21:00

Thanks for extending this feature to make it alone worth buying the whole package :)

Edit: Gahhh never mind, this is already done in 4.4.0.0!

Using 4.3.0.9 ULT x64 2020-03-01. In the Mass Rename Wizard, could you possibly add Ctrl-A / Select All to "What to match", "Target name template", and "Predefined"? Yes these are ComboBoxes not edit controls, so Select All may not be built in. But since these *are* used for editing, that extra bit of usability would help.

Post Reply