batch-filecertutil

Windows Batch file - Format CertUtil output and filename and size in single line


My requirement is to create an output file with the following format: Filename,file size,checksum

An example would be abc.tar,1024 Bytes,052107adc8c42d6cf581bf81225ae6de

Code

setlocal enabledelayedexpansion  
set OUTFILE="C:\Script\Batch_OUT.txt"

echo %OUTFILE%

echo Extracting Batch records to %OUTFILE% ...
echo pwd = `pwd`

cd C:\Script\TARS\

for %%f in (Batch_*.tar) do (  
(echo %%~nxf && echo %%~zf Bytes && certutil -hashfile "%%f" MD5 | find /V    ":") >>%OUTFILE%  
) 

pause 2m

Output

Batch_one.tar 
778240 Bytes 
052107adc8c42d6cf581bf81225ae6de

Expected Outcome

Batch_one.tar,778240 Bytes,052107adc8c42d6cf581bf81225ae6de

Solution

  • You can use the same trick as in the post, David Winder already linked in a comment:

    ...
    for %%f in (test.txt) do (  
     (
       <nul set /p ".= %%~nxf,%%~zf Bytes," 
       certutil -hashfile "%%f" MD5 | find /V ":"
     ) >>%OUTFILE%  
    ) 
    ...