NirCmd: Copy Path vs. Copy File Name

nircmd.exe execmd (echo.|set /p=\"%1\") | clip

Copies %1 path to the clipboard.

How can I copy the file name to the clipboard using NirCmd?

echo. | set /p = "%~nx1" | clip works in a BAT file.

but nircmd.exe execmd (echo.|set /p=\"%~nx1\") | clip does not work.


Solution 1:

// Edit

echo. | set /p = "%~nx1" | clip works in a BAT file.
Yes, the command-line interpreter will expand the argument %1 into the desired form, %~nx1 (Name.Extension) of your argument passed to your bat executed by cmd.exe

but nircmd.exe execmd (echo.|set /p="%~nx1") | clip does not work.
Note that it won't work, because you want to expand a registry argument to a nircmd command complement, and that's outside of a bat that didn't take any arguments, what was passed to the registry, not a bat, by command interpreter...

There is no expansion (%~?!1) for any registry arguments:

Arguments:
%1, %D, %H, %I, %L, %S, %V, %W
Not Work/Expand:
%~nx1, %~nxD, %~nxH, %~nxI, %~nxL, %~nxS, %~nxV, %~nxW
  Often we find "%1" in the commands associated with file types
(HKEY_CLASSES_ROOT\FileType\shell\open\command).
In Windows 9x through 2000 (and possibly XP) this would evaluate
to the short notation of the fully qualified path of the file of type FileType.
To get the long file name, use "%L" instead of "%1".


How can I copy the file name to the clipboard using NirCmd?
It's not possible

  • 1. Considering that %L returns me the full path (with Name.eXtension)

  • 2. Considering that %W returns me the path only (without %~nx-Name.eXtention)

  • 3. Use a variable to replace and get the substring that removes the path %W\ from the variable defined to %L:

C:\\Windows\\nircmd.exe execmd set "_=%L" & cmd /v /s /c "echo\|set/p;.="!_:%W\"=!"|clip"
  • To add entries without using the command line, you can use a hybrid bat file with reg.

  • Just save the file below as .cmd or .bat and run as administrator.

Windows Registry Editor Version 5.00

;@(cls&%__APPDIR__%reg.exe import "%~f0" & goto :EOF)

[HKEY_CLASSES_ROOT\*\shell\CopyFileName]
@="CopyFileName"

[HKEY_CLASSES_ROOT\*\shell\CopyFileName\command]
@="C:\\\\Windows\\\\nircmd.exe execmd set \"_=%L\" & cmd /v /s /c \"echo\\|set/p;.=\"!_:%W\\=!\"|clip\""

Edit //


What you get using %1 is the full path of the file, because %1 is also an argument to the registry...

  • You can try these commands and see the result, but for registry entries run as admin
%__AppDir__%reg.exe add "HKEY_CLASSES_ROOT\*\shell\CopyFileName" /ve /d "CopyFileName" /f
%__AppDir__%reg.exe add "HKEY_CLASSES_ROOT\*\shell\CopyFileName\command" /ve /d "C:\\Windows\\nircmd.exe execmd echo\|set/p;.=\"%1\"|clip" /t reg_sz /f
  • Obs.: 1 You can replace %1 and check the results for %D, %H, %I, %L, %S, %V and %W.

  • Obs.: 2 You can replace /t reg_sz with /t reg_expand_sz, get the same results.

  • Obs.: 3 Using %L you get the the Long fully qualified path of file.

  • Obs.: 4 Replace my path to Nircmd.exe (C:\\Windows\\nircmd.exe) to your path.


  • Consider consulting the answers to this question:
    Which special variables are available when writing a shell command for a context