检测批处理文件中文件夹中的两个文件

时间:2012-07-11 11:57:24

标签: batch-file

我需要一个代码来检测是否有两个具有相同扩展名的文件,我需要它来执行不同的代码。

  1. 检测文件夹是否是我需要的两个* .txt文件

  2. Echo此文件夹中有两个文本文件,请删除一个并继续

  3. 但如果只有一个文本文件,则保持代码运行

    我目前正在使用此代码来检测文件夹中是否有任何文件

    If not exist Files\*.txt echo there is no lyrics
    

    也许我们可以把这个结合起来像

      if text file equal to 0 echo no lyrics
    
      if text file equal to 1 echo.
    
      if text file Gtr 1 ech there is more than on text file in the folder
    

    代码将两次用于文本文件,另一次用于.ogg音频文件

    谢谢

1 个答案:

答案 0 :(得分:2)

DIR /B列出文件,每行一个,没有页眉或页脚信息

FIND /C计算匹配行数,/V ""匹配任何行

FOR /F处理命令的结果

for /f %%N in ('dir /b "Files\*.txt" ^| find /c /v ""') do (
  if %%N equ 0 echo No lyrics
  if %%N equ 1 echo(
  if %%N gtr 1 echo There is more than one text file in the folder
)