I would like to create a simple script containing:
"C:\Users\Professional\NVEncC_5.24_x64\NVEncC64.exe" --codec h265 --preset quality --profile main10 --tier high -i "Project.mkv" -o "Project-Q27.mkv"
that i can reach by Right Click => SendTo.
I Understand that i need to name the file XXX.bat and paste it in SendTo folder.
But i don't know how to get the filename to dynamically add it to the script instead of "Project.mkv".
Can you help me?
Thanks !
K.
A.bat file with the following should work.
"C:\Users\Professional\NVEncC_5.24_x64\NVEncC64.exe" --codec h265 --preset quality --profile main10 --tier high -i "%~f1" -o "%~n1-Q27.mkv"
pause
I also added the pause so you'll be able to see what happens if it's quick but it can be removed.
%~f1
is the argument for the file you've right clicked and used "SendTo" on (expanded to the full path). %~n1-Q27.mkv"
is essentially the same as argument %~f1
but without the file extension and it adds -Q27.mkv
to whatever the filename was.
So if you right click/SendTo/yourbat.bat on a file called Funny.mkv
the command (NVEncC64.exe) will be run on that file and output to a file called Funny-Q27.mkv
.
I suggest you take backups of your files before testing so that you do not overwrite any existing files by mistake.
To get the name of the PowerShell .ps1
script being run, you can use the following command from MyInvocation
:
$MyInvocation.MyCommand
This will return the .ps1
file object.
To get only the name string you could run: $MyInvocation.MyCommand.Name
While the answer that @notjustme provided works, I have a small addition to it: the output variable should include the drive and path of the filename that was passed to the batch file to have the output file be created in the same directory as the source file.
"C:\Users\Professional\NVEncC_5.24_x64\NVEncC64.exe" --codec h265 --preset quality --profile main10 --tier high -i "%~f1" -o "%~d1%~p1%~n1-Q27.mkv"
pause
The filename that is passed when you right-click --> SendTo --> your_batch_file.cmd is referenced as %1
or the first parameter. Because it is a filename, it can be parsed and expanded further into its parts. And as mentioned by @notjustme:
%~f1
expands %1 (the filename passed) to its fully qualified path name %~n1
expands %1 to its file name only But there is also: %~d1
expands %1 to its drive letter %~p1
expands %1 to its full path %~x1
expands %1 to its file extension only If you do not add the %~d1%~p1
to the output file variable, by default, the transcoded file will not be created in the same directory as the source file ; rather, it will be created in the directory where your batch file is located.
(Additionally, given rigaya's rather short development windows with NVEncC, you might want to install it to a more generally-named directory that does not include the versioning, eg, "C:\Users\Professional\NVEncC\"
instead of "C:\Users\Professional\NVEncC_5.24_x64\"
. That way, you do not have to update its path in your batch file with every new release.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.