Simple "SendTo" script on Windows - windows

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, e.g., "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.)

Related

How to execute this CMD Script in a Batch File?

I am trying to make a batch file that when executes makes a list named VideosToJoin.txt of the files with the extension .mp4, .mts and .avi in the folder and the after that proceed with the concat of those same files creating a final one named FinalVideo.mp4
I need some help here, because i want to automatize a specific process...
(for %i in (*.mp4,*.mts,*.avi) do #echo file '%i') > VideosToJoin.txt
ffmpeg -f concat -i VideosToJoin.txt -c copy FinalVideo.mp4
The expected result is to have the final video by executing just one file, that's all i want, please help.
A CMD Script requires that you double up the percent signs on a FOR loop.
Further you will need to specify what directory you want the script to check, because otherwise it will only check the CURRENT Directory, which is usually USER's Home Directory but could actually be any arbitrary directory depending on other factors.
I surmise you want to output the file to the same folder that the script will check so that much is easy, and we'll store the temporary videos file to the directory the script is in using the %~dp0 variable. %0 refers to the script itself, and ~dp tells it to return the drive and path to the script.
Also make sue that FFMpeg is in your path, or specify the full path to it in your script.
SET "_SrcPath=C:\Some\Folder\Path"
DEL /F /Q "%~dp0VideosToJoin.txt" 2>NUL
For %%i in (
"%_SrcPath%\*.mp4"
"%_SrcPath%\*.mts"
"%_SrcPath%\*.avi"
) do (
echo file '%%i'
)>>"%~dp0VideosToJoin.txt"
ffmpeg -f concat -i "%~dp0VideosToJoin.txt" -c copy "%_SrcPath%\FinalVideo.mp4"
You can do this using powershell:
Get-ChildItem "*.mp4","*.mts","*.avi" | %{ $_.Name } | Set-Content VideosToJoin.txt

Shell copy a file but changing extension

I am pretty new to linux and shell and I wold like to copy a file to a location passed by an argument but change its extension, the windows version of that would be
copy .\a\b\c.pre %~dpn2.i
d --> get the drive letter only
p --> get the path only
n --> get the file name only
So my question is : How can I get the full path of a file passed by argument and add a file extension to it?
The shell command would look like cp ./a/b/c.pre [$2 path but with extension .i]
Thanks in advance
for file in *.src; do
cp "${file}" "${tgt_dir}"/"${file/%.src/.tgt}"
done
This will simply rename all the files with extension .src in the source folder to .tgt in the target folder. You need to set the target folder in your script

Command for loop appending command name to files

I have an executable file, call it exec1.exe
I have a bunch of files with a .txt extension and I want to run exec1.exe on it and redirect output to a text file with the original file name somewhere in the output file. I'm running the command
for %i in (mydir\\*.txt) do exec1 %i > "%i2.txt"
But this tries to run on the first text file text1.txt,
exec1 exec1text1.txt > exec1text1.txt2.txt
But I want
exec1 text1.txt > text1.txt2.txt
Any idea what's going wrong?
You could probably get away with this, (double up the % if running from a batch file):
for %i in ("mydir\*.txt") do #start "" exec1.exe -y 754 "%i">"%~ni2.txt"
Note
I have used %~ni2 instead of %i2 to write to the current directory. This is because your command would be outputting to .txt whilst reading *.txt. An alternative would be to use a different known path, e.g. >"known\%i2.txt

windows command prompt substring in a for loop

I have to say before I describe my problem that I am new in performing command line prompts (Windows 10).
I have a directory with txt files (among other files). I need to perform a command line executable (txt2las) where the txt files are read and a .las file is written. This is what I have so far as a command prompt for /R %f in (.\*.txt) do txt2las -i %f -o %f.laz
However, this creates files like name.txt.laz. What I want is name.laz, therefore somehow take the substring from beginning till the last four characters. Can someone help me how to do that?
I tried the substring "function" but does not write anything.
You can extract the just the name of the file (without the extension) by using the ~n modifier:
for /R %f in (.\*.txt) do txt2las -i %f -o "%~dpnf.laz"
You can read more about the modifiers in the FOR /? help docs.
Edit per comments:
To ensure it is placed in the same directory as the source, add the ~dp modifiers in addition to ~n. This will extract the drive and path in addition to the file name. Also, you will likely want to wrap this entire output in quotes in the event the path has spaces in it.
... do txt2las -i %f -o %~nf.laz
The documentation can be read by executing for /? from the prompt.

Windows batch autoanswer XCOPY /i

I have created a batch file which uses FOR command to read a file of FROM Directory, FROM File, TO Directory, TO File as the parameters. (I am giving the files NEW names in the destination)
Everything works great until I add a new file to the mix.
In XCOPY /i option says it is a directory (which is NOT true). IF I don't use /i it wants to know if it is a file or a directory. It is ALWAYS a File. Is there a way I can autoreply or does someone have another suggestions.
echo f|xcopy [options] [files*]
A short investigation did not yield an easy command line parameter solution.
If I got you right, you are trying to copy a file from one directory to a new directory + give it a new name in the new directory. Copying to the new directory should work fine when you terminate target directory name with a tailing '\' (backslash) - this should result in a file with the same name in the target dir. Renaming this file to the new name will be straight forward. However, it's two commands instead of one...
In case this does not work for you: maybe you can illustrate your qn with a sample / snippet of the batch file?

Resources