Search code examples
batch-filecmd

Cannot get the value and assign to a variable


I have try to use the following to get the MD5 for the txt. Output it to other file and rescan. dumb method but seem it return nothing every time.

(certutil -hashfile %targetfolder%\%baseName%%n%\inventory_2.0.1.bck MD5 | find /v "hash" | find /v "") > rootfs_md5
set /p texte =< D:\script\rootfs_md5
echo %texte%

Also try the following method without temp file but seem also return null

for /f %%i in (certutil -hashfile %targetfolder%\%baseName%%n%\inventory_2.0.1.bck MD5 | find /v "hash") do set VAR=%%i
echo %VAR%

any reason for that


Solution

  • Here's examples for each of the methods you attempted.

    Using a temporary file:

    @Set "FileHash="
    @%SystemRoot%\System32\certutil.exe -HashFile "%targetfolder%\%baseName%%n%\inventory_2.0.1.bck" MD5 2>NUL | %SystemRoot%\System32\find.exe /V ":" 1>"D:\script\rootfs_md5"
    @If Not ErrorLevel 1 Set /P "FileHash=" 0<"D:\script\rootfs_md5"
    @If Defined FileHash Del "D:\script\rootfs_md5" & Echo %FileHash: =%
    

    Without temporary file:

    @Set "FileHash="
    @For /F "Delims=" %%G In ('%SystemRoot%\System32\certutil.exe -HashFile "%targetfolder%\%baseName%%n%\inventory_2.0.1.bck" MD5 2^>NUL ^| %SystemRoot%\System32\find.exe /V ":"') Do @Set "FileHash=%%G"
    @If Defined FileHash Echo %FileHash: =%