mardi 21 juillet 2015

How to safely code directory delete based on parameter

I have a script which orchestrates a number of processes, all of which create logs in a location which is defined in a parameter file (ie separate to the script). Prior to running the processes, the script empties the log location e.g.

rem LOGLOC is loaded from a parameter file
Del /F /Q /S %LOGLOC%\*.*

Clearly very dangerous. If for any reason LOGLOC was incorrectly defined, this could be catastrophic. I modified the script to add some safety...

if "%LOGLOC%" equ "" (
    ECHO FATAL ERROR: LOGLOC not defined
    exit /b 1
)
Del /F /Q /S %LOGLOC%\*.*

... but does not account for instances where LOGLOC might be accidentally defined as '.' or '..'.

Is there a better pattern for checking a parameter before using it in a potentially harmful way?

Aucun commentaire:

Enregistrer un commentaire