[b9] %P or "%P" - How should I handle path with spaces?

The behaviour described in the bug report is either by design, or would be far too complex/time-consuming to be changed

Moderators: white, sheep, Hacker, Stefan2

Post Reply
jiangzhenjerry
Junior Member
Junior Member
Posts: 38
Joined: 2016-01-15, 23:57 UTC

[b9] %P or "%P" - How should I handle path with spaces?

Post by *jiangzhenjerry »

I'm constructing custom buttonbar items to be my personal liking, and somehow the behavior of %P is inconsistent when applied to different external applications. I'm not sure if it is a duplicate of petermad's earlier report. If so, excuse me and feel free to merge/delete my post...

Note: Those who want to try out the executables in the examples below, feel free to follow the link to download from their official websites (and may need to install), or download from my GoogleDrive link (64-bit only, extracted from my installation folder, so no need to install and no virus - GoogleDrive's cloud-based virus scan will prove it 8))

Example 1: *CMD /K CD /D [directory]: Launch CMD with elevated right

:D Both %P and "%P" work if the path in the active panel has spaces.

Code: Select all

(Works for path with spaces)

buttonNN=%COMSPEC%
cmdNN=*%COMSPEC% /K
paramNN=CD /D "%P"

(Works for path with spaces)

buttonNN=%COMSPEC%
cmdNN=*%COMSPEC% /K
paramNN=CD /D %P
Example 2: FolderTimeUpdate.exe /BaseFolder [directory]: Launch FolderTimeUpdate and fill in with the current path

:? Only "%P" works if the path in the active panel has spaces. %P doesn't (program launches without any error message, but the path will be truncated up to the first space).

Code: Select all

(Works for path with spaces)

buttonNN=FolderTimeUpdate.exe
cmdNN=FolderTimeUpdate.exe
paramNN=/BaseFolder "%P"

(Doesn't work for path with spaces; path truncated)

buttonNN=FolderTimeUpdate.exe
cmdNN=FolderTimeUpdate.exe
paramNN=/BaseFolder %P
Example 3: WinRAR.exe [directory]: Launch WinRAR and navigate to the current directory

:evil: Neither %P nor "%P" works if the path in the active panel has spaces.

Code: Select all

(Doesn't work for path with spaces - error out)

buttonNN=WinRAR.exe
cmdNN=WinRAR.exe
paramNN="%P"

(Doesn't work for path with spaces - error out)

buttonNN=WinRAR.exe
cmdNN=WinRAR.exe
paramNN=%P
User avatar
nsp
Power Member
Power Member
Posts: 1396
Joined: 2005-12-04, 08:39 UTC
Location: Lyon (FRANCE)
Contact:

Re: [b9] %P or "%P" - How should I handle path with spaces?

Post by *nsp »

what works in many case is "%P\" as the latest \ of %P could be not well interpreted TC try to add itself " when needed but this is not all the time working.
jiangzhenjerry
Junior Member
Junior Member
Posts: 38
Joined: 2016-01-15, 23:57 UTC

Re: [b9] %P or "%P" - How should I handle path with spaces?

Post by *jiangzhenjerry »

I don't think it is the trailing '\' issue, as the programs here are flexible enough to recognize both [Directory Path] and [Directory Path\]. I tried it directly in command prompt already.
User avatar
Dalai
Power Member
Power Member
Posts: 7635
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Re: [b9] %P or "%P" - How should I handle path with spaces?

Post by *Dalai »

It depends on how the target program processes parameters. As nsp pointed out, %P always adds a backslash at the end to indicate it's a path rather than a directory. Since the backslash is usually used as an escape character, especially in C/C++ programming languages, the closing quote sign is "escaped", but the opening one is not. This leads to an imbalance in quoting, and there's no guarantee that all programs can handle such situation properly. Appending a backslash to %P escapes the backslash added by %P instead, so the closing quote remains intact.

It's up to debate whether or not TC should add a backslash, but because it has always been doing so, I doubt Ghisler is going to change it now.

If you have doubts about the backslash being the culprit, I suggest to add a question mark as the first character of the parameters field so you can see (and change) the parameters passed to the started programs. Try out different combinations, with and without trailing backslash, quoting and so on. Programs like Process Hacker and Process Explorer can also be used to inspect the command line of the started programs, which might give a clue to the culprit.

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

Plugins: Services2, Startups
User avatar
Hacker
Moderator
Moderator
Posts: 12041
Joined: 2003-02-06, 14:56 UTC
Location: Bratislava, Slovakia

Re: [b9] %P or "%P" - How should I handle path with spaces?

Post by *Hacker »

jiangzhenjerry,
As Dalai said, it's the difference between

Code: Select all

"c:\path\with spaces
and

Code: Select all

"c:\path\with spaces"
Roman
Mal angenommen, du drückst Strg+F, wählst die FTP-Verbindung (mit gespeichertem Passwort), klickst aber nicht auf Verbinden, sondern fällst tot um.
Fla$her
Member
Member
Posts: 127
Joined: 2020-01-18, 04:03 UTC

Re: [b9] %P or "%P" - How should I handle path with spaces?

Post by *Fla$her »

jiangzhenjerry wrote: 2021-05-09, 10:53 UTCNeither %P nor "%P" works if the path in the active panel has spaces.
Not confirmed. WinRAR version?
User avatar
Horst.Epp
Power Member
Power Member
Posts: 4309
Joined: 2003-02-06, 17:36 UTC
Location: Germany

Re: [b9] %P or "%P" - How should I handle path with spaces?

Post by *Horst.Epp »

This button works perfect for path with spaces as expected.
WinRAR is 6.01 x64 (but this not relevant)

Code: Select all

TOTALCMD#BAR#DATA
"C:\Program Files\WinRAR\WinRAR.exe"
"%P"
C:\Program Files\WinRAR\WinRAR.exe
WinRAR


-1
Windows 10 Home x64 Version 21H1 (OS Build 19043.1055)
TC 10.00 x64 / x86
Everything 1.5.0.1263a (x64)
jiangzhenjerry
Junior Member
Junior Member
Posts: 38
Joined: 2016-01-15, 23:57 UTC

Re: [b9] %P or "%P" - How should I handle path with spaces?

Post by *jiangzhenjerry »

Thanks everyone for sharing so many comments and thoughts. Really appreciated :D I am really amazed by how friendly and helpful everyone here is. I would hope to reply individually to your comments but fear that the post would be too long. So I'm trying to make my reply short.

Firstly, regarding how applications like WinRAR handle the backslash, they are pretty flexible. WinRAR does not treat [\"] as escaping ["]. For example, manually running the following two commands in the command prompt yields exact same result without errors:

Code: Select all

WinRAR.exe "D:\Downloads\Total Commander"
WinRAR.exe "D:\Downloads\Total Commander\"
Either way - with or without backslash - things are good.

Secondly, the button that @Horst.Epp provided indeed solves the problem. So what's the difference between his button and my original button? I found the answer -- I opened DEFAULT.BAR and noticed that the correct button is actually using double double-quotes (that is, two pairs of double-quotes) :shock:
buttonNN=WinRAR.exe
cmdNN=WinRAR.exe
paramNN=""%P""
Note that I didn't type this double double-quotes in DEFAULT.BAR myself. It is auto-generated by either directly pasting @Horst.Epp's code or manually creating a button and put "%P" (including the single pair of double-quotes) in the parameter.

I think my question still remains - why no quotes (as in DEFAULT.BAR) are ever needed in Example 1, and yet in Example 3 I need two pairs of double-quotes (again, referring to DEFAULT.BAR)?
Fla$her
Member
Member
Posts: 127
Joined: 2020-01-18, 04:03 UTC

Re: [b9] %P or "%P" - How should I handle path with spaces?

Post by *Fla$her »

2jiangzhenjerry
It's just INI syntax. Create buttons and commands through dialogues, rather than editing these files, then there will be fewer problems.
By the way, instead of %P, another option is already allowed:

Code: Select all

TOTALCMD#BAR#DATA
%ProgramFiles%\WinRAR\WinRAR.exe
-ap"%B"

WinRAR
jiangzhenjerry
Junior Member
Junior Member
Posts: 38
Joined: 2016-01-15, 23:57 UTC

Re: [b9] %P or "%P" - How should I handle path with spaces?

Post by *jiangzhenjerry »

Fla$her wrote: 2021-05-09, 16:06 UTC 2jiangzhenjerry
By the way, instead of %P, another option is already allowed:
Thank you for this hint. Yes, it does work. :D
Post Reply