Calling a Windows batch file via double click or enter behaves different than calling it via cmd /c ?!

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
jo2h2
Junior Member
Junior Member
Posts: 10
Joined: 2004-11-17, 12:23 UTC

Calling a Windows batch file via double click or enter behaves different than calling it via cmd /c ?!

Post by *jo2h2 »

Hi,

this might have been asked and answered already, but I could not find an answer.

When calling a Windows batch file - e.g. "ren_myself.cmd" via double click or enter, it behaves different than calling the identical file via cmd /c ren_myself.cmd from the Total Commander command line.

This "ren_myself.cmd" is only a test for a bigger file, and here simply doing

"ren_myself.cmd":
-----------------------
ren "%~f0" "%~0.done"

It shall rename itself after successful processing.

As said, running "ren_myself.cmd" from a command window or from Total Commander prompt via "cmd /c ren_myself.cmd" - it does what is expected, it renames "ren_myself.cmd" to "ren_myself.cmd.done" - but with double click or enter - not.

So
1. why do the two methods behave different ? and
2. how can I call "ren_myself.cmd" via double click / enter - to behave like called via cmd /c ?
Can I configure anything in Total Commander to force that ?

Thanks a lot !

Best wishes,
Jo ...
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 48021
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: Calling a Windows batch file via double click or enter behaves different than calling it via cmd /c ?!

Post by *ghisler(Author) »

TC just calls ShellExecuteExW with the file, the rest is done by Windows. It depends on the assiciation.
If you look with regedit.exe, the association is here:
Computer\HKEY_CLASSES_ROOT\cmdfile\shell\open\command
The command is defined as follows:
"%1" %*

Maybe it helps when you change that to:
cmd.exe /c "%1" %*
Author of Total Commander
https://www.ghisler.com
User avatar
petermad
Power Member
Power Member
Posts: 14741
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: Calling a Windows batch file via double click or enter behaves different than calling it via cmd /c ?!

Post by *petermad »

2jo2h2
It is because of the syntax for the REN command:

Code: Select all

REN [drive:][path]filename1 filename2
It is not allowed to put full path before filename2 - if you do, the rename fails.

When you press Enter or doubleclick on ren_myself.cmd in TC, TC sends drive:\path\ren_myself.cmd to be executed, hence drive:\path\ren_myself.cmd is passed as %0.

If you try: cmd /c drive:\path\ren_myself.cmd it does NOT work either.

Change your batch command to:

Code: Select all

ren "%~f0" "%~n0%~x0.done"
then only the filename is passed as the second parameter and then it will also work when doubleclicking ren_myself.cmd in TC
Last edited by petermad on 2019-03-24, 23:37 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.50b4 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
Dalai
Power Member
Power Member
Posts: 9364
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Re: Calling a Windows batch file via double click or enter behaves different than calling it via cmd /c ?!

Post by *Dalai »

2petermad

Code: Select all

ren "%~f0" "%~nx0.done"
should do the same.

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: 14741
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: Calling a Windows batch file via double click or enter behaves different than calling it via cmd /c ?!

Post by *petermad »

2Dalai
"%~nx0.done" should do the same.
You are right of course - Thanks :-)
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.50b4 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
jo2h2
Junior Member
Junior Member
Posts: 10
Joined: 2004-11-17, 12:23 UTC

Re: Calling a Windows batch file via double click or enter behaves different than calling it via cmd /c ?!

Post by *jo2h2 »

You all are great - thanks for your super fast and very help replies !

The corrected ren command works perfectly !

Again, thank you so much !!
Post Reply