Swap the name of two files. Any method?

English support forum

Moderators: Hacker, petermad, Stefan2, white

User avatar
sidefx88
Senior Member
Senior Member
Posts: 294
Joined: 2007-04-11, 17:07 UTC
Location: Argentina

Swap the name of two files. Any method?

Post by *sidefx88 »

Hi!

There's a situation that happens to me often. I rename two files, but if I'm not very focused, sometimes I name the files switched, for example:

Fasc17_P01-03_03.jpg
Fasc17_P01-03_04.jpg


This names are swithed by mistake, so I have to rename them again.

Fasc17_P01-03_03.jpg
Fasc17_P01-03_04a.jpg


Then...

Fasc17_P01-03_04.jpg
Fasc17_P01-03_04a.jpg


And then...

Fasc17_P01-03_04.jpg
Fasc17_P01-03_03.jpg


Is there a way (command, chain of commands, CMD/BAT script) to just select this two files and press a button (or even better: a hotkey) to do the filename swap automatically?

Thanks in advance!
I Love Total Commander!
User avatar
Ovg
Power Member
Power Member
Posts: 755
Joined: 2014-01-06, 16:26 UTC

Post by *Ovg »

Something like:
swap_names.cmd

Code: Select all

@echo off
ren %1 _$$$qqq.qqq$$_
ren %2 %1
ren _$$$qqq.qqq$$_ %2
button:

Code: Select all

TOTALCMD#BAR#DATA
swap_names.cmd
%S
WCMICONS.DLL,80
swap_names

1
-1
Change paths and names to your taste
It's impossible to lead us astray for we don't care even to choose the way.
#259941, TC 11.01 x64, Windows 7 SP1 x64
User avatar
petermad
Power Member
Power Member
Posts: 16101
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Post by *petermad »

2sidefx88
You can make a userdefined command like this:

Code: Select all

[em_swapfilenames]
button=wcmicons.dll,0
cmd=%COMSPEC% /C
menu=Swap Two Filenames
param=if "%P%S"=="%C1 %C2" ((move %C1 %Ptempfile) && (move %C2 %C1) && (move %Ptempfile %C2)) else ((echo MARK TWO FILES!) && (echo.) && pause)
Copy the code above to your usercmd.ini file

You can then make a userdefined hotkey for the em_swapfilenames command.

And / or you can make a button with this content:

Code: Select all

TOTALCMD#BAR#DATA
em_swapfilenames

wcmicons.dll
Swap Two Filenames

0
10365
Copy the code above and right click on the button bar to paste the button.


If you ONLY want a button you can use this:

Code: Select all

TOTALCMD#BAR#DATA
%COMSPEC% /C
if "%P%S"=="%C1 %C2" ((move %C1 %Ptempfile) && (move %C2 %C1) && (move %Ptempfile %C2)) else ((echo MARK TWO FILES!) && (echo.) && pause)
wcmicons.dll,0
Swap Two Filenames


-1
Copy the code above and right click on the button bar to paste the button.

This approach ensures that two and only two files are selected before running the commands and gives you a message if not.
License #524 (1994)
Danish Total Commander Translator
TC 11.55rc4 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1393a
TC 3.60b4 on Android 6, 13, 14
TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
solid
Power Member
Power Member
Posts: 755
Joined: 2004-08-09, 11:20 UTC

Post by *solid »

Until I've read this topic, I wasn't aware that actually I too often need swapping of filenames.
Thanks to OP for opening it, and thanks to @petermad for such elegant solution.
User avatar
nsp
Power Member
Power Member
Posts: 1939
Joined: 2005-12-04, 08:39 UTC
Location: Lyon (FRANCE)
Contact:

Post by *nsp »

petermad wrote:2sidefx88
You can make a userdefined command like this:

Code: Select all

[em_swapfilenames]
button=wcmicons.dll,0
cmd=%COMSPEC% /C
menu=Swap Two Filenames
param=if "%P%S"=="%C1 %C2" ((move %C1 %Ptempfile) && (move %C2 %C1) && (move %Ptempfile %C2)) else ((echo MARK TWO FILES!) && (echo.) && pause)
Copy the code above to your usercmd.ini file

You can then make a userdefined hotkey for the em_swapfilenames command.

And / or you can make a button with this content:

Code: Select all

TOTALCMD#BAR#DATA
em_swapfilenames

wcmicons.dll
Swap Two Filenames

0
10365
Copy the code above and right click on the button bar to paste the button.


If you ONLY want a button you can use this:

Code: Select all

TOTALCMD#BAR#DATA
%COMSPEC% /C
if "%P%S"=="%C1 %C2" ((move %C1 %Ptempfile) && (move %C2 %C1) && (move %Ptempfile %C2)) else ((echo MARK TWO FILES!) && (echo.) && pause)
wcmicons.dll,0
Swap Two Filenames


-1
Copy the code above and right click on the button bar to paste the button.

This approach ensures that two and only two files are selected before running the commands and gives you a message if not.
This is a very elegant solution Peter, to be safer, you could use %C1.tempfile istead of %PTempfile just to prevent collision or have a easiest way to go back if the second move fail..
User avatar
petermad
Power Member
Power Member
Posts: 16101
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Post by *petermad »

you could use %C1.tempfile istead of %PTempfile
Good idea!

Here is the updated code:

Code: Select all

TOTALCMD#BAR#DATA
%COMSPEC% /C
if "%P%S"=="%C1 %C2" ((move %C1 %C1.tempfile) && (move %C2 %C1) && (move %C1.tempfile %C2)) else ((echo MARK TWO FILES!) && (echo.) && pause)
wcmicons.dll
Swap Two Filenames


-1

Code: Select all

[em_swapfilenames]
button=wcmicons.dll
cmd=%COMSPEC% /C
menu=Swap Two Filenames
param=if "%P%S"=="%C1 %C2" ((move %C1 %C1.tempfile) && (move %C2 %C1) && (move %C1.tempfile %C2)) else ((echo MARK TWO FILES!) && (echo.) && pause)
License #524 (1994)
Danish Total Commander Translator
TC 11.55rc4 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1393a
TC 3.60b4 on Android 6, 13, 14
TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
sidefx88
Senior Member
Senior Member
Posts: 294
Joined: 2007-04-11, 17:07 UTC
Location: Argentina

Post by *sidefx88 »

Hi guys! Thanks for answering, but it doesn't work here.

Maybe I'm doing it in the wrong way...

I decided to use a hotkey only (no button on bar needed).

I assign Ctrl+Shift+R to command 'em_swapfilenames'.

And I defined the command on usercmd.ini:

cmd=%COMSPEC% /C
param=if "%P%S"=="%C1 %C2" ((move %C1 %C1.tempfile) && (move %C2 %C1) && (move %C1.tempfile %C2)) else ((echo MARK TWO FILES!) && (echo.) && pause)

When I select two files (same panel) and then press Ctrl+Shift+R a console window pops up, showing "MARK TWO FILES!" message, and below: "Press any key to continue..."

And the file(name)s stay the same.

Any idea on what's wrong?
Last edited by sidefx88 on 2016-07-26, 15:55 UTC, edited 1 time in total.
I Love Total Commander!
User avatar
petermad
Power Member
Power Member
Posts: 16101
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Post by *petermad »

Are you sure the line:

Code: Select all

if "%P%S"=="%C1 %C2" ((move %C1 %C1.tempfile) && (move %C2 %C1) && (move %C1.tempfile %C2)) else ((echo MARK TWO FILES!) && (echo.) && pause) 
is in one line...
License #524 (1994)
Danish Total Commander Translator
TC 11.55rc4 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1393a
TC 3.60b4 on Android 6, 13, 14
TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
sidefx88
Senior Member
Senior Member
Posts: 294
Joined: 2007-04-11, 17:07 UTC
Location: Argentina

Post by *sidefx88 »

Yes, it is!

:(
I Love Total Commander!
Henrie
Member
Member
Posts: 194
Joined: 2006-09-03, 23:12 UTC
Location: Volkel, the Netherlands

Post by *Henrie »

For me it did not work either.
Turns out it is because my 2 files were on my NAS with a UNC path. On my local harddrive it does work.
User avatar
sidefx88
Senior Member
Senior Member
Posts: 294
Joined: 2007-04-11, 17:07 UTC
Location: Argentina

Post by *sidefx88 »

My test folder is on my local C: drive :(
I Love Total Commander!
User avatar
Dalai
Power Member
Power Member
Posts: 10018
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Post by *Dalai »

I guess, the file names (%C1 and %C2 variables) must be quoted to make the command work for paths with spaces.

Regards
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Post by *Stefan2 »

For those where it doesn't work:

%C1 and %C2 needs minimum TC9.0 beta 1 or newer.
TC9 beta6 >> http://ghisler.ch/board/viewtopic.php?t=44960

08.06.16 Release Total Commander 9.0 beta 1 (32/64)
19.01.16 Added: Button bar, start menu parameters %C3..%C9: Selected files Nr. 3..9, empty if not as many selected (32/64)
19.01.16 Added: Button bar, start menu parameters %c1, %c2: Same as %C1, %C2, but with alternate 8.3 paths and names (DOS names) (32/64)
19.01.16 Added: Button bar, start menu parameters %C1, %C2: Compare parameters as in "Compare by content": First two selected files (with full path), or selected in source+target panel, or source file+same name in target panel (32/64)



 
User avatar
sidefx88
Senior Member
Senior Member
Posts: 294
Joined: 2007-04-11, 17:07 UTC
Location: Argentina

Post by *sidefx88 »

Maybe Stefan2 got the solution!

I was trying on TC 8.52a

I will try on TC 9b6 :)

Thanks!
I Love Total Commander!
User avatar
sidefx88
Senior Member
Senior Member
Posts: 294
Joined: 2007-04-11, 17:07 UTC
Location: Argentina

Post by *sidefx88 »

2Stefan2:

It works like silk on TC9! :)
I Love Total Commander!
Post Reply