使用今天的修改日期搜索目录

时间:2015-01-14 02:13:25

标签: batch-file

我刚开始学习如何在批处理文件中编码。我将非常感谢帮助我的批处理文件的以下要求。

我做了一些研究,发现这个链接(Is there a file in a directory with a modified date of today - Batch File)与我想要的方式类似。

Are there files in a directory with a modified date of today

If yes (may have more than one file with the modified date of today)
   Copy the files into the folder
else
   echo no file found

else
NO file for today

我在链接中尝试解决方案,但该解决方案只设法返回一个文件。例如,有两个文件即test_20150114和testString_20150114,解决方案只会将文件testString_20150114复制到该文件夹​​中。我想将文件test_20150114和testString_20150114复制到该文件夹​​中。我怎样才能实现它?

我尝试使用两个for循环来实现但不知何故失败。

这里我从其他网站上获取的代码与我的要求非常相似,

for /f "tokens=2" %%I in ("%date%") do set today=%%I
for /f "tokens=5" %%H in ('dir /a-d ^| findstr /v /i "%~nx0$" ^| find "test"') do (
for /f "tokens=4*" %%H in ('dir /a-d ^| findstr /v /i "%~nx0$" ^| find "%today%"') do (

rem record success for later
set found=1

rem search file %%I for "test" (case-insensitive).
find /i "string" "%%I">NUL 

rem Was last command successful?
if %ERRORLEVEL%==0 (
    echo test Files Found for today
If %%H GTR 0 (
  echo Found %%I file is greater than 0kb 
)
) else (
    echo test string NOT found
)
)
)
for /f "tokens=5" %%H in ('dir /a-d ^| findstr /v /i "%~nx0$" ^| find "testString"')do(

for /f "tokens=4*" %%H in ('dir /a-d ^| findstr /v /i "%~nx0$" ^| find "%today%"') do (

rem record success for later
set found=1

rem search file %%I for "testString" (case-insensitive).
find /i "string" "%%I">NUL 

rem Was last command successful?
if %ERRORLEVEL%==0 (
    echo testString Files Found for today
If %%H GTR 0 (
  echo Found %%I file is greater than 0kb 

)
) else (
    echo test string NOT found
)
)
)

*编辑:我设法在Serenity的帮助下获得此解决方案

  forfiles /s /m  *.* /d 0 /c "cmd /c 
  if @fsize==0 Echo @file is 0Kb || Copy @file D:\Test

添加问题:在设法找到今天的日期文件后,我想将文件的内容复制到新文件中。 (Test.txt内容将复制到Result.txt)

1 个答案:

答案 0 :(得分:0)

forfiles /d 0

列出今天修改的文件。

 Forfiles /d 0 && Echo copy etc || Echo File Not Found

虽然ForFiles可以在找到的文件上执行它自己的命令并打印它自己的消息File Not Found。

forfiles /d 0 /p c:\windows /m *.ini /s /c "cmd /c copy ^"@path^" c:\somewhereelse\"

编辑:将cmd / c添加到命令行