Passing filenames as arguments

English support forum

Moderators: Hacker, petermad, Stefan2, white

Post Reply
Perplexer
Junior Member
Junior Member
Posts: 55
Joined: 2007-09-22, 10:55 UTC

Passing filenames as arguments

Post by *Perplexer »

Two questions here:

I'm exploring the functionality of passing filenames as arguments to programs and scripts (Command:) in the Parameters: field of a custom configured button.

I wrote a small batch script to help me observe how different things work:

Code: Select all

@echo off

REM Loop through the parameters
for %%i in (%*) do (
    echo %%i
)

timeout /t 50
I then created a button with d:\script.bat in the the Command: textfield and %P%S in the Parameters: textfield. I then select/mark a couple of files in the pane and click the button. Script opens a CMD window and prints out their paths and filenames, for example:

Code: Select all

"d:\Downloads\File One.txt"
"d:\Downloads\File Two.txt"
"d:\Downloads\File Three.txt"
However, if I happen to also select/mark a file named FileFour.txt (notice no space), then the script won't run at all. This is probably because TC doesn't put double quotes around this one since it sees there is no space in the path & filename, and so it breaks for some reason. If I introduce a space by renaming the Downloads folder to Down loads or if I simply put a space in the filename, then it will work again. The solution is to put "%P%S" in double quotes in the Parameters: textfield, even though the help pages say not to:

Code: Select all

%P%S
insert the names of all selected files into the command line, with full path. Names containing spaces will be surrounded by double quotes. Do NOT put quotes around %P%S yourself!
I guess it only works without quotes if passing those parameters to a program (some_program.exe) but with a batch script, you apparently need to use them. Or is there something I can change in the script itself so that it will correctly receive the agruments and run even if I don't put %P%S in double quotes?

Second question:

%P%S passes the names of all selected files into the command line
%N passes the filename under the cursor into the command line

Is there a way to pass the file name only if it is MARKED (right-click with the mouse to make it red) and NOT if it is only selected (under the cursor)? I would like to avoid any accidents if I mistakenly click on the button which would cause at least one file/folder to be passed as an argument and get "worked on".
Last edited by Perplexer on 2023-10-22, 19:54 UTC, edited 1 time in total.
User avatar
Dalai
Power Member
Power Member
Posts: 9963
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Re: Passing filenames as arguments

Post by *Dalai »

The command line length is limited by TC and eventually by Windows, which means that there's a limit of how many filenames you can pass to a command, especially when each of them contains a path (when %P%S is used).

I suggest to use %L instead which is not limited. This also works around the quoting issues you seem to be having. The for-loop in a CMD to process the list is very similar:

Code: Select all

for /F "tokens=* delims=" %%A IN (%~1) DO (
    REM do something with each line/file
)
Button:

Code: Select all

Command: Path_to_your_file.cmd
Parameters: %L
Regarding your second question: You might want to put a question mark at the start of the Parameters field. TC then shows a dialog with expanded parameters before actually executing the command. Or, write the script in such a way that it requires a confirmation before actually doing something. There are several ways to achieve what you want.

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
Fla$her
Power Member
Power Member
Posts: 2996
Joined: 2020-01-18, 04:03 UTC

Re: Passing filenames as arguments

Post by *Fla$her »

Perplexer wrote: 2023-10-22, 18:24 UTCHowever, if I happen to also select/mark a file named FileFour.txt (notice no space), then the script won't run at all.
I don't confirm. By the way, the last percentage sign in your code is redundant: echo %%i%
Why don't you use "%S" instead of %P%S to reduce the impact of the command line length limit? Or do you use the button after searching in different folders?
Perplexer wrote: 2023-10-22, 18:24 UTC I would like to avoid any accidents if I mistakenly click on the button which would cause at least one file/folder to be passed as an argument and get "worked on".
And you don't click mistakenly. You need to treat the file under the cursor in the same way as any marked one. Take this as a rule and then you won't have to change the concept.
Something you can learn from this topic.
Overquoting is evil! 👎
Perplexer
Junior Member
Junior Member
Posts: 55
Joined: 2007-09-22, 10:55 UTC

Re: Passing filenames as arguments

Post by *Perplexer »

Fla$her , thanks for the heads-up regarding %%i%. Was a leftover from me playing around. Removed it now from my original post.

I use %P%S (actually "%P%S") because in the actual script I am calling different programs and passing them the received files from TC as arguments (depending on their extensions), and those programs need to get the full path to the files as input. Perhaps I could pass the path separately first, followed by the file names (if TC allows that, I would need to study the help pages more) and then concatenate that in the script, but I barely got this one together for now.

"And you don't click mistakenly." ... is a fantasy. Misclicks do happen, especially on cluttered button bars. TC could easily offer a parameter that would only be populated if the items were actually marked (not just selected), otherwise be empty. As a user I would prefer that over trusting myself to be "careful".
Fla$her
Power Member
Power Member
Posts: 2996
Joined: 2020-01-18, 04:03 UTC

Re: Passing filenames as arguments

Post by *Fla$her »

Perplexer wrote: 2023-10-22, 20:14 UTCand those programs need to get the full path to the files as input.
Many programs cope with reading names without paths if the start path field is cleared in the button.
Perplexer wrote: 2023-10-22, 20:14 UTC and those programs need to get the full path to the files as input. Perhaps I could pass the path separately first, followed by the file names
So %P is this path. Also with "%S" you can use %cd%: for %%f in (%*) do echo "%cd%\%%~f"
Perplexer wrote: 2023-10-22, 20:14 UTC Misclicks do happen, especially on cluttered button bars. TC could easily offer a parameter that would only be populated if the items were actually marked (not just selected), otherwise be empty. As a user I would prefer that over trusting myself to be "careful".
I am also the owner of cluttered panels on the test stand, but I don't accidentally click anywhere, well, or it happens extremely rarely. And it should be understood that accidentally you can both mark and press after marking.
I'm writing about the concept. You yourself should treat the use of scripts in relation to the file under the cursor as a matter of course. This is the concept that the author laid down: to process only one file, you do not have to mark it every time, it is enough to place the cursor on it.
But as I've already hinted, you're not the only one who needs it.
Overquoting is evil! 👎
User avatar
petermad
Power Member
Power Member
Posts: 16027
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: Passing filenames as arguments

Post by *petermad »

Perplexer wrote:Is there a way to pass the file name only if it is MARKED (right-click with the mouse to make it red) and NOT if it is only selected (under the cursor)?
Yes use %Y:
Help wrote:%Y anywhere in the parameters: Pass empty list to program when nothing is selected when using one of the List parameters like %L, or multi-file parameters like %S. Otherwise, the file under the cursor would be passed.

With %Q you can turn off TC's automatic quotation, and set it manually for all files also those without spaces - use: %Q"%P%S"
Help wrote:%Q Turn off automatic quotation marks around certain parameters like %P%N when the name contains a space. The user will then have to place them by himself.
So I guess %Y%Q"%P%S" might solve your problem.
License #524 (1994)
Danish Total Commander Translator
TC 11.51 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1391a
TC 3.60b4 on Android 6, 13, 14
TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
Post Reply