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?

mael'
  • 1,946

2 Answers2

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!

mael'
  • 1,946
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 
somebadhat
  • 1,240