1

I have a simple program which deletes a file based on the extension, which is triggered by a Windows Task. I would like to create a log file which if ran would say successful or if it failed would say Delete Failed. I'm not sure how to create the log file for this.

@echo off
del "D:\BACnet-CSV\Test csv files\Test1\*.csv" 
IF %ERRORLEVEL% NEQ 0 goto ERROR
echo SUCCESSFUL
exit /b 0

:ERROR
echo Delete Failed
exit /b 1

Thanks

I've searched the Web and tried several things, just couldn't get it to show correctly.

yacc
  • 2,915
  • 4
  • 19
  • 33
  • Sorry - forgot to mentioned that I tried this to save it to a log file SET LOGFILE=D:\BACnet-CSV\Delete_Log_Files\log.txt 2>&1 – Clinton Langan Apr 06 '23 at 14:34
  • Could you explain what you're trying to delete? "a file" or all CSV files, "`*.csv`". – Compo Apr 06 '23 at 15:06
  • Just a single .csv file in a folder and create a log file when it was successful and if it fails. For example if there isn't a .csv file in the folder it should create an error – Clinton Langan Apr 06 '23 at 15:23
  • So what's wrong with: ```@(If Exist "D:\BACnet-CSV\Test csv files\Test1\*.csv" (Del /A /F /Q "D:\BACnet-CSV\Test csv files\Test1\*.csv" 1>NUL 2>&1 & If Exist "D:\BACnet-CSV\Test csv files\Test1\*.csv" (Echo Delete Failed& Exit /B 1) Else Echo SUCCESSFUL) Else Echo No Match) & Exit /B 0```? – Compo Apr 06 '23 at 15:50
  • It's not creating a log file – Clinton Langan Apr 06 '23 at 17:29
  • My example above doesn't attempt to write a log file! – Compo Apr 06 '23 at 19:34

1 Answers1

-1

You can check if the files have been deleted like so:

@echo off
set LOFILE=D:\BACnet-CSV\Delete_Log_Files\log.txt
echo %DATE% %TIME% >>%LOGFILE%

if not exist "D:\BACnet-CSV\Test csv files\Test1\*.csv" (
  echo NOTHING TO DELETE >>%LOGFILE%
  exit /b 0
)
del "D:\BACnet-CSV\Test csv files\Test1\*.csv"

if exist "D:\BACnet-CSV\Test csv files\Test1\*.csv" (
  echo FAILED >>%LOGFILE%
  exit /b 1
) else (
  echo SUCCESSFUL >>%LOGFILE%
  exit /b 0
)

DEL does not set ERRORLEVEL if it doesn't delete a file, so you have to work around this a little.

If you want to get rid of the deletion message, use

del "D:\BACnet-CSV\Test csv files\Test1\*.csv" 2>nul

You also need to make sure that your log directory exists, or else you won't see the logfile.

yacc
  • 2,915
  • 4
  • 19
  • 33
  • Thanks - what would be required to log this to a folder -- D:\BACnet-CSV\Delete_Log_Files – Clinton Langan Apr 06 '23 at 17:44
  • It already logs to the file `%LOGFILE%`, just set it up like you said before: `LOGFILE=D:\BACnet-CSV\Delete_Log_Files\log.txt` and log date/time with `echo %DATE% %TIME% >>%LOGFILE%` if you want. – yacc Apr 06 '23 at 18:32
  • Not sure want I'm missing, but I tried using echo %DATE% %TIME% >>%LOGFILE% and can't find the location of log file. – Clinton Langan Apr 06 '23 at 19:22
  • The folder `D:\BACnet-CSV\Delete_Log_Files` must exist - did you create it? Put the `SET LOGFILE=D:\BACnet-CSV\Delete_Log_Files\log.txt` to the top of your script for now until all works out. – yacc Apr 06 '23 at 19:34
  • I've added some of this to the answer. – yacc Apr 06 '23 at 19:46