Page 1 of 1
right click command line help
Posted: 20 Jan 2020 08:37
by davzo
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
Re: right click command line help
Posted: 21 Jan 2020 04:24
by ShadowThief
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.
Re: right click command line help
Posted: 21 Jan 2020 08:19
by davzo
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
Re: right click command line help
Posted: 21 Jan 2020 09:41
by Squashman
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"
Re: right click command line help
Posted: 21 Jan 2020 11:11
by Eureka!
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 ...
Re: right click command line help
Posted: 22 Jan 2020 17:31
by ShadowThief
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.
Re: right click command line help
Posted: 22 Jan 2020 19:19
by Eureka!
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.
Re: right click command line help
Posted: 23 Jan 2020 04:45
by penpen
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
Re: right click command line help
Posted: 25 Oct 2021 08:51
by MicrosoftIsKillingMe
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]
Re: right click command line help
Posted: 25 Oct 2021 14:04
by aGerman
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
Re: right click command line help
Posted: 25 Oct 2021 14:21
by MicrosoftIsKillingMe
(deleted)
Re: right click command line help
Posted: 25 Oct 2021 14:28
by MicrosoftIsKillingMe
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.
Re: right click command line help
Posted: 25 Oct 2021 14:33
by aGerman
aGerman wrote: ↑25 Oct 2021 14:04
the & is a special character used for command concatenation
Quoting SET assignments and quoting paths is just best practice to avoid problems with special characters.
Steffen
Re: right click command line help
Posted: 25 Oct 2021 16:13
by Squashman
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.
Re: right click command line help
Posted: 25 Oct 2021 17:07
by MicrosoftIsKillingMe
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.