-2

I am writing a Batch file that traverses through an entire directory (including all sub-directories) and prints checksum of each file it finds into a checksum.txt text file.

It seems that both ECHO statements which I've made bold return null values of the variables count and var. Eventually this means that the resulting checksum.txt file that is created is empty.

setlocal ENABLEDELAYEDEXPANSION

For /R "C:\Users\MyName\Desktop\SourceDir" %%G in (*) do (
SET /A count=1
    FOR /F "tokens=* USEBACKQ" %%F IN (`certutil -hashfile "%%G" sha256`) DO (
            SET var!count!=%%F
            SET /A count=!count!+1

echo ****************************************** %count% %var1%
        )
echo ****************************************** %var1%
SET FILECHECKSUM=%var1% %var2%

>>"CHECKSUM.TXT" echo(%FILECHECKSUM%

)

timeout /t -1
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • 2
    you seem to know about [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028), so why aren't you using it consequently? – Stephan Apr 28 '20 at 13:25
  • `SET /A count=!count!+1` --> `set/a"count+=1"` – ScriptKidd Apr 28 '20 at 13:28
  • 2
    I would suggest to use `>>"CHECKSUM.TXT ( for ... do ( for ... do ( ... ... ) ) )` as it opens file only once. in your code, file is opened and closed in every `for` iteration. this will sped up execution, specially if there are many files to parse. – elzooilogico Apr 28 '20 at 14:44

1 Answers1

0

Thank you Stephan, HackingAddict1337 and Squashman.

I've managed to get it working using the below code now.

setlocal ENABLEDELAYEDEXPANSION

For /R "C:\Users\MyName\Desktop\SourceDir" %%G in (*) do (
SET /A count=1
    FOR /F "tokens=* USEBACKQ" %%F IN (`certutil -hashfile "%%G" sha256`) DO (
            SET var!count!=%%F
            SET /a count=!count!+1
echo xxxxxxxxxxxxxxx !count! !var1!
        )

SET FILECHECKSUM=!var1! , !var2!

>>"CHECKSUM.csv" echo(!FILECHECKSUM!

)


timeout /t -1