【问题标题】:fc to loop through files to see if any files matchfc 遍历文件以查看是否有任何文件匹配
【发布时间】:2017-11-22 17:16:04
【问题描述】:

我的目标是将一个 txt 文件的内容与特定文件夹中所有文本文件的内容进行比较,如果 ANY txt 文件匹配,则执行一组进程,如果没有匹配则执行另一套。

我尝试过以下操作:

@echo off
:main
fc "C:\Users\degibson\Desktop\test\*.txt" "C:\Users\degibson\Desktop\test2\temp1.txt" > NUL
if errorlevel 1 goto error

:next
echo do some important task
pause

:error
echo do some other important task
pause

问题 - 仅当所有文件都与 temp1.txt 内容匹配时才有效。

有没有更好的方法来完成这项任务?

【问题讨论】:

    标签: loops batch-file


    【解决方案1】:

    您可以使用FOR 命令迭代目录中的所有文本文件。然后,我建议您使用CALL 命令而不是GOTO,因为CALL 会将执行返回到FOR 命令,而GOTO 将中断FOR 命令的执行。执行返回到FOR命令,使用内置的:EOF标签结束当前调用的进程。你也可以使用EXIT /B。

    @ECHO OFF
    
    :main
    FOR %%G IN (C:\Users\degibson\Desktop\test\*.txt) DO (
        fc "%%G" "C:\Users\degibson\Desktop\test2\temp1.txt" >NUL
        if errorlevel 1 (
            CALL :error
        ) ELSE (
            CALL :next
        )
    )
    GOTO :EOF
    :next
    echo do some important task
    pause
    GOTO :EOF
    
    :error
    echo do some other important task
    pause
    GOTO :EOF
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2013-11-30
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      相关资源
      最近更新 更多