| | |
Delete a directory using batch script
![]() |
•
•
Join Date: Apr 2007
Posts: 9
Reputation:
Solved Threads: 0
Hi,
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?
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
Hope this helps.
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
Hope this helps.
0
#10 Oct 10th, 2009
Do you mean like
?
rd /q/s C:\DOCUME~1\John\Cookies rd /q/s C:\DOCUME~1\Jill\Cookies rd /q/s C:\DOCUME~1\Johann\Cookies
![]() |
Similar Threads
- Changing a background in a batch script (Windows NT / 2000 / XP)
- Closing batch script after .exe is called (Windows NT / 2000 / XP)
- c++ or shell script to delete some files (C++)
- Script or Batch file to load Administrators in XP (Networking Hardware Configuration)
- How to delete files in UNIX using shell script (Shell Scripting)
- Creating Batch Files in WIndows XP (Windows NT / 2000 / XP)
- Zip files, move files, delete source (help me please) (Shell Scripting)
- Batch Script - Error 7901??? (Windows NT / 2000 / XP)
Other Threads in the Legacy and Other Languages Forum
- Previous Thread: GW Basic running on WIndows 2000/XP
- Next Thread: please help me write this prog in lisp
| Thread Tools | Search this Thread |






