right click command line help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
davzo
Posts: 2
Joined: 20 Jan 2020 08:28

right click command line help

#1 Post by davzo » 20 Jan 2020 08:37

hello
I am using a command line with some sofware like this:

C:\Program Files\SC\sc.exe /c pdf "%1" "%1".pdf

this should convert a file like example.plt to example.pdf
but instrad I am getting the converted file as :example.plt.pdf

I need it to convert it as a final result to:example.pdf
I tried to use this command below, and nothing happens at all.
I read that d=drive, p=path' n=filename

C:\Program Files\SC\sc.exe /c pdf "%1" "%~dpn1".pdf

can someone help on how to do this right so the files it creates will be example.pdf and not example.plt.pdf

Thanks

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: right click command line help

#2 Post by ShadowThief » 21 Jan 2020 04:24

What program are you using? sc.exe is already an executable that's built into Windows that controls system services, so I'm having a hard time Googling what you're using.

Just guessing wildly here because I don't have all of the information that I need, your ending quote is in the wrong place. Instead of "%~dpn1".pdf you should be using "%~dpn1.pdf" but without knowing what program you're using and what arguments it takes, it's hard to say. For all I know you only need to provide the basename and extension for the output file instead of the whole path.

davzo
Posts: 2
Joined: 20 Jan 2020 08:28

Re: right click command line help

#3 Post by davzo » 21 Jan 2020 08:19

Hi
Thanks for the anwsew. tried it but it won't work.
I am using Viecompanion viewer software :
C:\Program Files\Software Companions\ViewCompanion Premium\viewcompanion.exe /c pdf "%1" "%1".pdf
(the above is what I am currently using but it converts files from example.plt to --> example.plt.pdf
and I need it do convert to only example.pdf

Thanks

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: right click command line help

#4 Post by Squashman » 21 Jan 2020 09:41

So you are using the SENDTO option of the context menu?

The syntax for the command is
ViewCompanion /c format inputfile outputfile

So ShadowThief gave you the correct syntax for using the command modifiers.

ViewCompanion /c pdf "%~1" "%~dpn1.pdf"

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: right click command line help

#5 Post by Eureka! » 21 Jan 2020 11:11

At least, you should enclose the program name - including folder - in double quotes as it contains spaces:
"C:\Program Files\Software Companions\ViewCompanion Premium\viewcompanion.exe"

Furthermore, the "%~dpn1" syntax is not supported everywhere, so it is very relevant how and where you start this, as @Squashman already pointed out.

EDIT:
Possible workaround removed as that didn't work ...

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: right click command line help

#6 Post by ShadowThief » 22 Jan 2020 17:31

Eureka! wrote:
21 Jan 2020 11:11
At least, you should enclose the program name - including folder - in double quotes as it contains spaces:
"C:\Program Files\Software Companions\ViewCompanion Premium\viewcompanion.exe"

Furthermore, the "%~dpn1" syntax is not supported everywhere, so it is very relevant how and where you start this, as @Squashman already pointed out.

EDIT:
Possible workaround removed as that didn't work ...
I would be very interested to know in what situations %~dpn1 won't work.

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: right click command line help

#7 Post by Eureka! » 22 Jan 2020 19:19

ShadowThief wrote:
22 Jan 2020 17:31
I would be very interested to know in what situations %~dpn1 won't work.
Windows context menu entries, for example.

Here the command is executed by the shell (Explorer) and not CMD.
Context menu entries use their own "variables" %1 and %* (and a couple of less often used ones like %L %v and %i) to pass parameters to - in this case - viewcompanion.exe.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: right click command line help

#8 Post by penpen » 23 Jan 2020 04:45

If you are using a link for the sendto menue, this might help you (tested on Win10) to type into the field "target" of the link (properties; this only echoes the command lines):

Code: Select all

C:\Windows\System32\cmd.exe /V:ON /E:ON /k @for %n in (1 2) do @if %n == 2 (for %b in (!params!) do @call echo("C:\Program Files\Software Companions\ViewCompanion Premium\viewcompanion.exe" /c pdf "%~b" "%~dpnb.pdf") else set params=
If the echoed command lines are correct, then remove the "echo(" and change "/k" to "/c" (untested, but should work):

Code: Select all

C:\Windows\System32\cmd.exe /V:ON /E:ON /c @for %n in (1 2) do @if %n == 2 (for %b in (!params!) do @call "C:\Program Files\Software Companions\ViewCompanion Premium\viewcompanion.exe" /c pdf "%~b" "%~dpnb.pdf") else set params=
penpen

MicrosoftIsKillingMe
Posts: 55
Joined: 11 Dec 2017 09:08

Re: right click command line help

#9 Post by MicrosoftIsKillingMe » 25 Oct 2021 08:51

I apologize in advance if this has been covered. I bled mucho time searching before posting.

I am finding that the "p" in the magic substitution string %%~_1 causes processing failure if an ampersand is in the DIR name, e.g.
c:\my dog & pony
This is on Win 10, regular CMD, .BAT. %1 is foo.txt (or foo) (and foo.txt exists there)
While in such a DIR, with a filename as %1, the batch file correctly processes %%~n1
echo {%%~n1}
but anything with a p in it produces nothing, however you wish to correctly express "nothing". Put it this way, SET TEMP_TEMP=%%~dpn1.txt leaves TEMP_TEMP unset, and
echo {%%~dpn1}
shows nothing, maybe just an empty line.
n1 works. dn1 works. dpn1 fails. Anything with p fails. (nf1 fails also)

If I am correct and some other bizarre or user-101 factor is not to blame, and p is indeed unusable, my first desire is to begin the BAT with an IF, perhaps using the colon(:) trick for testing for a substring. But besides the intimidation of achieving proper "escaping" of the testing ampersand, ~p doesn't work anyway so aieee I don't even know if I can see the path (i.e. folder name). Of course, this is the surrender solution, where I'll echo an admonition and exit the BAT without doing what the BAT was made for!

What's my next move - assuming anyone reproduces this anyway? I tried renaming the dir without & and it worked, so I'm strongly reproducing the problem.

And thanks SO MUCH for the site and for people who still care to be effective and efficient and productive by taking advantage of DOS. Even if evil forces threaten to blow it all up, I still assure you that you are doing God's work :)

[edited 11:20 AM EDT for typos, such as revising %p to be p for clarity]

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: right click command line help

#10 Post by aGerman » 25 Oct 2021 14:04

Seems you tricked out yourself. You know, the & is a special character used for command concatenation. You have to quote or escape it properly.
This code works flawlessly for me

Code: Select all

@echo off &setlocal
echo "%~1"
echo "%~d1"
echo "%~p1"
echo "%~n1"
echo "%~x1"
SET "TEMP_TEMP=%~dpn1"
echo "%TEMP_TEMP%"
pause
Either drag/drop foo.txt on it or call it using foo.txt as argument.

Steffen

MicrosoftIsKillingMe
Posts: 55
Joined: 11 Dec 2017 09:08

Re: right click command line help

#11 Post by MicrosoftIsKillingMe » 25 Oct 2021 14:21

(deleted)
Last edited by MicrosoftIsKillingMe on 25 Oct 2021 17:08, edited 1 time in total.

MicrosoftIsKillingMe
Posts: 55
Joined: 11 Dec 2017 09:08

Re: right click command line help

#12 Post by MicrosoftIsKillingMe » 25 Oct 2021 14:28

Anyway, I see that you used double quoting. It new works when surrounding with double quotes.

The double quoting wasn't necessary when embedded spaces were in the folder name, but were with the ampsersand. Thanks for getting me to that solution.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: right click command line help

#13 Post by aGerman » 25 Oct 2021 14:33

aGerman wrote:
25 Oct 2021 14:04
the & is a special character used for command concatenation
:wink:
Quoting SET assignments and quoting paths is just best practice to avoid problems with special characters.

Steffen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: right click command line help

#14 Post by Squashman » 25 Oct 2021 16:13

If you are doing a drag and drop onto a batch file, the file paths that have spaces in them will get quoted. If you drag and drop a file with no spaces but has an ampersand it will not get quoted.

MicrosoftIsKillingMe
Posts: 55
Joined: 11 Dec 2017 09:08

Re: right click command line help

#15 Post by MicrosoftIsKillingMe » 25 Oct 2021 17:07

Thank you - similar to my new understanding that %~p is "already" effectively quoted if there are spaces, but not if there is an ampersand even if there are spaces.

I don't know what I would have done without you guys.

Post Reply