Swap the name of two files. Any method?

English support forum

Moderators: white, Hacker, petermad, Stefan2

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

Post by *petermad »

Well it actually doesn't work if there are spaces in the path or filename:

This shold do it:

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
Note that the lowercase variables must not be changed in order to use DOS names without spaces, since DOS' string comparison cannot handle spaces.
Last edited by petermad on 2016-07-26, 22:26 UTC, edited 1 time in total.
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
Dalai
Power Member
Power Member
Posts: 9387
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Post by *Dalai »

petermad wrote:[...] since DOS' string comparison cannot handle spaces.
Well, of course it can. That's why it's best to always quote variables when using them, you're even using a space yourself by writing "%c1 %c2". The issue is the quotes in the comparison. They are not always there but only when there are spaces in the path.

Note that CMD expects the whole command to be enclosed in quotes when /C or /K is given as parameter. It gets tricky when quotes are used in the command itself.

Short file names are not always available either, since they can be disabled on NTFS. In the end it's better and more reliable to write the code into a batch file and call that from TC. This allows to remove any quotes TC gives over to CMD. Something like this:

Code: Select all

@echo off

if "%~2"=="" goto ERR
if NOT "%~3"=="" goto ERR

move "%~1" "%~1.tempfile" && move "%~2" "%~1" && move "%~1.tempfile" "%~2"
pause
goto :EOF
    
:ERR
echo MARK TWO FILES!
echo.
pause
called by this button

Code: Select all

TOTALCMD#BAR#DATA
%COMMANDER_PATH%\scripts\swapnames.cmd
?%P%S
wcmicons.dll
Swap Two Filenames


-1
Technically you'd need add a check whether the parameters are directories instead of files... It's not so easy with this batch stuff; I guess it would be simpler in PowerShell.

Regards
Dalai
Last edited by Dalai on 2016-07-26, 23:08 UTC, edited 1 time in total.
#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
sidefx88
Senior Member
Senior Member
Posts: 294
Joined: 2007-04-11, 17:07 UTC
Location: Argentina

Post by *sidefx88 »

2petermad / 2Dalai:

I was initially testing on a folder with no spaces on its name, and using two files inside with no spaces on their names... weird.

But I will take your new improved code because I just tried using filenames with spaces and didn't work.

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 »

It works now on filenames with spaces, using the new petermad code:

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) 
(I removed the first "?" because I get an error. Is it right?)

BUT, it is really weird: it only works on 1 of 3 swapping tries!

No error on failed tries , just do nothing.

I'm wondering if it is a way to stop or pause the script at the end of the process to see the console (it closes suddenly at the end, and I cannot see any message or error).

Thanks again! This script is getting better every time :)
I Love Total Commander!
User avatar
petermad
Power Member
Power Member
Posts: 14796
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Post by *petermad »

2Dalai

My goal is to do it without an external .cmd/.bat file (which might get lost) - and I cannot use the ~ trick ("%~1") to remove the inner qutation marks with TC's %C1, %C2 parameters. So there is no way I can remove the inner quotes that is set by TC when there is a space in the path, so I will end up with double quotes like:

Code: Select all

If ""c:\dir name\file name.txt" "c:\dir name\file name 2.txt""==""c:\dir name\file name.txt" "c:\dir name\file name 2.txt""
and THAT is what DOS's IF cannot handle - nested quotations - so that is why i use %c1 and %c2 in stead.
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
petermad
Power Member
Power Member
Posts: 14796
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Post by *petermad »

2sidefx88
I removed the first "?" because I get an error. Is it right?)
It is not an error it is just a way to have the command line displayed before it is executed. I used it when testing and forgot to remove it before I copied it.

I have now edited it away in the previous post.
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
Dalai
Power Member
Power Member
Posts: 9387
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Post by *Dalai »

2petermad
Yes, I know why you're trying to do it that way. The only reason is that the script could get lost - which can easily prevented by making a backup of it. I just wanted to point out that it's VERY limited to do it with just a button. I find it confusing to have such long commands on a button, so I try to make it clearer and cleaner for me.

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
petermad
Power Member
Power Member
Posts: 14796
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Post by *petermad »

2Dalai
if "%~1"=="" goto ERR
if NOT "%~3"=="" goto ERR
It will not work since %C1 and %C2 are never ="" hence "%~1" is never =""
If nothing is selected %C1 gives the filename under the cursor but with the path of the left pane and %C2 gives the filename under the cursor and path of the right panel.

If a file is selected in the opposite panel, then that filename is passed as the filename for %C1 or %C2 depending on which panel you are in.

So: if "%~1"=="" will never trigger an error, and if no files are selected in TC your script will swap the file under the cursor with a file with the same name in the opposite panel if such file exists - not really what you want.
Short file names are not always available either, since they can be disabled on NTFS.
I think that extremely few users has done that...


This might work:

Code: Select all

TOTALCMD#BAR#DATA
%COMMANDER_PATH%\scripts\swapnames.cmd
%P%S %C1 %C2
wcmicons.dll
Swap Two Filenames


-1 

Code: Select all

@echo off

if not "%~1 %~2"=="%~3 %~4" goto ERR

move "%~3" "%~3.tempfile" && move "%~4" "%~3" && move "%~3.tempfile" "%~4"

goto :EOF
   
:ERR
echo MARK TWO FILES!
echo.
pause 
Last edited by petermad on 2016-07-26, 23:18 UTC, edited 2 times in total.
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
Dalai
Power Member
Power Member
Posts: 9387
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Post by *Dalai »

It will not work since %C1 and %C2 are never ="" hence "%~1" is never =""
Well, the script may be used independently from TC, so it's always a good thing to prevent errors from happening.
So: if "%~1"=="" will never trigger an error, and if no files are selected in TC your script will swap the file under the cursor with a file with the same name in the opposite panel if such file exists - not really what you want.
Yep, you're right about that. It seems it's better to just use %P%S, and then even the check for the number of parameters makes sense again. And there's a little error in the first check. I'll update my post above.

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
petermad
Power Member
Power Member
Posts: 14796
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Post by *petermad »

It seems it's better to just use %P%S
Note that %P%S is also never = "" If nothing is selected it returns the name of the file under the cursor...
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
Dalai
Power Member
Power Member
Posts: 9387
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Post by *Dalai »

2petermad
Yes, but that's not a problem because in this case only one parameter is passed to the script so "%~2" will be empty, resulting in the error message. I tested the script with zero through three selected files, in paths with and without spaces and it works on my system.

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
petermad
Power Member
Power Member
Posts: 14796
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Post by *petermad »

Technically you'd need add a check whether the parameters are directories instead of file
I have now added check for folder selection:

Code: Select all

TOTALCMD#BAR#DATA
%COMSPEC% /C
if not "%p%s"=="%c1 %c2" ((echo MARK TWO FILES!) && (echo.) && pause) else if exist %c1\* ((echo DO NOT MARK DIRECTORIES!) && (echo.) && pause) else (move %C1 %c1.tempfile && move %C2 %C1 && move %c1.tempfile %C2)
wcmicons.dll
Swap Two Filenames


-1
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
petermad
Power Member
Power Member
Posts: 14796
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Post by *petermad »

Quote:
Short file names are not always available either, since they can be disabled on NTFS.

I think that extremely few users has done that...
I have been developing this button on a Windows 7 computer. After checking on my Windows 8.1 and Windows 10 machines, I see that short filenames (8dot3name) on NTFS volumes its by default only enabled for the system volume (drive C:).

So caution:
My button/em_command will most likely not work on NFTS partitions other than drive C: on Windows 8.1 and Windows 10, when there is a space in the path/filename
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
XPEHOPE3KA
Power Member
Power Member
Posts: 854
Joined: 2006-03-03, 18:23 UTC
Location: Saint-Petersburg, Russia

Post by *XPEHOPE3KA »

Thank all of you guys for these solutions! I need the feature quite often.
F6, Enter, Tab, F6, Enter, Tab, F6, Enter, Tab... - I like to move IT, move IT!..
User avatar
sidefx88
Senior Member
Senior Member
Posts: 294
Joined: 2007-04-11, 17:07 UTC
Location: Argentina

Post by *sidefx88 »

Thanks!
I Love Total Commander!
Post Reply