need help with a dos batch script.

June 18, 2009 at 11:58:56
Specs: Windows XP
Hi members,
I am trying to convert a unix shell script to dos. It needs to delete all .test files in a directory and write to log file that the command was successful. When there are no files, it should write "no files available" to the logfile.

I have this right now:

SET LOGFILE="C:\test.log"
SET DATE=%mm%%dd%%yyyy%
echo %DATE% >> %LOGFILE%

echo Deleting old .test files > %LOGFILE%
echo "" >> %LOGFILE
del %DIR%\*.test
echo Deleted old .test files > %LOGFILE%

I need to add the if else condition. i.e when there are no files available, it should write the same. Any help will be appreciated.

See More: need help with a dos batch script.

Report •

June 18, 2009 at 13:19:38
@echo off

set drive=C:
set logfile=C:\test.log

echo.%date% > %logfile%
echo.Deleting old .test files >> %logfile%
echo.>> %logfile%

if exist %drive%\*.test (
  del %drive%\*.test
  echo.Deleted old .test filesfrom %drive%\*.test >> %logfile%
) else (
  echo. No files available >> %logfile%
:: End_Of_Batch

First of all use lowercase to improve readibility. Then use the %date% system variable to be NEVER modified. More if you want to delete the *.test files in the root directory AND all the subdirs add the /S switch at the end of the DEL command.

By the way this is NT batch nothing to do with DOS (that does not exist in XP).

Report •
Related Solutions

Ask Question