I have a folder, which has files and folders inside it like
C:/MyFolder
C:/MyFolder/File1.txt
C:/MyFolder/File2.txt
C:/MyFolder/File3.sql
C:/MyFolder/Folder1
C:/MyFolder/Folder1/File5.txt
What batch command do I need to use to delete all the folders and contents inside them without deleting files inside my folder. Example : Delete Folder1,Folder1/File5.txt but retain File1.txt,File2.txt and File3.sql?
This shows you the commands - if you are happy with them then remove the echo keyword and run it again.
#echo off
for /d %%a in ("C:\MyFolder\*") do echo rd "%%a" /q /s
pause
from command prompt:
for /f "tokens=* delims=" %a in ('dir /b /a:d "C:\someDir"') do #rd /s /q "%~fa"
from batch file:
for /f "tokens=* delims=" %%a in ('dir /b /a:d "C:\someDir"') do #rd /s /q "%%~fa"
For DOS/Command prompt use
for /d %F in ("path*") do rmdir /s /q "%F"
Use double % if you use it in a batch file.
for /d %%F in ("H:\EDGE-backup*") do rmdir /s /q "%%F"
I used this to backup the EDGE bookmarks and such, and since XCOPY always brings with it it's root directory subfolders i had to delete these after the copy.
The above worked for this. Result,only files remained in H:\EDGE-backup.
I want to have a batch file that will delete all the folders and files in my cache folder for my wireless toolkit.
Currently I have the following:
cd "C:\Users\tbrollo\j2mewtk\2.5.2\appdb\RMS"
del *.db
This will delete all .db files in my RMS directory, however I want to delete every single thing from this directory. How can I do this?
Use:
Create a batch file
Copy the below text into the batch file
set folder="C:\test"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
It will delete all files and folders.
del *.* instead of del *.db. That will remove everything.
IF EXIST "C:\Users\tbrollo\j2mewtk\2.5.2\appdb\RMS" (
rmdir "C:\Users\tbrollo\j2mewtk\2.5.2\appdb\RMS" /s /q
)
This will delete everything from the folder (and the folder itself).
I just put this together from what morty346 posted:
set folder="C:\test"
IF EXIST "%folder%" (
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
)
It adds a quick check that the folder defined in the variable exists first, changes directory to the folder, and deletes the contents.
del *.* will only delete files, but not subdirectories. To nuke the contents of a directory, you can use this script:
#echo off
setlocal enableextensions
if {%1}=={} goto :HELP
if {%1}=={/?} goto :HELP
goto :START
:HELP
echo Usage: %~n0 directory-name
echo.
echo Empties the contents of the specified directory,
echo WITHOUT CONFIRMATION. USE EXTREME CAUTION!
goto :DONE
:START
pushd %1 || goto :DONE
rd /q /s . 2> NUL
popd
:DONE
endlocal
The pushd changes into the directory of which you want to delete the children. Then when rd asks to delete the current directory and all sub directories, the deletion of the sub directories succeed, but the deletion of the current directory fails - because we are in it. This produces an error which 2> NUL swallows. (2 being the error stream).
You can do this using del and the /S flag (to tell it to recurse all files from all subdirectories):
del /S C:\Path\to\directory\*
The RD command can also be used. Recursively delete quietly without a prompt:
#RD /S /Q %VAR_PATH%
Rmdir (rd)
set "DIR_TO_DELETE=your_path_to_the_folder"
IF EXIST %DIR_TO_DELETE% (
FOR /D %%p IN ("%DIR_TO_DELETE%\*.*") DO rmdir "%%p" /S /Q
del %DIR_TO_DELETE%\*.* /F /Q
)
Use
set dir="Your Folder Path Here"
rmdir /s %dir%
mkdir %dir%
This version deletes without asking:
set dir="Your Folder Here"
rmdir /s /q %dir%
mkdir %dir%
Example:
set dir="C:\foo1\foo\foo\foo3"
rmdir /s /q %dir%
mkdir %dir%
This will clear C:\foo1\foo\foo\foo3.
(I would like to mention Abdullah Sabouin's answer. There was a mix up about me copying him. I did not notice his post. I would like to thank you melpomene for pointing out errors!)
Try the following; it works for me.
I have an application which dumps data in my "C:\tmp" folder, and the following works the best for me. It doesn't even ask Yes or No to delete the data. I have made a schedule for it to run after every 5 minutes
cd "C:\tmp"
del *.* /Q
Better yet, let's say I want to remove everything under the C:\windows\temp folder.
#echo off
rd C:\windows\temp /s /q
You could use robocopy to mirror an empty folder to the folder you are clearing.
robocopy "C:\temp\empty" "C:\temp\target" /E /MIR
It also works if you can't remove or recreate the actual folder.
It does require an existing empty directory.
I would like to suggest using simple tool like cleardir. So, in batch file you can write:
cleardir path/to/dir
And you'll get empty directory dir. A bit slow, but still resolves the "problem".
I'm an author of the tool =)
The easiest way is:
Create *.txt file
Write:
rmdir /q /s . dir
Save file as *.bat in folder which you want to clear (you can call the file NUKE.bat)
Turn it on
WARNING!
THIS DELETES EVERYTHING IN THE FOLDER WHERE IT IS WITHOUT ASKING FOR CONFIRMATION!!!
SO CHOOSE WISELY PLACE FOR SAVING IT.
Easy simple answer :
C:
del /S /Q C:\folderName\otherFolderName\*
C: Important in case you have to switch from D: to C: or C: to D: (or anything else)
/S Recursive, all subfolders are deleted along
/Q If you don't activate quiet mode, prompt will ask you to type y for every subfolders... you don't want that
Be carful, it's drastic.
You cannot delete everything with either rmdir or del alone:
rmdir /s /q does not accept wildcard params. So rmdir /s /q * will error.
del /s /f /q will delete all files, but empty subdirectories will remain.
My preferred solution (as I have used in many other batch files) is:
rmdir /s /q . 2>NUL
Just a modified version of GregM's answer:
set folder="C:\test"
cd /D %folder%
if NOT %errorlevel% == 0 (exit /b 1)
echo Entire content of %cd% will be deleted. Press Ctrl-C to abort
pause
REM First the directories /ad option of dir
for /F "delims=" %%i in ('dir /b /ad') do (echo rmdir "%%i" /s/q)
REM Now the files /a-d option of dir
for /F "delims=" %%i in ('dir /b /a-d') do (echo del "%%i" /q)
REM To deactivate simulation mode remove the word 'echo' before 'rmdir' and 'del'.
#echo off
#color 0A
echo Deleting logs
rmdir /S/Q c:\log\
ping 1.1.1.1 -n 5 -w 1000 > nul
echo Adding log folder back
md c:\log\
You was on the right track. Just add code to add the folder which is deleted back again.
I'm trying to delete all .svn folders ONLY if they are in a CVS folder. The pattern should be something like this "*CVS\.svn".
However, my attempts at writing a batch script at this is not working at the moment. Here is what I have so far although it doesn't work.
FOR /D /R %%X IN (*CVS\.svn) DO RD /S /Q "%%X"
or
FOR /R CVS %%X IN (.svn) DO (RD /S /Q "%%X")
This works if you start one level higher than the "CVS" directory (it's a little more complex otherwise):
for /f %d in ('dir /a:d /b /s CVS') do (
if exist "%d\.svn\." rd /s /q "%d\.svn"
)
The first line finds all the directories named "CVS" recursively, then the second deletes the sub-directory ".svn" if it exists. If you're running it from a batch/shell script, use %%d instead of %d.
Using CMD line, in a given directory, I want to detect the most recently created/written folder and delete all the contents of that folder.
Any help/suggestions would be helpful.
This command prints all subdirectories in order of their last write/created time in reverse order (latest directories first):
DIR /A:D /O:-D /TW /B
To delete a directories' contents, a simple
DEL /S /Q "directory"
should be sufficient
If you want to process only the first result of the DIR command, you can use a FOR loop in a batch file, that leaves after the first iteration.
It should look something like this:
#ECHO OFF
REM delete all contents from the sub directory most recently created or written to
FOR /F "delims=" %%A IN ('DIR /A:D /O:-D /TW /B') DO (
RD /S /Q %%A
EXIT /B
)
Only works for the subdirectories of the current working directory, so use with care!
I guess for empty directories there will be some weird output, but I didn't test it.
EDIT:
Updated the batch file to remove the whole directory and its content using:
RD /S /Q "directory"
I need to write a command in a .bat file that recursively deletes all the folders starting with a certain string. How may I achieve this ?
This is the complete answer you are looking for:
FOR /D /R %%X IN (certain_string*) DO RD /S /Q "%%X"
where obviously you need to replace certain_string with the string your folders start with.
This deletes RECURSIVELY as you asked (I mean it goes throught all folders and subfolders).
How about:
for /d %a in (certain_string*) do rd /s %a
This will work from the command prompt. Inside a batch file, you would have to double the %s, as usual:
#echo off
for /d %%a in (certain_string*) do rd /s %%a
Unfinished, I think. If you meant "Recursively go down a directory hierarchy to delete all folders starting with a certain string", then the following might suffice:
for /f "delims=" %%x in ('dir /b /ad abc*') do rd /s /q "%%x"
This will recurse into the directory tree, finding all folders starting with "abc", iterate over that list and removing each folder.
Maybe you need to wrap an if exist around the rd depending on the order in which directories are found and returned. In general, iterating over something and changing it at the same time is rarely a good idea but sometimes it works :-)
rm -rf -- "Directory name"
Ex : rm -rf -- "-2096378"
Above command will deletes the folders/directories starting with - or wildcard characters
FOR /F "tokens=*" %i IN ('DIR **[[SearchTerm]]** /A:D /s /b') do rd /S / Q %i