2015-01-14 41 views
0

我刚开始学习如何在批处理文件中编码。我希望能帮助我的批处理文件符合以下要求。搜索今日修改日期的目录

我已经做了一些研究,发现这个链接(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 
) 
) 
) 

*编辑:我设法得到帮助宁静的

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

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

+0

您可以包括您的实际工作不代码? – NiematojakTomasz

回答

0
forfiles /d 0 

列表文件今天修改。

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

虽然FORFILES可以执行它在找到的文件自己的命令,并打印它自己的消息文件未找到。

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

编辑:新增CMD/C命令行

+0

感谢您的回答。除了搜索今天有日期的文件之外,我还想检查文件是否大于0KB,我该如何实现? – missypooh

+0

'forfiles/d 0/c“cmd/c if @ fsize == 0 echo hi' – Serenity

+0

如果您试图复制一个零字节文件,它将被删除,这就是'Copy'所做的。 – Serenity