I have provided all the detail and code below, but, the problem is only in the last line.
Background
I am creating a script named, GetSelectedFiles.cmd, whose shortcut is added to Windows' "Send To" context menu. For example, by copying the shortcut of above file to location: c:\users\user\AppData\Roaming\Microsoft\Windows\SendTo
.
Purpose:
The goal of that script file is to get the list of selected file names when the user goes to the context menu and selects Send To > GetSelectedFiles.cmd.
Requirement:
1) The the selected file names will be separated by new lines.
2) The list will only contain the bare file name and extension (no path).
3) The list will be a saved inside the same directory as selected files.
4) The list will will be saved in a file whose file name matches the first selected file and has extension of `.selection`.
Example:
Assume you are in directory c:\users\u\
. Say this directory has files named: w.txt, x.txt, y.txt, z.txt, with some other files.
User selects those above named 4 files, right clicks and does Send To > GetSelectedFiles.cmd.
After completing the above steps, the directory should have a new file named w.selection
and it should contain following lines
w.txt
x.txt
y.txt
z.txt
It is a basic task, but, where I am having problem is the last line, specifically %firstFile%
just returns empty. What am I missing?
::::::::::::::::::::::::::::::::::::
:::: Here is the complete code :::::
setlocal EnableDelayedExpansion
:: For new line const
set LF=^
for %%A in (%*) do (
IF NOT DEFINED firstFile (SET firstFile=%%~nxA)
::: This last line is the problem!
echo %%~nxA ^%LF% >> %%~dpA%firstFile%.selection
)