I'm trying to create a Batch whose purpose is to use MediaInfo.exe (CLI) to create a single text file (.nfo) containing all the "mediainfo" of the video files contained in a folder.
The Batch is executed from the context menu: right click on the folder containing the video files. To do so, the file will be placed in "shell:sendto".
The generated .nfo file containing all the mediainfo has to be "placed/saved or move" in the folder containing the video files.
A specificity is that i need to alter the final .nfo file, in order to delete all the path part of the lines "Complete name" :
Complete name : /Users/me/Downloads/Folder1/Folder2/RÉCENTS/[File] File Name - 185 (Encode Format 1080p).mkv
in this example : Do a search & replace to leave only :
Complete name : [File] File Name - 185 (Encode Format 1080p).mkv
For the moment, I've managed to create this batch (link below). This one has a few bugs, namely :
To summarize process searched by the Batch :
1. Right click on the folder containing the files > send to > click on the Batch file.
2. Creation of all the Media info txt files of the files contained in the folder
3. Merge Media Info txt files by adding a separator between each content "----".
4. Search & Replace on the final file with a Regex to remove all path before filename, then save (.nfo) inside the folder.
5. Clean the old temporary files from step 2.
The actual Batch files I managed to create :
@echo off
:: Fullpath current folder : %cd%
:: Fullpath folder where the right click was made : %~f1
:: Name of the folder where the right click was made : for %%f in ("%~f1") do set Name=%%~nxf
for %%f in ("%~f1") do set Name=%%~nxf
:: Step 1: Creating a temporary folder
mkdir "%~f1-Temp"
:: Step 2: Creation of all nfo txt files in the temporary folder
FOR /F "tokens=*" %%G IN ('dir "%~f1" /b *.mkv') DO (
call "C:\Users\me\Desktop\MediaInfo\MediaInfo.exe" "%~f1\%%G" > "%~f1-Temp\%%~nG.txt"
)
:: Step 3: Filling in the final nfo
setlocal enabledelayedexpansion
set i=0
FOR /F "tokens=*" %%G IN ('dir "%~f1" /b *.mkv') DO (
IF !i! == 1 (
echo ------------------------------------------------------------------------------------------------------------------------------------ >> "%~f1-Temp\%Name%.txt"
echo. >> "%~f1-Temp\%Name%.txt"
echo. >> "%~f1-Temp\%Name%.txt"
)
cat "%~f1-Temp\%%~nG.txt" >> "%~f1-Temp\%Name%.txt"
set i=1
)
endlocal
:: Step 4: Removing the path
setlocal
set $source="%~f1-Temp\%Name%.txt"
set $dest="%~f1-Temp\%Name%1.txt"
set "search=%~f1\\"
for /f "delims=" %%a in ('powershell -c "(get-content '%$source%') | foreach-object {$_ -replace '(?<=Complete name\s+:\s+).+\\'} | set-content '%$dest%'"') do echo %%a
endlocal
:: Step 5: Renaming the nfo and moving to the right place
Ren "%~f1-Temp\%Name%1.txt" "%Name%.nfo"
move "%~f1-Temp\%Name%.nfo" "%~f1"
:: Step 6: Deleting temporary files
rmdir /s /q "%~f1-Temp"
pause
If you have suggestions for improvement or/and corrections of what is already coded, don't hesitate! Thank you in advance for your help!
I think you've created an issue yourself, by providing MediaInfo with the full path as the argument. If you just provide it with the filename, the Complete name
will reflect just the filename.
In addition, unless I'm misunderstanding your intention, you're jumping through many hoops when I do not believe they are necessary.
The following example batch file should be created in your usual scripts directory, or even 'Documents' if you prefer.
Then you should create a shortcut to it in your 'SendTo' directory, giving it a name which you'll understand, perhaps MediaNFO
.
Now all you should need to do is right click on your directory and choose Send to
➡ MediaNFO
. This should create a file within the right clicked directory, with the same name as that directory, and containing the mediainfo
output for every supported file it contains. The Complete name
line should not include the path information, only the File name
.
@Echo Off
SetLocal EnableExtensions
Set "MIExe=%UserProfile%\Standalone\MediaInfo\MediaInfo.exe"
Set "OutExt=nfo"
Set "HR=------------------------------------------------------------------"
For %%G In ("%~1") Do If "%%~aG" Lss "d" (If "%%~aG" GEq "-" (
Echo Error! File arg.
GoTo EndIt) Else (Echo Error! Invalid directory arg.
GoTo EndIt)) Else If "%%~dG" == "" (
Echo Error! Full directory path required.
GoTo EndIt)
If Not Exist "%MIExe%" (Echo Error! MediaInfo not found.
GoTo EndIt)
PushD "%~1"
Set "ExtLst="
For %%G In (aac ac3 aifc aiff ape asf au avi avr dat dts flac iff ifo irca m1v
m2v mac mat mka mks mkv mov mp2 mp3 mp4 mpeg mpg mpgv mpv ogg ogm paf pvf qt
ra rm rmvb sd2 sds vob w64 wav wma wmv xi) Do If Not Defined ExtLst (
Set "ExtLst=".":"*.%%G"") Else Call Set "ExtLst=%%ExtLst%% ".":"*.%%G""
Set "}=%PATHEXT%" && Set "PATHEXT="
%SystemRoot%\System32\where.exe /Q %ExtLst%
If ErrorLevel 1 (Echo Error! Directory has no supported files.
GoTo EndIt)
Set "i="
(For /F "Delims=" %%G In ('%SystemRoot%\System32\where.exe %ExtLst% 2^> NUL'
) Do (If Defined i Echo %HR%
"%MIExe%" "%%~nxG"
Set "i=T")) 1> "%~nx1.%OutExt%"
:EndIt
%SystemRoot%\System32\timeout.exe /T 3 /NoBreak 1> NUL
GoTo :EOF
You can change the location of mediainfo.exe
on line 4
, and the output extension, on line 5
; but nothing else should be modified.
You should also note that I've added some input validation to this script. If your input directory is not a fully qualified directory path containing supported files for an existing mediainfo executable, it will report an error and close.