尝试为校验和文件创建验证过程

时间:2017-03-22 15:18:58

标签: batch-file file-io windows-7

我的网络上有两个不同位置的校验和文件,我需要验证它们是否相互匹配,如果不相同则输出** UNVERIFIED **。所以需要做的是我需要一次将两个文件读入一个变量,然后相互检查变量以进行验证。

我尝试过的事情:

:verify_integrity
    set "verf_integrity_src_file=\\ROCIO-148084\c$\Users\z-perkins-thomas\Documents\bin\batch\verf"
    set "verf_integrity_dst_file=%cd%\docs\checksum.md5"
    pushd "%verf_integrity_src_file%"
    setlocal enabledelayedexpansion
    set content_src=

    for /f "delims=" %%x in (%verf_integrity_src_file%\checksum.md5) do (set content_src=!content_src! %%x)

    endlocal
    setlocal enabledelayedexpansion
    set content_dst=

    for /f "delims=" %%y in (%verf_integrity_dst_file%) do (set content_dst=!content_dst! %%y)

    endlocal

    @echo %content_dst%==%content_src%

这将不输出任何内容 - > " =="

其次,我尝试过这样的方法:

:verify_integrity
    set "verf_integrity_src_file=\\ROCIO-148084\c$\Users\z-perkins-thomas\Documents\bin\batch\verf"
    set "verf_integrity_dst_file=%cd%\docs\checksum.md5"
    pushd "%verf_integrity_src_file%"
    set verf_src=
    for /f "delims=" %%x in (%verf_integrity_src_file%\checksum.md5) do set verf_src=%verf_src% %%x
    set verf_dst=
    for /f "delims=" %%y in (%verf_integrity_dst_file%) do set verf_dst=%verf_dst% %%y
    @echo %verf_src%
    goto :verify_argv

这将输出校验和文件中的最后一行 - > 22534f499bc7755d58412327853c8d9a * ./ zip-archive / utilis0.1.zip

所以我的问题是,如何将这两个文件读入内存,并验证它们是否相互匹配?

1 个答案:

答案 0 :(得分:4)

要检查两个文件是否相同,请使用fc

如果您确定,这两个文件都存在,它就像以下一样简单:

fc "file1.ext" "file2.ext" >nul || echo ** UNVERIFIED ** 

如果没有,请明确检查%errorlevel%

-1 Invalid syntax (e.g. only one file passed) 
0 The files are identical.
1 The files are different.
2 Cannot find at least one of the files.