用于列出日期间隔中所有文件的批处理文件

时间:2014-08-19 23:13:40

标签: date batch-file batch-processing intervals

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

@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日,但我想搜索从10/07/2014开始创建的所有文件。 因此,我必须复制所有日期为10/07 / 2014,11 / 07/2014年12月12日等的行,直到停止今天创建的文件为止。

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

有人知道怎么做吗?

2 个答案:

答案 0 :(得分:1)

如果我正确理解了请求,您真的不想要在给定间隔中创建文件,而是在给定日期之后创建文件。下面的批处理文件假定系统使用的日期以DD / MM / YYYY顺序显示:

编辑作为回复评论的一些修改

@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

答案 1 :(得分:0)

使用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