【问题标题】:Batch file giving count of files date wise批处理文件给出文件日期明智的计数
【发布时间】:2014-01-27 05:05:27
【问题描述】:

网络文件夹中有数百万个 HL7 文件。我想知道有多少文件进入网络文件夹。

我想创建批处理文件,它可以让我按日期计算 HL7 文件的数量。

例如

2014-01-27: 458 Files
2014-01-26: 987 Files
2013-10-17: 2308 Files

如果上述方法是不可能的,那么它也可以让我计算在文件夹下的特定日期创建/修改/访问的文本文件。

例如

如果我想知道今天创建的文件,它应该会给我一些类似这样的输出:

2014-01-27: 45458 Files

【问题讨论】:

标签: batch-file command-line command


【解决方案1】:

下面的批处理文件是初步版本;当您确认这是您想要的时,我们将对其进行修改。它假定dir 命令中显示的日期字段中的分隔符是破折号;如果不是,只需替换此行中的右侧分隔符:set fileDate=!fileDate:-=_!(等号之前)。

@echo off
setlocal EnableDelayedExpansion

for /F %%a in ('dir %1') do (
   set fileDate=%%a
   set fileDate=!fileDate:-=_!
   if "!fileDate!" neq "%%a" set /A Files[!fileDate!]+=1
)
set Files[

您可以在参数中插入/T开关,以选择所需日期的类型;详情请见dir /?

【讨论】:

  • 非常简洁 - 但您需要添加 /a-d 开关以避免计数目录
  • Aacini:这正是我想要的。谢谢 :) 是的,没有目录。
【解决方案2】:

我不想编辑 Aacini 的答案来添加这个,但下面的代码使用了他的算法,并且可能更健壮一些。

@echo off
setlocal EnableDelayedExpansion

for /F %%a in ('dir %1 /a-d ^|findstr "^[0-9]"') do (
   set fileDate=%%a
   set fileDate=!fileDate:/=_!
   set fileDate=!fileDate:-=_!
   set /A Files[!fileDate!]+=1
)
set Files[
pause

【讨论】:

    【解决方案3】:

    不如 Aacini 或 foxidrive 的答案快,只是添加一个选项以系统格式输出日期,并按日期排序输出。

    @echo off
        setlocal enableextensions enabledelayedexpansion
    
        set "count=0"
        set "previous="
        for /f %%f in ('dir "%~1" /a-d /tc /od ^| findstr /r /c:"^[^ ]"') do (
            if "!previous!"=="%%f" ( set /a "count+=1" ) else (
                if defined previous echo !previous! : !count!
                set "previous=%%f"
                set "count=1"
            )
        )
        if defined previous echo !previous! : !count!
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多