With EM command, percent symbols in Command are processed like Parameters

Bug reports will be moved here when the described bug has been fixed

Moderators: Hacker, petermad, Stefan2, white

User avatar
Flint
Power Member
Power Member
Posts: 3511
Joined: 2003-10-27, 09:25 UTC
Location: Belgrade, Serbia
Contact:

With EM command, percent symbols in Command are processed like Parameters

Post by *Flint »

As we know, when you create a button, there is a distinction in how percent sign is processed in the Command and Parameters fields.
Command: Environment variables (if any) are expanded by TC, otherwise no special processing is applied.
Parameters: Percent sign is a special symbol that encodes lots of custom template values (%P, %N, etc.); to specify the percent itself it has to be doubled; if an environment variable is passed (like %%USERNAME%%), it is not expanded but passed to the program's argument as is (a literal text "%USERNAME%").

However, if you put a EM-command into the Command editbox, this all changes. All the extra parameters you append in that box will be treated by the "Parameters rules", instead of the "Command rules".

Case for reproduction.
1. Create a simple EM command, for example, like this:

Code: Select all

[em_notepad]
button=notepad.exe
cmd=notepad.exe
2. Add two buttons to the buttonbar:

Code: Select all

TOTALCMD#BAR#DATA
notepad.exe %TEMP%

notepad.exe



-1

Code: Select all

TOTALCMD#BAR#DATA
em_notepad %TEMP%

notepad.exe



-1
3. Press the first of these two buttons. Notepad will start and complain that it has no permission to open the file C:\Users\USERNAME\AppData\Local\Temp (which is obviously not a file, so that's OK). In the task manager (with Command Line column enabled) you can see that the full command line being, as expected:

Code: Select all

"C:\Windows\System32\notepad.exe" C:\Users\USERNAME\AppData\Local\Temp
4. Now press the second button. Notepad will start and complain that it cannot find the file c:\Program Files\TotalCmd\EMP.txt. The full command line being:

Code: Select all

"C:\Windows\System32\notepad.exe" c:\Program Files\TotalCmd\EMP
In my case, c:\Program Files\TotalCmd\ just happened to be the path opened in the inactive TC panel, so obviously %T was substituted with this target panel path, and the "EMP" part just remained as is (and the dangling percent discarded). In other words, the %TEMP% parameter was treated like it was in Parameters instead of Command. I checked a few other %letter templates, and they all worked, including %%TEMP%% which was converted into literal text %TEMP% and passed to notepad (which promptly asked me if I want to create a new file named %TEMP%.txt).

Note:
This only happens if the Parameters field is empty when you specify a EM command in the Command editbox. If Parameters is not empty, then percent signs in both fields are processed correctly, just as described in the TC help (and as summarized at the top of my post).

Tested in Windows 10, TC 11.03 32- and 64-bit.
Flint's Homepage: Full TC Russification Package, VirtualDisk, NTFS Links, NoClose Replacer, and other stuff!
 
Using TC 11.03 / Win10 x64
User avatar
white
Power Member
Power Member
Posts: 5964
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: With EM command, percent symbols in Command are processed like Parameters

Post by *white »

Confirmed.
Perhaps this is easier for testing. (click here)

em_command: (declaration of user-defined command em_showfolder in usercmd.ini, this command is used in button 2 and 3)
  • Code: Select all

    [em_showfolder]
    button=cmd.exe
    cmd="cmd.exe /k echo Folder: "
    

Button 1: (shows normal expansion of environment variables)
  • Code: Select all

    TOTALCMD#BAR#DATA
    cmd /k echo Folder: %TEMP%
    
    cmd.exe
    
    
    
    -1
    

Button 2: (shows expansion of parameter variables in the command field)
  • Code: Select all

    TOTALCMD#BAR#DATA
    em_showfolder %TEMP%
    
    CMD.EXE
    
    
    
    -1
    

Button 3: (shows normal expansion of environment variables when using em_ command and parameters field is not empty)
  • Code: Select all

    TOTALCMD#BAR#DATA
    em_showfolder %TEMP%
    ?
    CMD.EXE
    
    
    
    -1
    
Parameter support for em_commands in buttons was added in TC 11.00b3. In that version, the Parameters field still stayed disabled when the command field had an em_ command (this changed in the next beta). Perhaps that has something to do with it.
User avatar
petermad
Power Member
Power Member
Posts: 16098
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: With EM command, percent symbols in Command are processed like Parameters

Post by *petermad »

You must use %|TEMP| instead %TEMP% with em_commands.

If you make a test.txt file in %TEMP% then all these buttons will open the file in Notepad:

Code: Select all

TOTALCMD#BAR#DATA
notepad.exe %TEMP%\test.txt

notepad.exe

Code: Select all

TOTALCMD#BAR#DATA
em_notepad %|TEMP|\test.txt

notepad.exe

Code: Select all

TOTALCMD#BAR#DATA
em_notepad 
%|TEMP|\test.txt
notepad.exe

But it is peculiar and perhaps a bug, because with cm_commands you have to use %TEMP%:

This does NOT work

Code: Select all

TOTALCMD#BAR#DATA
cm_List %|TEMP|\test.txt

wcmicons.dll,27
But these works:

Code: Select all

TOTALCMD#BAR#DATA
cm_List %TEMP%\test.txt

wcmicons.dll,27

Code: Select all

TOTALCMD#BAR#DATA
cm_List
%|TEMP|\test.txt
wcmicons.dll,27
The best would be if both %TEMP% and %|TEMP| worked with both cm_ and em_ commands when used in the Command field.

It is also not consistent that cm_List %TEMP%\test.txt works in the Command of a button, but in the Command line you have to use cm_List %|TEMP|\test.txt
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
Flint
Power Member
Power Member
Posts: 3511
Joined: 2003-10-27, 09:25 UTC
Location: Belgrade, Serbia
Contact:

Re: With EM command, percent symbols in Command are processed like Parameters

Post by *Flint »

petermad wrote: 2024-07-21, 12:04 UTC You must use %|TEMP| instead %TEMP% with em_commands.
Which exactly corresponds to what I described. %|TEMP| is the syntax for the Parameters field, not for Command.

You see, %TEMP% was just one single example, but the issue is not limited to environment variables. Any usage of the percent sign turns into Parameters-like template processing, even though it's a part of the Command string. I can give another example, this time without any environment variables: if I want to run something like

Code: Select all

em_yt_dlp -o "%(title)s-%(id)s" ...
it won't work, the %( sequences will be cut out completely (as invalid from the point of view of Parameters parsing). But if I use yt_dlp.exe directly, without EM wrapper, the same command line will work smoothly.
Flint's Homepage: Full TC Russification Package, VirtualDisk, NTFS Links, NoClose Replacer, and other stuff!
 
Using TC 11.03 / Win10 x64
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 50817
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: With EM command, percent symbols in Command are processed like Parameters

Post by *ghisler(Author) »

It's complicated: When you pass parameters to an em_command on a button, they replace the "parameters" field of the command stored in that em_command. Example:
Command: em_notepad a
Parameters: b
->
Command: notepad.exe
Parameters: a b

Since it's in the parameters field, it uses placeholders like %P or %N.
However, it makes a difference now whether the parameters field of the button is empty or not: When it's empty, TC switches to multi-command mode, e.g. you can have
em_notepad a,cm_focusleft
If there is a parameter, TC switches to single command mode because you can't say for which command the parameter is meant.

In single command mode, TC replaces environment variables in the command field to their values before continuing. In the simpler multi-command mode it doesn't do that because it only expects cm_commands and em_commands (which did not support parameters until recently). It's probably better to handle environment variables there too.
Author of Total Commander
https://www.ghisler.com
User avatar
Flint
Power Member
Power Member
Posts: 3511
Joined: 2003-10-27, 09:25 UTC
Location: Belgrade, Serbia
Contact:

Re: With EM command, percent symbols in Command are processed like Parameters

Post by *Flint »

Thank you for the explanation!

I would suggest then, that it should be added to the help. Because when I suddenly discovered it, it was really confusing. :)
Flint's Homepage: Full TC Russification Package, VirtualDisk, NTFS Links, NoClose Replacer, and other stuff!
 
Using TC 11.03 / Win10 x64
User avatar
petermad
Power Member
Power Member
Posts: 16098
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: With EM command, percent symbols in Command are processed like Parameters

Post by *petermad »

2ghisler(Author)
It still doesn't explain why only em_notepad %|TEMP| and only cm_List %TEMP% works in the Command field (with blank Parameters field) - as you explain it, it should work the same way for both em_ and cm_ commands.

It's probably better to handle environment variables there too.
Yes, that's what I suggested here above.
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
ghisler(Author)
Site Admin
Site Admin
Posts: 50817
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: With EM command, percent symbols in Command are processed like Parameters

Post by *ghisler(Author) »

It still doesn't explain why only em_notepad %|TEMP| and only cm_List %TEMP% works in the Command field
That's because em_notepad isn't the command where the replacement of environment variables happens!
Command: em_notepad %|TEMP|
is resolved to e.g.
Command: notepad.exe
Parameters: %|TEMP|
and then the replacement of variables occurs in the parameters field with the rules of that field.

When clicking on a button with
Command: cm_List %TEMP%
the command is the final command to be executed, so the variable replacement happens in the command field with the rules of the command field.

I will change the help (Configuration - Change button bar) as follows:
Additionally, you can define your own commands with prefix em_ via "Choose command" and then use them here.
Commands with prefix em_ handle parameters in a special way: When not using the "Parameters" field, the parameters directly behind the command name are passed unchanged to the inner command and then parsed, e.g. em_notepad %P%N (defined as notepad.exe) will be handled as Command: notepad.exe, Parameters: %P%N. When using the "Parameters" field, environment variables behind the command name will be handled before passing them together with the parameters field to the inner command.
Author of Total Commander
https://www.ghisler.com
User avatar
white
Power Member
Power Member
Posts: 5964
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: With EM command, percent symbols in Command are processed like Parameters

Post by *white »

ghisler(Author) wrote: 2024-07-22, 07:41 UTC I will change the help (Configuration - Change button bar) as follows:
Additionally, you can define your own commands with prefix em_ via "Choose command" and then use them here.
Commands with prefix em_ handle parameters in a special way: When not using the "Parameters" field, the parameters directly behind the command name are passed unchanged to the inner command and then parsed, e.g. em_notepad %P%N (defined as notepad.exe) will be handled as Command: notepad.exe, Parameters: %P%N. When using the "Parameters" field, environment variables behind the command name will be handled before passing them together with the parameters field to the inner command.
How about saying it like this:
Additionally, you can define your own commands with prefix em_ via "Choose command" and then use them here.
When em_ commands are used in the "Command" field, specified parameters for an em_ command are passed to the em_ command and replace the contents of the "Parameters" field within the em_ command. For instance, using em_notepad %P%N in the "Command" field (where em_notepad represents notepad.exe) will be interpreted as:
* Command: notepad.exe
* Parameters: %P%N
The substitution of environment variables in em_ command parameters specified in the "Command" field depends on whether the "Parameters" field is empty or not.
- If the "Parameters" field is empty, environment variables will remain unresolved, requiring the use of %|var| instead. These variables will be resolved later as part of the "Parameters" field in the custom command.
- If the "Parameters" field contains content, environment variables will be resolved.
User avatar
petermad
Power Member
Power Member
Posts: 16098
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: With EM command, percent symbols in Command are processed like Parameters

Post by *petermad »

Support for white's proposal.
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
ghisler(Author)
Site Admin
Site Admin
Posts: 50817
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: With EM command, percent symbols in Command are processed like Parameters

Post by *ghisler(Author) »

Too long, have a look at the page where this would be added. :(
Author of Total Commander
https://www.ghisler.com
User avatar
white
Power Member
Power Member
Posts: 5964
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: With EM command, percent symbols in Command are processed like Parameters

Post by *white »

ghisler(Author) wrote: 2024-07-24, 09:16 UTC Too long, have a look at the page where this would be added. :(
Create a separate point 3 for user-defined commands, and renumber the rest of the points?
Or is the problem the overall size of the page?
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 50817
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: With EM command, percent symbols in Command are processed like Parameters

Post by *ghisler(Author) »

Create a separate point 3 for user-defined commands, and renumber the rest of the points?
That would be a possible solution.
Or is the problem the overall size of the page?
No, the whole section is just overloaded with information, too long for most people to read.
Author of Total Commander
https://www.ghisler.com
User avatar
white
Power Member
Power Member
Posts: 5964
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: With EM command, percent symbols in Command are processed like Parameters

Post by *white »

ghisler(Author) wrote: 2024-07-24, 10:02 UTC No, the whole section is just overloaded with information, too long for most people to read.
Move the info about the Command field and Parameters field to separate pages. That way you can suffice with a very short explanation and the users can click further for details. And other pages could refer to it as well, like Help - Dialog box: Change start menu.

Or move the explanations of the Command and Parameters fields to the bottom of the page.
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 50817
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: With EM command, percent symbols in Command are processed like Parameters

Post by *ghisler(Author) »

This should be fixed in Total Commander 11.50 beta 1, please check it!
22.07.24 Fixed: Button bar, command em_notepad containing command notepad.exe used on button with parameter %P%N, cursor on file named test%N.txt -> the %N in the file name was resolved by mistake (32/64)
Author of Total Commander
https://www.ghisler.com
Post Reply