【问题标题】:Batch file for listing all files from a date interval用于列出日期间隔内所有文件的批处理文件
【发布时间】:2014-08-19 23:13:39
【问题描述】:

我需要做一个批处理文件,按日期间隔对所有文件进行排序,例如:

@echo off
echo Input the date(dd/mm/yyyy):
set /p compDate=

::After that I will compare from the actual day (%date%), example:

set interval = %compDate% - %date%... *Something like that*

::After that I need to list all files from a specific directory, example:

echo Input the directory:
set /p directory=
SET Exit= %UserProfile%\Desktop\test.txt

::After that I might need dir /tc to get the creation date, example:

pushd "%directory%"
dir /s /tc /a-d > %Exit%

::After that I don't know how to get only the lines which are in date interval, example:

今天是 2014 年 8 月 19 日,但我想搜索从 2014 年 7 月 10 日创建的所有文件。 所以我必须复制所有日期为 10/07/2014、11/07/2014、12/07/2014 等的行,直到今天创建的文件停止。

我尝试使用findstr,但我无法设置日期间隔,只能在创建的 .txt 中搜索特定日期。

有人知道怎么做吗?

【问题讨论】:

  • forfiles 命令用于执行此操作。尝试使用它。 XCopy 也可以做到(它可以列出要复制的文件而不复制它们)。两者都允许按日期选择。输入xcopy /?forfiles /?
  • 批处理中没有日期/时间计算程序或函数。
  • forfiles /d +26/2/2014 /p c:\windows /m *.txt /c "cmd /c echo @fpath @fdate

标签: date batch-file batch-processing intervals


【解决方案1】:

如果我正确理解了请求,您确实不希望在给定时间间隔内创建文件,而是在给定日期之后创建文件。下面的批处理文件假设系统使用的日期以 DD/MM/YYYY 的顺序出现:

编辑对cmets的一些修改

@echo off
setlocal EnableDelayedExpansion

echo Input the date(dd/mm/yyyy):
set /p compDate=
for /F "tokens=1-3 delims=/" %%a in ("%compDate%") do set compDate=%%c%%b%%a

echo Input the directory:
set /p directory=
SET Exit=%UserProfile%\Desktop\test.txt

pushd "%directory%"

(for /F "tokens=1-5*" %%a in ('dir /s /od /tc /a-d') do (
   set "fileDate=%%a"
   if "!fileDate:~2,1!!fileDate:~5,1!" equ "//" (
      for /F "tokens=1-3 delims=/" %%x in ("!fileDate!") do set fileDate=%%z%%y%%x
      if !fileDate! geq %compDate% (
         set "fileSize=               %%e"
         echo %%a  %%b %%c %%d  !fileSize:~-16! %%f
      )
   )
)) > %Exit%

popd

【讨论】:

  • 是的,我想要一个在该日期或给定日期之后创建的所有文件的列表,在我的系统中已经出现 DD/MM/YYYY,但是这个批处理文件不会生成 *.txt列表,并且 'dir' 中的 /b 不显示创建日期。
  • 为了创建一个带有结果的文本文件,只需使用(for /F ... do ( ... )) > %exit_file%;对于dir 显示的创建日期,只需修改批处理文件以显示来自dir 本身而不是%%~Ta 的数据输出。我专注于“日期间隔”问题......
  • 我向你展示了如何使用 forfiles 来做到这一点。太容易了。
  • @Noodles:是的,使用 forfiles 很容易,但它使用最后修改日期而不是创建日期,正如 OP 要求的那样!
  • 我是批处理文件的初学者,很抱歉,我会再试一次...谢谢!
【解决方案2】:

使用 WMIC 且独立于时间/日期设置的解决方案:

@echo off


setlocal

set /p compDate=Input the date(yyyymmdd): 
set /p directory=Full directory path (with no slash at the end): 
set exit_file= %UserProfile%\Desktop\test.txt
break>%exit_file%

for /f "tokens=1,2 delims=:" %%a in ("%directory%") do (
    set "dir_drive=%%~a:"
    set "dir_path=%%~b\"    
) 

set dir_path=%dir_path:\=\\%

setlocal enableDelayedExpansion

for /f "useback skip=1 tokens=1,2* delims=:" %%f in (`" wmic datafile where (drive='!dir_drive!' and path like '%dir_path%') get CreationDate^,name"`) do (
    set creation_date=%%f
    set creation_date=!creation_date:~0,8!
    set "file_name=%dir_drive%%%~g"

    if 1!creation_date! GTR 1%compDate% (
        echo  !file_name!>>%exit_file%
     )

)
exit /b 0 

【讨论】:

  • 执行批处理文件时,创建的*.txt是空的……不值得。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多