批处理文件创建日志不起作用,为什么?

时间:2018-06-20 04:09:33

标签: batch-file

基于下面的脚本,我想删除3天以上的文件,并创建是否删除文件的日志列表。但是日志输出不起作用(不像我想要的那样),它应该写如下日志:

如果文件存在: yyyy/mm/dd hh:mm:ss <file location and name>

如果不是: yyyy/mm/dd hh:mm:ss There is no files deleted.

只有在真正可行的情况下,其他才能运行。有人可以帮忙吗?哦,别管日期/时间脚本,仅供参考。

@echo off
For /f "tokens=2-4 delims=/" %%a in ("%DATE%") do (
    set YYYY=%%c
    set MM=%%a
    set DD=%%b
)

For /f "tokens=1-4 delims=/:." %%a in ("%TIME%") do (
    SET HH24=%%a
    set MM=%%b
    set SS=%%c
    set FF=%%d
)

for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %%x
set fmonth=00%Month%
set fday=00%Day%

set datetime=%Year%/%fmonth:~-2%/%fday:~-2% %HH24%:%MM%:%SS% 
set num=forfiles /p "E:\data_integration\Data\LogFiles" /d -7 /c "cmd /c echo @fdate total : " | find /v ""
set search=forfiles /p "E:\data_integration\Data\LogFiles" /s /m *.* /d -7 /c "cmd /c"
set log_location=E:\data_integration\Data\LogFiles\LOG_DEL_HISTORY.txt
set log_delete=forfiles /p "E:\data_integration\Data\LogFiles" /s /m *.* /d -7 /c "cmd /c del @path & echo @path >> %log_location%"

echo | set /p = %datetime% >> %log_location%

if exist "%search%" (
    goto del
) else (
    goto notfound
)

:del
%log_delete%
goto exit

:notfound
echo | set /p ="There are no files deleted." >> %log_location%
echo. >> %log_location%
goto exit

:exit
echo | set /p = "Preparing to close..."
@echo off
echo. & echo. & echo.
rem set /p input="Press enter to exit..."
timeout /t 5

1 个答案:

答案 0 :(得分:0)

感谢你们给我所有建议,尤其是@Mark Dodsons,现在我的批处理脚本可以按我的意愿工作了。

这是我的最终批处理脚本,希望能为您遇到与我一样的问题:

@echo off
for /f "tokens=1-4 delims=/:." %%a in ("%TIME%") do (
    set HH24=%%a
    set MM=%%b
    set SS=%%c
    set FF=%%d
)

for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %%x
set fmonth=00%Month%
set fday=00%Day%

for /f "tokens=*" %%a in ('forfiles /p "E:\data_integration\Data\LogFiles" /s /m *.* /d -30 /c "cmd /c echo total : " ^|find /v /c ""') do set /a filescount=%%a-1

set datetime=%Year%/%fmonth:~-2%/%fday:~-2%   %HH24%:%MM%:%SS% 
set log_location=E:\data_integration\Data\LogFiles\LOG_DEL_HISTORY.txt
set log_delete=forfiles /p "E:\data_integration\Data\LogFiles" /s /m *.* /d -30 /c "cmd /c del @path & echo | set /p = %datetime% >> %log_location% & echo @path >> %log_location%"

echo | set /p = %datetime% >> %log_location%
echo  Program Run >> %log_location%

if %filescount% geq 1 (goto del) else (goto notfound)

:del
%log_delete%
echo | set /p = %datetime% >> %log_location% 
echo | set /p = "There are " >> %log_location% & echo | set /p = %filescount% >> %log_location% & echo | set /p = "files deleted." >> %log_location%
echo. >> %log_location%
goto exit

:notfound
set /a filescount=0
echo | set /p = %datetime% >> %log_location% 
echo | set /p = "There are " >> %log_location% & echo | set /p = %filescount% >> %log_location% & echo | set /p = "files deleted." >> %log_location%
echo. >> %log_location%
goto exit

:exit
echo | set /p = %datetime% >> %log_location%
echo  Program Complete. >> %log_location%
echo Preparing to close...
echo. & echo. & echo.
timeout /t 5