0

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
Rio Odestila
  • 125
  • 2
  • 19
  • Batch-files are not like bash. You cannot assign the output of a command to a variable with the `SET` command. – Squashman Jun 20 '18 at 04:51
  • well, even i dont assign the output of a command using SET command and just do it in IF command, still doesn't work. Have u a proper way to do it? – Rio Odestila Jun 20 '18 at 06:01
  • 1
    You could begin improving your script by replacing the first `20` lines with these `3`: `@Echo Off`,   `For /F %%A In ('WMIC OS Get LocalDateTime') Do If Not "%%~xA"=="" Set "ds=%%~nA"`   and   `Set "datetime=%ds:~,4%/%ds:~4,2%/%ds:~6,2% %ds:~8,2%:%ds:~10,2%:%ds:~-2%"`. – Compo Jun 20 '18 at 11:09
  • 1
    Possible duplicate of [Batch file to delete files older than N days](https://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days) – aschipfl Jun 20 '18 at 14:59
  • @Compo thanks for your advise but I prefer to used my date/time script because you can customize date/time format easily. My main problem is the IF command doesn't work like i want and I dont know what wrong with it. I try [Squashman] suggestion but still same. – Rio Odestila Jun 21 '18 at 01:22
  • @aschipfl I dont think is't duplicate 'cause my problem is different, I will change the title. – Rio Odestila Jun 21 '18 at 01:22
  • Setting the output of a command to a variable is actually easy just do `for /f “tokens=*” %%A in (‘command’) do (set var=%%A)` – Mark Deven Jun 21 '18 at 13:35

1 Answers1

0

Thanks for you guys that give me all that suggestions especially @Mark Dodsons, now my batch script work like I want.

Here is my final batch script, hopefully will help you that have same problem like me:

@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
Rio Odestila
  • 125
  • 2
  • 19