How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

English support forum

Moderators: Stefan2, white, sheep, Hacker

fliptoback
Junior Member
Junior Member
Posts: 11
Joined: 2019-05-15, 12:23 UTC

How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

Post by *fliptoback » 2019-05-15, 12:29 UTC

Hi,

I regularly copy files to a folder and then rename them to say XXX_panasoinc, XXX_canon, etc.

Take for instance I wanted to rename all the files in the folder to XXX_panasonic. Is there a way whereby I can assign a button to TC such that if I select all the files and press that button, it will automatically append "panasonic" to the end of the original filename? likewise if i wanted to appendix "canon" to the end of the filename, I would select all the files and press the icon that is associated with the Canon button.

Thanks

User avatar
Stefan2
Power Member
Power Member
Posts: 2588
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

Post by *Stefan2 » 2019-05-15, 13:42 UTC

Hi fliptoback and welcome.

You can just execute a shell script to do that, TC will aid you with the list of all selected files (or the current folder name / Path)


First steps:
- Right click the button bar to create a new button (Change...).
- In the button dialog click at [Help].
- Scroll down and read at "%L,...... create a list file in the TEMP directory with the names of the selected files and directories,
- Now you only need some script of any kind to perform the actual work :D

TC will provide a temporary file (%F/%L) with each selected items, one per line (Tip: Dir's have got a trailing backslash).
Now use a tool of your choice to parse the temp file (%F/%L) line-by-line:
VBScript, JavaScript, MS-DOS Batch, AutoHotkey, AutoIt, PascalScript, PowerPro, PowerShell, Python, Perl, Rexx....
The people on this forum mostly use Batch, VBS, AutHotkey and PowerPro.


Such script you can call from a button, or create a "user defined command" (UDC ; usercmd.ini)
The UDC you can use later as command for a button too, and also can assign a keyboard shortcut to.

Example Total Commander Button code:

Code: Select all

Command: "D:\rive\path to my\ForEachSelFileDo.ext"  (or portable: "%COMMANDER_PATH%\TOOLs\Scrpts\ForEachSelFileDo.ext")
Parameters: "%F" or "%L" or unicode forms
Start path: 
Icon file: WCMICONS.DLL
Tooltip: Script to do smtg with each file from "%F"
'For parameters see > Help > Operation > Menus > Menu: Configuration > Change Button Bar > Configuration - Change button bar.
' %L / %F : Create a list file in the TEMP directory with the names of the selected files and directories.



Example script in VBS

Code: Select all

' Total Commander VBScript "ForEachSelectedFileDo -smtg.vbs" by Stefan, 2014-07-24, v0.01
'   Found at: https://ghisler.ch/board/viewtopic.php?p=355257#p355257
' Usage:
'    1. Create this script ForEachSelFileDo.vbs as plain text file.
'    2. Create the below button.
'    3. Select some files and click that new button.
'
'       TotalCommander Button code:
'       CMD:     "D:\rive\path to my\ForEachSelFileDo.vbs"  (or portable: "%COMMANDER_PATH%\TOOLs\VBS\ForEachSelFileDo.vbs")
'       PARAM:   "%F"
'       START:
'       ICON:    C:\Windows\System32\WScript.exe,2
'       TOOLTIP: ForEachSelFileDo VBS
'
'For parameters see > Help > Operation > Menus > Menu: Configuration > Change Button Bar > Configuration - Change button bar.
'    %L / %F : Create a list file in the TEMP directory with the names of the selected files and directories.

If Wscript.arguments.count < 1 Then MsgBox "Error: TC-Param missing..." : WScript.Quit
strTCtempList = Wscript.arguments.Item(0) ' The TC temp file due to the "%L" parameter
Set FSO = CreateObject("Scripting.FileSystemObject")
If  FSO.FileExists(strTCtempList) Then
        Set oTextStream = FSO.OpenTextFile(strTCtempList,1)
        Do Until oTextStream.AtEndOfStream
            strLine    = oTextStream.ReadLine
            strNewFilename = replace(strLine,"e","OOOO") '//modifiy the string here
            If not (FSO.FileExists(strNewFilename)) Then
                MsgBox "Old: " & strLine &vbLF& "New: " & strNewFilename
                '//FSO.MoveFile strLine, strNewFilename
            End If
        Loop
        oTextStream.Close
Else
        MsgBox "Input file  strTCtempList  not found. Check button parameter and quoting."_
                &vbLF&vbLF & strTCtempList,,"TC-Script Error - ForEachSelFileDo"
End If

This is one possibility only.



- - -

Some more code snippets:

Code: Select all

If  FSO.FileExists(sTCtempList) Then
	Set oTextStream = FSO.OpenTextFile(sTCtempList,FORREADING)
	Do Until oTextStream.AtEndOfStream
		iCurrFileCount = iCurrFileCount + 1

		'//Get basic properties of current file
		sFullName = oTextStream.ReadLine	'//Ex: "X:\Backup\File.txt"
		If(Right(sFullName,1)= "\") Then 
			Set oItem = FSO.GetFolder(sFullName)
		Else
			Set oItem = FSO.GetFile(sFullName)
		End If
		sName    = FSO.GetFileName(oItem)	    '//Ex: "File.txt"
		sBase     = FSO.GetBaseName(oItem)	    '//Ex: "File"
		sExte     = FSO.GetExtensionName(oItem) '//Ex: "txt"
		sSize     = oItem.Size
		sVers     = FSO.GetFileVersion(oItem)
		sCreated  = oItem.DateCreated
		sAccessed = oItem.DateLastAccessed
		sModified = oItem.DateLastModified
		'Attributes: integer value: 0 (Normal), 1 (ReadOnly), 3 (Hidden), 4 (System), 8 (Volume), 
		'16 (Directory), 32 (Archive), 64 (Alias), and 128 (Compressed). ReadOnly(1) + System(4) + Archive(32) = 37
		sAttributes = oItem.Attributes
		'MsgBox sFullName &LF& sName &LF& sBase &LF& sExte
		'//================================================================================
		sDrive = FSO.GetDriveName(oItem)	'//Ex: "X:"
		sPath  = oItem.ParentFolder		'//Ex: "X:\Backup"
		'//================================================================================
		aPathParts = split(sPath, "\")
		iUBound 	= UBound(aPathParts)
		sParentFolder1 	= aPathParts(iUBound)	'//Ex: "Backup"
		If (iUBound > 0) Then sParentFolder2  =  aPathParts(iUBound -1)
		If (iUBound > 1) Then sParentFolder3  =  aPathParts(iUBound -2)
		If (iUBound > 0) Then sTopFolder1   =  aPathParts(1)
		If (iUBound > 1) Then sTopFolder2  =  aPathParts(2)
		If (iUBound > 2) Then sTopFolder3  =  aPathParts(3)
		'MsgBox sDrive&LF&sPath&LF&iUBound&LF&sParentFolder1&LF&sParentFolder2&LF&sTopFolder2
		'//================================================================================
		sModified = oItem.DateLastModified	'// 16.05.2018 09:20:00			Deutsch German format
		sDate = split(sModified," ")(0)
		sTime = replace(split(sModified," ")(1),":","")
		sDay = split(sDate,".")(0)
		sMon = split(sDate,".")(1)
		sYer = split(sDate,".")(2)
		strTimestamp = sYer &"-"& sMon &"-"& sDay &"-"& sTime 
		'//================================================================================
		'//================================================================================
		



HTH?
Inofficial FAQs || WIKI (Deu/Eng) || TC Home (What's new? // FAQ // Download // Order // Addons // Tools // Plugins)
Erst wenn der letzte Baum gefällt oder die letzte Biene verendet ist, werden die Leute verstehen warum Umweltschutz wichtig ist.

fliptoback
Junior Member
Junior Member
Posts: 11
Joined: 2019-05-15, 12:23 UTC

Re: How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

Post by *fliptoback » 2019-05-15, 14:46 UTC

Thanks Stefan. I will try that and report back. Cheers for the reply :)

User avatar
petermad
Power Member
Power Member
Posts: 8470
Joined: 2003-02-05, 20:24 UTC
Location: Valsted, Denmark
Contact:

Re: How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

Post by *petermad » 2019-05-15, 16:45 UTC

You can also use a saved Multi-Rename Tool setting:
1. open the Multi-Rename Tool (Ctrl+M)
2. In the "Rename mask" field type [N]_Panasonic
3. Press F2 to save the settings - call it Panasonic.
4. Close the Multi-Rename Tool.
5. Make this button:

Code: Select all

TOTALCMD#BAR#DATA
MULTIRENAME Panasonic

wcmicons.dll,46
Multi Rename Tool - Panasonic


-1
To make the button:
1. Mark the green text above (click SELECT ALL).
2. Copy it to the ClipBoard (press Ctri+C).
3. Right click on TC's buttonbar and choose "Paste".
Now you can open the Multi-Rename Tool with the Panasonic setting with this button - then you only have to click the Start! button.
License #524 (1994)
Danish Total Commander Translator
TC 9.22a 32+64bit on Win XP 32bit, Win 7, 8.1 & 10 (1903) 64bit, 'Everything' 1.4.1.935 (x64)
TC 3.0b6 on Android 6.0
Get: Extended Total Commander Menus | PHSM-Calendar

fliptoback
Junior Member
Junior Member
Posts: 11
Joined: 2019-05-15, 12:23 UTC

Re: How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

Post by *fliptoback » 2019-05-16, 00:04 UTC

Stefan2 wrote:
2019-05-15, 13:42 UTC

Code: Select all

' Total Commander VBScript "ForEachSelectedFileDo -smtg.vbs" by Stefan, 2014-07-24, v0.01
'   Found at: https://ghisler.ch/board/viewtopic.php?p=355257#p355257
' Usage:
'    1. Create this script ForEachSelFileDo.vbs as plain text file.
'    2. Create the below button.
'    3. Select some files and click that new button.
'
'       TotalCommander Button code:
'       CMD:     "D:\rive\path to my\ForEachSelFileDo.vbs"  (or portable: "%COMMANDER_PATH%\TOOLs\VBS\ForEachSelFileDo.vbs")
'       PARAM:   "%F"
'       START:
'       ICON:    C:\Windows\System32\WScript.exe,2
'       TOOLTIP: ForEachSelFileDo VBS
'
'For parameters see > Help > Operation > Menus > Menu: Configuration > Change Button Bar > Configuration - Change button bar.
'    %L / %F : Create a list file in the TEMP directory with the names of the selected files and directories.

If Wscript.arguments.count < 1 Then MsgBox "Error: TC-Param missing..." : WScript.Quit
strTCtempList = Wscript.arguments.Item(0) ' The TC temp file due to the "%L" parameter
Set FSO = CreateObject("Scripting.FileSystemObject")
If  FSO.FileExists(strTCtempList) Then
        Set oTextStream = FSO.OpenTextFile(strTCtempList,1)
        Do Until oTextStream.AtEndOfStream
            strLine    = oTextStream.ReadLine
            strNewFilename = replace(strLine,"e","OOOO") '//modifiy the string here
            If not (FSO.FileExists(strNewFilename)) Then
                MsgBox "Old: " & strLine &vbLF& "New: " & strNewFilename
                '//FSO.MoveFile strLine, strNewFilename
            End If
        Loop
        oTextStream.Close
Else
        MsgBox "Input file  strTCtempList  not found. Check button parameter and quoting."_
                &vbLF&vbLF & strTCtempList,,"TC-Script Error - ForEachSelFileDo"
End If


This is one possibility only.
Wow thanks Stefan. That is great. I see that this is someform of vbscript..is the script files limited to using vbscript? I am not familiar with VB unfortunately.

fliptoback
Junior Member
Junior Member
Posts: 11
Joined: 2019-05-15, 12:23 UTC

Re: How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

Post by *fliptoback » 2019-05-16, 00:05 UTC

petermad wrote:
2019-05-15, 16:45 UTC
You can also use a saved Multi-Rename Tool setting:
1. open the Multi-Rename Tool (Ctrl+M)
2. In the "Rename mask" field type [N]_Panasonic
3. Press F2 to save the settings - call it Panasonic.
4. Close the Multi-Rename Tool.
5. Make this button:

Code: Select all

TOTALCMD#BAR#DATA
MULTIRENAME Panasonic

wcmicons.dll,46
Multi Rename Tool - Panasonic


-1
To make the button:
1. Mark the green text above (click SELECT ALL).
2. Copy it to the ClipBoard (press Ctri+C).
3. Right click on TC's buttonbar and choose "Paste".
Now you can open the Multi-Rename Tool with the Panasonic setting with this button - then you only have to click the Start! button.
Now this is new for me and I have been using TC for like 15 years if not more. I have never realized that you can save the multi-rename settings! Cool bananas.

User avatar
Stefan2
Power Member
Power Member
Posts: 2588
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

Post by *Stefan2 » 2019-05-16, 07:28 UTC

fliptoback wrote:
2019-05-16, 00:04 UTC

...
..is the script files limited to using vbscript?

...

No.
TC just provides a temporary file with each selected items, one per line (Tip: Dir's have got a trailing backslash).
'For parameters see > Help > Operation > Menus > Menu: Configuration > Change Button Bar > Configuration - Change button bar.
' %L / %F : Create a list file in the TEMP directory with the names of the selected files and directories.


Now use a tool of your choice to parse the temp file line-by-line: VBScript, JavaScript, MS-DOS Batch, AutoHotkey, AutoIt, PascalScript, PowerPro, PowerShell, Python, Perl, Rexx....

The people on this forum mostly use Batch, VBS, AutHotkey and PowerPro.



HTH?
Inofficial FAQs || WIKI (Deu/Eng) || TC Home (What's new? // FAQ // Download // Order // Addons // Tools // Plugins)
Erst wenn der letzte Baum gefällt oder die letzte Biene verendet ist, werden die Leute verstehen warum Umweltschutz wichtig ist.

User avatar
petermad
Power Member
Power Member
Posts: 8470
Joined: 2003-02-05, 20:24 UTC
Location: Valsted, Denmark
Contact:

Re: How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

Post by *petermad » 2019-05-16, 10:42 UTC

2fliptoback
You can also incorporate A DOS script like this directly in the button:

Code: Select all

TOTALCMD#BAR#DATA
%COMSPEC% /C
if not exist "%P" (echo •) else if exist %Y%P%S1\ (echo•) else chcp 65001 && %%COMSPEC%% /C for /F "usebackq delims=" %%n in (`type %WL`) do attrib -H -S "%%n" && %%COMSPEC%% /C for /F "usebackq delims=" %%n in (`type %WL`) do if not exist "%%n\*" ren "%%n" "%%~nn_Panasonic%%~xn"
wciconex.dll,32
Add "_Panasonic" to selected filenames


-1
You will get a beep if you have not selected at least one file or if you are trying to run the script from a place where DOS does not work - like inside Archives, FTP sites or Virtual panels. It works with Unicode file names, and for hidden and system files - BUT the hidden/system file attributes will be removed.
License #524 (1994)
Danish Total Commander Translator
TC 9.22a 32+64bit on Win XP 32bit, Win 7, 8.1 & 10 (1903) 64bit, 'Everything' 1.4.1.935 (x64)
TC 3.0b6 on Android 6.0
Get: Extended Total Commander Menus | PHSM-Calendar

User avatar
Hacker
Moderator
Moderator
Posts: 11266
Joined: 2003-02-06, 14:56 UTC
Location: Bratislava, Slovakia

Re: How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

Post by *Hacker » 2019-05-16, 11:07 UTC

fliptoback,
A quick and dirty AHK solution:

Code: Select all

SetWorkingDir, %A_ScriptDir%
FileRead, FileList, % A_Args[1]
FileList := SubStr(FileList, 1, -2)
Loop, Parse, FileList, `n, `r
	FileMove, % A_LoopField, % InStr(A_LoopField, ".") ? SubStr(A_LoopField, 1, InStr(A_LoopField, ".", , 0) - 1) . A_Args[2] . SubStr(A_LoopField, InStr(A_LoopField, ".", , 0)) : A_LoopField . A_Args[2]
As parameters in the Button Bar use:

Code: Select all

"%L" _panasonic
HTH
Roman
Mal angenommen, du drückst Strg+F, wählst die FTP-Verbindung (mit gespeichertem Passwort), klickst aber nicht auf Verbinden, sondern fällst tot um.

fliptoback
Junior Member
Junior Member
Posts: 11
Joined: 2019-05-15, 12:23 UTC

Re: How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

Post by *fliptoback » 2019-05-16, 12:38 UTC

fliptoback wrote:
2019-05-16, 00:05 UTC
petermad wrote:
2019-05-15, 16:45 UTC
You can also use a saved Multi-Rename Tool setting:
1. open the Multi-Rename Tool (Ctrl+M)
2. In the "Rename mask" field type [N]_Panasonic
3. Press F2 to save the settings - call it Panasonic.
4. Close the Multi-Rename Tool.
5. Make this button:

Code: Select all

TOTALCMD#BAR#DATA
MULTIRENAME Panasonic

wcmicons.dll,46
Multi Rename Tool - Panasonic


-1
To make the button:
1. Mark the green text above (click SELECT ALL).
2. Copy it to the ClipBoard (press Ctri+C).
3. Right click on TC's buttonbar and choose "Paste".
Now you can open the Multi-Rename Tool with the Panasonic setting with this button - then you only have to click the Start! button.
Now this is new for me and I have been using TC for like 15 years if not more. I have never realized that you can save the multi-rename settings! Cool bananas.
Hi, I tried out this script. Works well. But is it possible to program this such that when i select the files, press the panasonic button and voila it will rename automatically without me pressing the "start!" button? At the moment it will pop up the Multi-Rename tool and i have to press the start! button manually. Thanks!

fliptoback
Junior Member
Junior Member
Posts: 11
Joined: 2019-05-15, 12:23 UTC

Re: How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

Post by *fliptoback » 2019-05-16, 12:40 UTC

Stefan2 wrote:
2019-05-16, 07:28 UTC
fliptoback wrote:
2019-05-16, 00:04 UTC

...
..is the script files limited to using vbscript?

...

No.
TC just provides a temporary file with each selected items, one per line (Tip: Dir's have got a trailing backslash).
'For parameters see > Help > Operation > Menus > Menu: Configuration > Change Button Bar > Configuration - Change button bar.
' %L / %F : Create a list file in the TEMP directory with the names of the selected files and directories.


Now use a tool of your choice to parse the temp file line-by-line: VBScript, JavaScript, MS-DOS Batch, AutoHotkey, AutoIt, PascalScript, PowerPro, PowerShell, Python, Perl, Rexx....

The people on this forum mostly use Batch, VBS, AutHotkey and PowerPro.



HTH?
Thanks for that info. I am only vaguely familiar with batch and autoIt but you mentioned PascalScript. I used to do a bit of pascal when i was at school so i will look into that one.

fliptoback
Junior Member
Junior Member
Posts: 11
Joined: 2019-05-15, 12:23 UTC

Re: How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

Post by *fliptoback » 2019-05-16, 12:48 UTC

petermad wrote:
2019-05-16, 10:42 UTC
2fliptoback
You can also incorporate A DOS script like this directly in the button:

Code: Select all

TOTALCMD#BAR#DATA
%COMSPEC% /C
if not exist "%P" (echo •) else if exist %Y%P%S1\ (echo•) else chcp 65001 && %%COMSPEC%% /C for /F "usebackq delims=" %%n in (`type %WL`) do attrib -H -S "%%n" && %%COMSPEC%% /C for /F "usebackq delims=" %%n in (`type %WL`) do if not exist "%%n\*" ren "%%n" "%%~nn_Panasonic%%~xn"
wciconex.dll,32
Add "_Panasonic" to selected filenames


-1
You will get a beep if you have not selected at least one file or if you are trying to run the script from a place where DOS does not work - like inside Archives, FTP sites or Virtual panels. It works with Unicode file names, and for hidden and system files - BUT the hidden/system file attributes will be removed.
Hmm..I could not get this to work. I copied the code, and right click at the menubar and press paste. it created the button but when i select the files and pressed it, it run the batch file but did not change any filenames.

fliptoback
Junior Member
Junior Member
Posts: 11
Joined: 2019-05-15, 12:23 UTC

Re: How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

Post by *fliptoback » 2019-05-16, 12:57 UTC

Hacker wrote:
2019-05-16, 11:07 UTC
fliptoback,
A quick and dirty AHK solution:

Code: Select all

SetWorkingDir, %A_ScriptDir%
FileRead, FileList, % A_Args[1]
FileList := SubStr(FileList, 1, -2)
Loop, Parse, FileList, `n, `r
	FileMove, % A_LoopField, % InStr(A_LoopField, ".") ? SubStr(A_LoopField, 1, InStr(A_LoopField, ".", , 0) - 1) . A_Args[2] . SubStr(A_LoopField, InStr(A_LoopField, ".", , 0)) : A_LoopField . A_Args[2]
As parameters in the Button Bar use:

Code: Select all

"%L" _panasonic
HTH
Roman
Thanks. I installed AHK and now copied/pasted the first part of the code into the script. But I can't seem to able to paste into the toolbar to create the button?

User avatar
Stefan2
Power Member
Power Member
Posts: 2588
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

Post by *Stefan2 » 2019-05-16, 13:05 UTC

fliptoback wrote:
2019-05-16, 12:57 UTC
...
But I can't seem to able to paste into the toolbar to create the button?
Only some small script (mostly batch and PoSh) can be used directly in an button,
for all others write the code to a plain text file and call that from the button:

Total Commander Button code:

Code: Select all

Command: "D:\rive\path to my\ForEachSelFileDo.ext"  (or portable: "%COMMANDER_PATH%\TOOLs\Scrpts\ForEachSelFileDo.ext")
Parameters: "%F" or "%L" or unicode forms
Start path: mostly always empty
Icon file: WCMICONS.DLL or the used interpreter.exe
Tooltip: Script to do smtg with each file from "%F"


And only code which starts with a " TOTALCMD#BAR#DATA "-line are directly button-code for to paste onto the buttonbar.

If you like, read on there >> viewtopic.php?f=3&t=52425&p=354622#p354622



 
Inofficial FAQs || WIKI (Deu/Eng) || TC Home (What's new? // FAQ // Download // Order // Addons // Tools // Plugins)
Erst wenn der letzte Baum gefällt oder die letzte Biene verendet ist, werden die Leute verstehen warum Umweltschutz wichtig ist.

User avatar
Hacker
Moderator
Moderator
Posts: 11266
Joined: 2003-02-06, 14:56 UTC
Location: Bratislava, Slovakia

Re: How to create a button to bulk rename all files in a folder and append a fixed sequence to the files

Post by *Hacker » 2019-05-16, 13:19 UTC

fliptoback,
I can't seem to able to paste into the toolbar to create the button?
Try e.g.:

Code: Select all

TOTALCMD#BAR#DATA
C:\Scripts\AppendFileName.ahk
"%L" _panasonic
C:\Program Files\AutoHotkey\AutoHotkey.exe,1
Append "_panasonic"


-1
HTH
Roman
Mal angenommen, du drückst Strg+F, wählst die FTP-Verbindung (mit gespeichertem Passwort), klickst aber nicht auf Verbinden, sondern fällst tot um.

Post Reply