Copy name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
celsius
Posts: 11
Joined: 05 Feb 2020 01:27

Copy name

#1 Post by celsius » 05 Feb 2020 01:41

Hi, Greetings to all users of this forum

I need help to modify this command

Code: Select all

cmd /c echo|set /p="%2"|clip
It copies full path of the selected file, I'd like instead coping only the name without extension
I know this is an elementary request, I'm sorry but, please, be patient

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

Re: Copy name

#2 Post by ShadowThief » 05 Feb 2020 06:32

Variable expansion for filename manipulation is hidden in the output of for /? for some reason, but if you manage to find it you'll see that you can use

Code: Select all

cmd /c echo|set /p="%~n2"|clip
to get the filename

celsius
Posts: 11
Joined: 05 Feb 2020 01:27

Re: Copy name

#3 Post by celsius » 05 Feb 2020 06:50

Thanks,
Unfortunately in my case this solution doesn't work. It is copied only %~n2
I tried the for variable output, but as now, only the variable is copied and not the filename

jeb
Expert
Posts: 967
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Copy name

#4 Post by jeb » 05 Feb 2020 08:09

Hi celsius,

is it possible that your command is bound to the context menu of the explorer?
And your command is stored in the registry?

I'm only guessing, but when "%~n2" doesn't expand, that's an obvious possibility.

jeb

celsius
Posts: 11
Joined: 05 Feb 2020 01:27

Re: Copy name

#5 Post by celsius » 05 Feb 2020 08:43

I'd like to create a button in the explorer toolbar of Open-Shell that allows me to copy the name of the selected file.
I was able to copy only the full path

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

Re: Copy name

#6 Post by Squashman » 05 Feb 2020 10:02

Look at Context Menu options using CMD.exe or Powershell
In my first example it would be extremely easy to change it from copying the file path to just the file name.

celsius
Posts: 11
Joined: 05 Feb 2020 01:27

Re: Copy name

#7 Post by celsius » 05 Feb 2020 10:45

Yes, it works well as context menu. Again, as button, the variable is not expanded

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

Re: Copy name

#8 Post by Squashman » 05 Feb 2020 10:52

celsius wrote:
05 Feb 2020 10:45
Yes, it works well as context menu. Again, as button, the variable is not expanded
Then you are going to have to work with the developer of Open Shell.

celsius
Posts: 11
Joined: 05 Feb 2020 01:27

Re: Copy name

#9 Post by celsius » 05 Feb 2020 11:18

In my last attempts, I also tried

Code: Select all

cmd /c echo|set /p="%1"|clip
In this way is it copied the path without the filename. I tried 0 4 5 without luck
Also, I realized that in these cases and in your suggestions is it copied nothing. The clipboard content didn't change

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

Re: Copy name

#10 Post by penpen » 06 Feb 2020 05:52

I used something similar for a link in the win10 "sendto" menue - but i don't know if that also works for buttons in openshell, so the following might or might not help you:

Code: Select all

%comspec% /V:ON /E:ON /c @for %n in (1 2) do @if %n == 2 (if defined params for %b in (!params!) do @call <nul set/p= "%~nb" | clip) else set params=
penpen

celsius
Posts: 11
Joined: 05 Feb 2020 01:27

Re: Copy name

#11 Post by celsius » 06 Feb 2020 13:03

Thanks,
Unfortunately it doesn't work

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

Re: Copy name

#12 Post by Eureka! » 06 Feb 2020 18:15

So this OpenShell has it's own definitions of %1 (path of selected file) and %2 (fully qualified filename including path)?
And it doesn't understand things like %~2 and passes % it doesn't understand as % (instead of stripping it) ...

In that case, does this work for you?

Code: Select all

cmd.exe /c @for %x in ("%2") do @echo %~nx|clip

And please, provide better feedback than "It doesn't work" ....

celsius
Posts: 11
Joined: 05 Feb 2020 01:27

Re: Copy name

#13 Post by celsius » 06 Feb 2020 19:28

I get on internet the code I posted in my first post. I don't remember anymore what is the site. It's not related with OpenShell.
Even with your code there is nothing copied in the clipboard, its content isn't modified at all

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

Re: Copy name

#14 Post by Eureka! » 06 Feb 2020 20:30

OK, downloaded OpenShell; broke open the installer and extracted the DLL responsible for the Explorer Toolbar; registered the DLL and tried my suggestion. I could see a CMD windows opening and closing very fast .. and that was it.

This does work (tested):

Code: Select all

%SystemRoot%\System32\cmd.exe  /c @for %%x in ("%2") do @echo %%~nx|clip
(It should be %% instead of % after all)

celsius
Posts: 11
Joined: 05 Feb 2020 01:27

Re: Copy name

#15 Post by celsius » 06 Feb 2020 22:42

Thanks!!!
I'm very happy :D :D :D

The last button I'd like to create is one that selects all files with the same extension of the already selected file. Do you think is possible through the command line?
Alternative I need to modify an Autohotkey script? (I know this is not the right forum, sorry)

Post Reply