How to execute this CMD Script in a Batch File? - for-loop

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

Related

Simple "SendTo" script on 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.)

How to write NUL to all log files in a folder using windows command line

I have multiple log files that start as ABC_.log in a windows environment. I want to clean that file (like writing /dev/null to file in linux). I need to do it through command line.
What I tried:
cmd:$ break > ABC_*.log
and
cmd:$ type NUL > ABC_*.log
Error:
The filename, directory name, or volume label syntax is incorrect
this can't be done via wildcard (not possible to redirect to more than one file at a time). Use a for loop to process each file on it's own:
for %%a in (ABC_*.log) do (
break>"%%a"
)
or directly on command line:
for %a in (ABC_*.log) do break>"%a"
The easiest way to empty a file in UNIX/Linux:
rm <filename>
touch <filename>

Copy Contents From Folder Using Wildcard On The Directory

I have a directory which contains CSV files needing to be moved to another directory:
C:\Users\JohnSmith\Desktop\Testing\report-20180819040000-20180826040000-4
We receive a new file weekly where the dates in the directory name will be updated. I want to create a batch file to copy this data using a wildcard on report* but I am running into some issues.
The wildcard appears to work without any issues when I first navigate to:
C:\Users\JohnSmith\Desktop\Testing\
then use:
dir report*
It also works fine when I navigate to:
C:\Users\JohnSmith\Desktop\Testing\
then run
copy * C:\Users\JohnSmith\Desktop\Testing\Destination
My goal is to be able to run something simple in my batch file like the below:
copy C:\Users\JohnSmith\Desktop\Testing\report* C:\Users\JohnSmith\Desktop\Testing\Destination
Whenever I try running the above, I receive the following error:
The system cannot find the file specified.
0 file(s) copied.`
Does anyone have any suggestions?
Use For /D with a wildcards for your directory name, then you can use Copy with a wildcard too!
From the Command Prompt:
For /D %A In ("%UserProfile%\Desktop\Testing\report-*-*-*") Do #Copy "%A\*.csv" "%UserProfile%\Desktop\Testing\Destination">Nul 2>&1
From a batch file:
#For /D %%A In ("%UserProfile%\Desktop\Testing\report-*-*-*") Do #Copy "%%A\*.csv" "%UserProfile%\Desktop\Testing\Destination">Nul 2>&1
Alternatively you could do it directly at the Powershell Prompt:
Cp "$($Env:UserProfile)\Desktop\Testing\report-*-*-*\*.csv" "$($Env:UserProfile)\Desktop\Testing\Destination"

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.

Recursively search directories moving specified file type from one location to another preserving directory structure

Is there some way to specify a directory (let's say "C:\images") and move every .jpg file from there to another directory (say "D:\media") but preserve the directory structure (so if the file were "C:\images\paintball\july\07\headshot.jpg" after moving it would be "D:\media\paintball\july\07\headshot.jpg")?
I'm using cygwin (but would be happy to use DOS if that works too).
Yup.
Do a tar archive of *.jpg files while preserving directory structure (there's a switch) then extract it to the target directory. Should be a one-liner.
( cd /cygdrive/c/images
tar --create --file - . ) | ( cd /cygdrive/d/media
tar --extract --file - )
There's also a --directory option in some versions of tar with which you can avoid the complexity of piping between subshells, but I never use it myself, so I may be missing something:
tar --create --file - -C /cygdrive/c/images . | tar --extract --file - -C /cygdrive/d/media
If you need more power/flexibility, take the time to investigate rsync.
Since you're on windows, you could also take a look at xxcopy. It's great for this kind of stuff and much else.
You can also use xcopy command, like in this example (old is a directory):
xcopy cvs_src\*.jpg old /e/i/h/y/d/exclude:files_to_exclude
Thanks for the XCOPY solution, it solved my similar problem, so I thought I'd share the details for anyone else needing it.
I wanted a list (not a copy) of all the files in a directory (and sub-directories) that were not of a particular type, such as *.jpg. But the DIR command doesn't have an exclude function. So I:
Created a file named exclist.txt that contained a single line ".jpg"
Ran the command "xcopy c:\files c:\test /exclude:exclist.txt /l /d /e /h /i /y > found.txt"
Opened found.txt in Notepad to see the list of non-jpg files
Note the XCOPY /l parameter, which lists the files to be copied without copying them. Since XCOPY is executed in "list mode", the destination folder c:\test is not created and no files are copied. "> found.txt" saves the output from the XCOPY command to the file found.txt, rather than displaying the results on screen.

Resources