Pergunta

I have given myself a simple task of writing a batch file that will do the following:

Compare CRC32 of each file in "first" folder with each file in "second" folder. Output needs to be the name of every file from the "first" folder, which doesn't have its duplicate in "second".

Note: "CRC32.exe -nf" outputs CRC32 in first line, and file size in second one.

Here is how I tried to do it:

@echo off

for %%i in ("%cd%\first\*.*") do (
    set uniq=0
    for /f %%x in ('crc32 %%i -nf') do (
        set one=%%x
        goto find_two
    )
    :find_two
    for %%j in ("%cd%\second\*.*") do (
        for /f %%x in ('crc32 %%j -nf') do (
            set two=%%x
            goto compare
        )
        :compare
        if one==two (
            goto next
        ) else (
            set uniq=1
        )
    )
    if uniq==1 (
        echo %%i >>result.txt
    )
    :next
)

I assume there are several errors present in this code, but I had trouble finding them. So if anyone has time, and thinks he can help, I would be grateful.

If you think different approach is required, feel free to show it.

Foi útil?

Solução

There are two major problems.

Goto's inside brackt doesn't work as expected, they stop the loop immediately, so your code will simply fail there.
The solution is to avoid goto or to call a function and use the goto there.

The second problem are the compares.
if one==two ( will be always false, as it compare the word one against the word two, but not the content of the variables.

Normally you would use if "%one%"=="%two%" echo equal, but as it should work inside of brackets you need delayed expansion, so it looks like.

if !one!==!two! echo equal

Don't forget to enable the delayed expansion before you start the loop with

setlocal EnableDelayedExpansion

Outras dicas

Might be easier to capture the output of crc32 using something like this:

for /f %%x in ('crc32 "%filename%" -nf ^| find /v "CRC32"') do stuff with %%x

Can you show me the full output of an example crc32 if this is not helpful?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top