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

English support forum

Moderators: Stefan2, white, sheep, Hacker

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 » 2019-03-24, 03:41 UTC

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: 37264
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) » 2019-03-24, 07:47 UTC

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
http://www.ghisler.com

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

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

Post by *petermad » 2019-03-24, 12:48 UTC

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 9.22a 32+64bit on Win XP 32bit, Win 7, 8.1 & 10 64bit, 'Everything' 1.4.1.935 (x64) and TC 3.0b3 on Android 6.0
Get: Extended Total Commander Menus | PHSM-Calendar

User avatar
Dalai
Power Member
Power Member
Posts: 6534
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 » 2019-03-24, 14:58 UTC

2petermad

Code: Select all

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

Regards
Dalai
#101164 Personal licence
Athlon X4 880K, 16 GiB RAM, Gigabyte F2A88X-D3HP, Win7 x64

Plugins: Services2, Startups

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

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

Post by *petermad » 2019-03-24, 15:06 UTC

2Dalai
"%~nx0.done" should do the same.
You are right of course - Thanks :-)
License #524 (1994)
Danish Total Commander Translator
TC 9.22a 32+64bit on Win XP 32bit, Win 7, 8.1 & 10 64bit, 'Everything' 1.4.1.935 (x64) and TC 3.0b3 on Android 6.0
Get: Extended Total Commander Menus | 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 » 2019-03-24, 23:30 UTC

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