I need a batch script, which has to create a directory, but if the directory already exists, it has to delete it, and create a fresh one. I dont know, what command to be used.
Normally, to check for a file exist, we do like
IF EXIST sample.txt DEL sample.txt, so that we can create a new file.
but, this doesn't work for the directory like if exist <dir name> del <dir name>
could anyone suggest what is the script to be used, to do this?
The answer actually depends on which version of Windows you are using. Also, since this is a batch file, you want to suppress questions like "Do you really want to delete X?", which will really confuse and frighten your end-user.
Here's a batch that should work on all versions of windows for you: redir.bat
@echo off
if %1.==. goto usage
if exist %1\nul goto deldir
goto newdir
::------------------------------------------------
:deldir
:: Determine if we are using Windows 2000 or XP
ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto xp-and-2000
ver | find "2000" > nul
if %ERRORLEVEL% == 0 goto xp-and-2000
goto nt-and-older
:xp-and-2000
rd /s/q %1
goto newdir
:nt-and-older
deltree /y %1
::------------------------------------------------
:newdir
md %1
goto end
::------------------------------------------------
:usage
echo usage:
echo %0 DIRNAME
echo.
echo Deletes the directory named DIRNAME and everything in it if it exists!
echo Then creates a new directory named DIRNAME
echo.
:end
It is always great to give a nice, complete example and to be completely ignored not only at the time the example was given, but also a full year later.
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.