How to capture the result of a command and store in a MS-DOS variable?

Asked

Viewed 179 times

-2

I am developing a script and need to check if a file has been converted before. I decided to create a database saving file checksum, and before converting the next file I check its checksum and compared in the database if it has already been converted, if it has already been converted I discard it and step to the next.

The point is that I want to do this through Windows CMD. So I’m using FCIV.EXE

When I run fciv.exe a.mp4

Call me back

//
// File Checksum Integrity Verifier version 2.05.
//
c6928bfc8e1313c057f57f17eaecd775 a.mp4

I would like to result in the variable only checksum c6928bfc8e1313c057f57f17eaecd775

I would like to put the result in a variable to continue to use in the script, how to do this?

If I run fciv.exe a.mp4 -xml db.xml a database is created in xml, but what I wanted was to create a variable with the result containing the checksum of the file in this variable created for me to use in the course of the script.

I tried something like fciv.exe a.mp4 %checksum%but it didn’t work out.

I also tried to use a for /f with information in another question made here on the subject but did not result.

cd /d "d:\videos"
for /f "delims=" %f in (' @"D:\videos\fciv.exe" a.mp4 ')
echo %f%

When I run this . bat returns:

d:\Videos>checksum.bat
'checksum.bat' não é reconhecido como um comando interno
ou externo, um programa operável ou um arquivo em lotes.

Or is there any other way to get the checksum without using fciv? that can make the service easier?

Updating:

I was able to do it this way:

fciv.exe a.mp4 >> checksum.txt
for /f "usebackq skip=3 tokens=1 delims= " %%G in ("D:\videos\checksum.txt") DO echo %%G
del checksum.txt

usebackq skip=3 Skip the first 3 lines recorded in . txt

delims= "

Use the space to separate the string.

Now it’s left to figure out how to run the fciv.exe a.mp4 within the for /f so you don’t have to go through . txt

  • I even tended to follow the guidelines of this other post but could not result.

  • I believe that the ideial here would be to use the token, so could the checksum in a variable and file name in another variable, but I keep getting error. for /f "tokens=1,2 delims=; " %%G in (' @"D:\videos\fciv.exe" "a.mp4" ')

  • The best result I got so far was passing the checksum result to a file . txt and reading a file . txt with for. for /f "usebackq skip=3 tokens=1 delims= " %%G in ("D:\videos\t.txt") DO echo %%G The question now is, how to run the program inside it if you need to pass to . txt?

1 answer

1

@echo off 

title <nul & title .\%~nx0
setlocal & cd /d "D:\videos" 

set "_hash=%cd%\mp4_hash.txt"
set "_ff=%ProgramFiles%\FFmpeg\bin\ffmpeg.exe"

if not exist "%_hash%" >"%_hash%" (
     "%~dp0fciv.exe" "D:\videos" -type *.mp4 | find/v "/" 
    )else for %%i in (*.mp4)do call :^) "%%~i" "%_hash%" "%_ff%"

:^)
if "%~1" == "" endlocal && goto=:EOF
for /f tokens^=1-2 %%i in ('^^^< nul "%~dp0fciv.exe" "%~f1" ^|find/v "/"
')do echo=%%~i:"%%~j" & 2>nul %__AppDir__%findstr.exe %%~i "%~2">nul && exit/b 

"%~3" -hide_banner -v error -stats -i "%~1" -c:v libx265 -c:a copy "%~dpn1_tmp%~x1"

2>&1 move/y "%~dpn1_tmp%~x1" "%~1">nul && ("%~dp0fciv.exe" "%~f1" |find/v "/">>"%~2"
     echo=Arquivo: %~nx1 & echo\Convertido ^& MD5 adicionado na base "%~2" && exit/b
    ) || %__AppDir__%timeout.exe /t 5 | echo=Arquivo: "%~dpnx1" Falhou!... && exit/b

Assuming:

1. That your base file will be created in a first run

2. That your base.txt file will be created in the folder where you have your.mp4 files

3. That.mp4 Files have previously been converted to their base

4. That the base is always fed after your new files are converted

5. That same batch scans and does the conversion of your.mp4 files

6. That the executable FCIV.exe is in the same folder as your batch


rem :: entre na pasta de seus arquivos mp4 :: 
cd /d "D:\videos" 

rem :: defina o arquivo para salvar as strings MD5 de seus .mp4 :: 
set "_hash=%cd%\mp4_hash.txt"

rem :: defina o caminho para seu conversor :: 
set "_ff=%ProgramFiles%\FFmpeg\bin\ffmpeg.exe"


rem :: se não existir a base (1º execução) salve os MD5 :: 
if not exist "%_hash%" >"%_hash%" (
     "%~dp0fciv.exe" "D:\videos" -type *.mp4 | find/v "/" 
    )else ... ::
          rem :: se existir a base para todos os mp4 chame o label :^) passando :: 
          rem :: argumento 1 == caminho\file.mp4
          rem :: argumento 2 == caminho\base_hash == _hash=%cd%\mp4_hash.txt"
          rem :: argumento 3 == caminho\FFmpeg\exe == _ff=%ProgramFiles%\FFmpeg\bin\ffmpeg.exe

    )else for %%i in (*.mp4)do call :^) "%%~i" "%_hash%" "%_ff%"

:^)
rem :: quando na excução nenhum argumento for passado para o :label, dai ele ::     
rem :: vai para o final do arquivo e já parando/terminado a execução do bat  :: 
if "%~1" == "" endlocal && goto=:EOF

rem :: esse loop extrair o MD5 do arquivo em loop passado no argumento %1 :: 
for /f tokens^=1-2 %%i in ('^^^< nul "%~dp0fciv.exe" "%~f1" ^|find/v "/"

rem :: ecoando MD5 e nome para acompanhamento, também procurando na base essas strings ::
rem :: e para caso de encontrar, o arquivo ja foi convertido e restando sair do label  :: 
')do echo=%%~i "%%~j" & %__AppDir__%findstr.exe %%~i "%~2">nul && exit/b 

rem :: para casos de não ter encontrado o MD5 do mp4 em loop na sua base, o seu     :: 
rem :: conversor fara a conversão do mp4 ("%~1") e gerando um arquivo temporario,   ::
rem :: adicionando "_tmp" no nome e antes da extenção .mp4 (caminho\file+_tmp+.mp4) ::
"%~3" -hide_banner -v error -stats -i "%~1" -c:v libx265 -c:a copy "%~dpn1_tmp%~x1"


rem :: se o seu arquivo temporario foi criado, ele será movido já sobrescrevendo o :: 
rem :: mp4 original e imediatamente o novo MD5 sera adicionado na sua base >>"%~2" :: 
2>&1 move/y "%~dpn1_tmp%~x1" "%~1">nul && ("%~dp0fciv.exe" "%~f1" |find/v "/">>"%~2"

rem :: uma mensagem informará a conversão e alimentação do banco, e logo saindo do :label :: 
     echo=Arquivo: %~nx1 & echo\Convertido ^& MD5 adicionado na base "%~2" && exit/b

rem :: caso as ações de converter/mover o arquivo não retornem 0, o operador || vai ::
rem :: executar o timeout de 5 segundos já redirecionado para uma mensagem de erro  ::
rem :: omitindo o contador e ao finalizar esse timeout ele sai do label continuando ::
rem :: as execuções com os outro arquivos .mp4 em loop, e exibindo o erro do FFmpeg ::
    ) || %__AppDir__%timeout.exe /t 5 | echo=Arquivo: "%~dpnx1" Falhou!... && exit/b

Remarks:

to) The Ffmpeg (%_ff% line 5 | %~3 line 16) serves to illustrate/suggest where to use conversion

b) It is possible to have this result just by using Ffmpeg to get the MD5 of files

c) It is also possible add and/or consult Metadata comment=Convertido without using a base

d) The FCIV.exe is suitable for use with any tool that handles arquivos.xml

and) Use the Certutil.exe which already comes with system to get the MD5, do not need to download the FCIV.exe

Certutil -hashfile "D:\videos\arquivo.mp4" md5|find/v "."

Responding:

  • To save a variable by looping MD5 from FCIV.exe:
rem :: salvando apenas o MD5 :: 
for /f %%i in ('^^^< nul "fciv.exe" "arquivo.mp4" ^|find/v "/"
')do set "_md5=%%~i"

rem :: salvando o MD5 e o nome do arquivo :: 
for /f tokens^=1* %%i in ('^^^< nul "fciv.exe" "arquivo.mp4" ^|find/v "/"
')do set "_md5_nome=%%~i %%~j"

rem :: salvando o MD5, o nome do arquivo e o caminho completo :: 
for /f tokens^=1* %%i in ('^^^< nul "fciv.exe" "%CD%\arquivo.mp4" -wb ^|find/v "/"
')do set "_md5_nome=%%~i %%~j"

  • To save a variable by looping MD5 from the base.txt file:

Obs.: If created using: >"arquivo_base.txt" "fciv.exe" "arquivo.mp4" ^|find/v "/"

rem :: salvando apenas o MD5 :: 
for /f %%i in ('type arquivo_base.txt')do set "_md5=%%~i"

rem :: salvando o MD5 e o nome do arquivo :: 
for /f tokens^=1* %%i in (''type arquivo_base.txt'')do set "_md5_nome=%%~i %%~j"

rem :: salvando o MD5, o nome do arquivo e caminho completo :: 
for /f tokens^=1* %%i in ('type arquivo_base.txt')do set "_md5_path_name=%%~i %%~j"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.