Based on my script below, I want delete file older than 3 days and create log list of files deleted or not. But log output is not working (not like I want), it should write log like :
if files exist :
yyyy/mm/dd hh:mm:ss <file location and name>
if not :
yyyy/mm/dd hh:mm:ss There is no files deleted.
Only if true work, the else is not working. Can anyone help? Oh, and never mind about date/time script, its only for reference.
@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