3

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
Neuron
  • 5,141
  • 5
  • 38
  • 59
BPrasad
  • 33
  • 3
  • No @DavidWinder I want to get the checksum also generated for the file. – BPrasad Sep 05 '18 at 13:43
  • Yes it is. Other problem, but same solution (writing without a `CRLF`). I "translated" it to your problem in an answer below. – Stephan Sep 05 '18 at 15:11

1 Answers1

2

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%  
) 
...
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thanks @Stephan and David.... that about snippet worked ...I was always assigning set /p to a variable and had issues around !! ...but the above code helped me with the task..Thank you – BPrasad Sep 06 '18 at 00:35
  • 1
    @RayWoodcock. a) You realize, `& echo:` produces a linebreak? (` – Stephan Apr 04 '22 at 09:39
  • Yes, echo: is not the problematic break. With your suggestion, everything looks good except the hash does not appear. In case you were wondering, I'd have posted a separate question, but they semi-suspended me for not getting enough upvotes on my questions, and I didn't find another source that explained it as clearly as you did. Thanks for trying to help. I'll definitely welcome further suggestions. This question is closed, so I've posted the current status elsewhere: https://raywoodcockslatest.wordpress.com/2022/04/03/dir-filename-hash-oneline/ – Ray Woodcock Apr 05 '22 at 13:21
  • 1
    You have a simple (copy and paste?) error: you don't have %%f. Replace it with %%A. Also you don't need the `set /p` trick, because all the desired information is present at the same time: `for %%A in (*) do (`, `for /f %%B in ('certutil -hashfile "%%A" SHA512 ^| find /V ":"') do echo %%~tA %%~zA %%B %%~fA`, `)` – Stephan Apr 05 '22 at 18:09
  • That makes sense. Basically, use the inner FOR loop to accumulate all desired items of file information; then the end of that loop adds the desired newline. This works. I will be updating my post shortly. You have no idea how happy I am to get this sorted out. It has been a bottleneck. Thank you again. – Ray Woodcock Apr 06 '22 at 00:15
  • 1
    Exactly. To write the output of the `certutil` without a linebreak with `set /p`, you need to capture the output with a `for /f` loop anyway. Then - why not use the captured string directly, instead of setting it to a variable to be able to use an awkward workaround.. – Stephan Apr 06 '22 at 06:51
  • New problem: the FOR loops seem very slow. I've been encouraged not to post further at StackOverflow, so the discussion continues elsewhere: https://www.tenforums.com/general-support/193553-batch-slow-loops-produce-one-line-file-info-date-hash-etc.html#post2413352 – Ray Woodcock Apr 20 '22 at 14:11
  • Try `(for /r %%A in (*) do for /f %%B in ('certutil -hashfile "%%A" SHA512 ^| find /V ":"') do echo %%~tA %%~zA %%B %%~fA)>%OUTFILE%` (a single redirect instead of redirecting each line on its own) – Stephan Apr 20 '22 at 14:47