Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

batch file forfiles help

Status
Not open for further replies.

TheDash

MIS
Mar 25, 2004
171
US
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 DIR=C:
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.
 
How about something like:
Code:
SET workingDIR=C:
SET LOGFILE="C:\test.log"
SET DATE=%mm%%dd%%yyyy%
echo %DATE% >> %LOGFILE%

if exist %workingDIR%\*.test (
  echo Deleting old .test files >> %LOGFILE%
  echo "" >> %LOGFILE
  del %workingDIR%\*.test
  echo Deleted old .test files >> %LOGFILE%
) else (
  echo no files available >> %LOGFILE%
)
Note: I changed your variable DIR to workingDIR because although technically OK in this scenario, it's a bad habit to use a reserved command as a variable
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top