3

My batch skills are limited. To produce results for many files, I use DIR /a-d /s /b > dirlist.txt. I import dirlist.txt into Excel (column A), and use that to produce file-by-file commands (in column B) that I can then copy into a batch file. It's more cumbersome than looping, but at least I understand it.

The present situation calls for CRC32 calculation. I have a command-line tool that expresses only the CRC32 value, without the corresponding filename. Hence, my Excel solution outputs the filename (i.e., ECHO D:\[full path].ext >> output.txt) before calculating the CRC32 value (i.e., ampersand (&) followed by CRC32.exe "D:\[full path].ext" >> output.txt). In other words, the spreadsheet produces a single command that states the filename and then the CRC32 value.

Unfortunately, this puts the filename on one line, in output.txt, followed by the CRC32 value on the next line. I think it is possible to get the two on the same line, but this situation seems more complicated than the simple examples I'm seeing in tutorials. So the question is, is there a relatively straightforward way to state the filename and the CRC32 value in a single line of output?

4
  • Are you using the concatenate function in Excel to join your file name and checksum? Or are you doing it via VBA? May 23, 2019 at 9:30
  • Column B in Excel is just the formula indicated above. It's really long, with the full pathnames, but in effect it's ="ECHO "&[full path from column A]&".ext & CRC32 "&[full path]&".ext >> output.txt." That produces a batch command consisting of ECHO and CRC32 sections. Ampersands in that pseudocode perform different functions: batch, if enclosed in quotes, Excel concatenation if not. May 23, 2019 at 16:52
  • I return to this question 2.5 years later with an upgraded interest in finding a Windows-compatible SHA-512 executable that would function as CRC32.EXE does, in the answers provided below. It seems I may have to use Cygwin or git-bash. Suggestions welcome. See raywoodcockslatest.wordpress.com/2021/12/08/ransomware-hash Dec 8, 2021 at 6:59
  • Jool (superuser.com/a/1246749/124687) advances toward addressing that need, but that solution (a) doesn't state the filename and path on the output line and (b) is written to calculate the hash for just one file, not for multiple files listed in a DIRLIST.TXT input file. Dec 8, 2021 at 7:14

2 Answers 2

4

If you're not entirely opposed to a full batch solution, this should work for you:

@echo off

set "list=dirlist.txt"
set "out=output.txt"

dir /a-d /s /b > %list%

setlocal enabledelayedexpansion
for /f "tokens=*" %%A in (%list%) do (
    set "path=%%A"
    for /f %%B in ('CRC32.exe "!path!"') do (
        set "calc=%%B"
        echo !path! + !calc! >> %out%
    )
)

We set a variable for both the dirlist.txt and output.txt to make it easier to update those with fixed paths if you ever want to, and your DIR command is unchanged. For the loops, we setlocal enabledelayedexpansion so we can use in-loop variables; our initial loop takes a look at every line (token=*) of your dirlist.txt and assigns it to parameter %%A, which we set as our path variable. Next we have our nested loop that executes CRC32.exe with our !path! variable as the argument and assigns the output to parameter %%B, then sets that as the calc variable. Lastly, we can echo !path! and !calc! on the same line of our output.txt. In the example above I separate them with a + (based on the title of your question) but you can use whatever you want, or nothing at all - just remember you may need to escape a special character with ^.
I'd like to note that I do not have CRC32.exe, so the syntax of the command in the for-loop is based on my understanding of your question. Hopefully this helps!

1
  • Some of my comments on the answer by somebadhat apply here as well. For some reason, the path to C:\Windows\crc32.exe has to be specified here. Somebadhat correctly identified the download source of crc32.exe; I should have stated it. This code has the advantage of allowing me to comment out the DIR line and pre-supply a dirlist.txt that may contain a random list of file paths and names. I also appreciate the batch mini-tutorial. I seem to have a learning block on batch loops. Dec 8, 2021 at 6:51
2

For all of the files in a directory and it's sub-directories calculate their hash / checksum with crc32.exe and output the fully qualified path name followed by the hash value on the same line to a text file.

Download crc32.exe and put it in your Windows folder.

@echo off
setlocal enableextensions
cd.> dirlist.txt
for /F "eol=| tokens=* delims=" %%g in ('dir *.* /S /A-D /B 2^>nul') do (
for /F %%h in ('%systemroot%\crc32.exe "%%g"') do (
echo %%g + %%h >> dirlist.txt
)
)
exit 
rem 
1
  • I'm using dirlist.txt as the input, so I replace "dirlist.txt" in this code with "output.txt." In case the clever c.> line eludes me again, I replace it with the more verbose "if exist output.txt del /f output.txt." With %systemroot% (i.e., C:\Windows) in path, and with crc32.exe in C:\Windows, I can get by without the path statement here, referring merely to crc32.exe. I can also dispense with the "exit" and "rem." I reverse %%g and %%h in the output line, so the left column is uniform for easier spreadsheet parsing. With those tweaks, this works and I understand much of it. Dec 8, 2021 at 6:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .