How to delete files and folders in the folder?
Eg. "cat" folder. In the folder existing (3 folders and 5 mp3s and 4 docxs)files.
I delete with this codes:
del /f /s /q c:\cat
rd /s /q c:\cat
del..... it delete mp3, docx but not del 3 folders. It delete files in the 3 folder.it not del 3 folders.
rd...... it delete "cat" folder, I don't del "cat" folder. I want is to delete files and folders in the "cat" folder.
for /d %%a in (c:\cat\*) do echo rd /s /q "%%~a"
Removes (deletes) a directory.
RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path
/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.
/Q Quiet mode, do not ask if ok to remove a directory tree with /S
rd /s /q c:\cat
md c:\cat
(as you don't need no files or folders inside you can delete the folder and re-create it)
Looking for a better way...EDIT:(I think is not possible without listing items)
for /f "delims=" %%a in ('dir /b "c:\cat"') do (
rd /s /q "%~a" >nul 2>&1||del /q /f "%~a" >nul 2>&1
)
( pushd "c:\cat" && rmdir . /s /q ) & popd
Change to the desired directory.
If that do work, then remove current directory. This will delete all inside the current directory, but, as the current directory is in use, it can't be deleted.
Once finished, return to previous directory
Related
For this type of directory structure:
\\rdwlhsdevserver\root\user1\folders\testdat.txt
\\rdwlhsdevserver\root\abhay\testdat.txt
\\rdwlhsdevserver\root\testuser\folders1\folder2\testdat.txt
\\rdwlhsdevserver\root\devadmin\input\testdat.txt
\\rdwlhsdevserver\root\admin\testdata\testdat.txt
I know that I can use del /s /q \\rdwlhsdevserver\root\* to delete files from parent folder and all sub-folders. But I want to delete all folders and files except \\rdwlhsdevserver\root\<folder>\.
After running cmd output should be like:
\\rdwlhsdevserver\root\user1\
\\rdwlhsdevserver\root\abhay\
\\rdwlhsdevserver\root\testuser\
\\rdwlhsdevserver\root\devadmin\
\\rdwlhsdevserver\root\admin\
pushd "\\rdwlhsdevserver\root" && (
for /d %%a in (*) do ( cd "%%a" && ( 2>nul rmdir . /s /q & cd .. ) )
del /f /q *
popd
)
This will change the current active directory (pushd) to the target directory and if there are no problems (conditional execution operator &&) for each folder (for /d) change to it (cd), remove its contents (rmdir) and return to the parent folder. Once done, remove (del) the files inside the root folder and restore the initial active directory.
Why not change the inner for to for /d %%a in (*) do rmdir "%%a" /s /q ? Because this will also remove the folder. But if we first make the folder the current active directory (cd) we will be able to delete its contents but not the folder itself as it is in use (the 2>nul is a redirection of the stderr stream to nul to hide the error in the rmdir saying it can not remove the folder because it is in use)
You need to iterate over all sub-directories of \\rdwlhsdevserver\root\ using a for /D loop, then loop over each sub-directory's sub-directories again by another for /D loop, then apply the rmdir (or rd) command on each of the returned items, like this:
for /D %%J in ("\\rdwlhsdevserver\root\*") do (
for /D %%I in ("%%~J\*") do (
rmdir /S /Q "%%~I"
)
)
Or in command prompt directly:
for /D %J in ("\\rdwlhsdevserver\root\*") do #for /D %I in ("%~J\*") do #rmdir /S /Q "%~I"
For the sake of overall performance, if you want to delete directories and files, I recommend to do the above directory removal before the file deletion (del /S /Q "\\rdwlhsdevserver\root\*"). Note that your del command line also deletes files located in the directory \\rdwlhsdevserver\root\.
I'm trying to clear a folder of all it's contents but a certain sub-folder and its contents. My deleting works fine but I can't figure out how to exclude.
cd C:\testfolder
del * /S /Q
rmdire /S /Q "C:\testfolder"
But I do not want to delete the folder C:\testfolder\subf. How can I do this?
If you are using at least windows vista (robocopy command is used), this should do the job
#echo off
setlocal enableextensions disabledelayedexpansion
rem Create a temporary empty folder
set "tempFolder=%temp%\%~nx0.%random%%random%%random%.tmp"
md "%tempFolder%" >nul 2>nul
rem Purge from target folder anything not in the empty source folder,
rem but exclude the indicated folder
robocopy "%tempFolder%" "c:\testfolder" /nocopy /purge /xd "c:\testfolder\subfolder"
rem Cleanup
rmdir "%tempFolder%" /s /q >nul 2>nul
If you mean you want to delete all files but not the subfolder you can just use the following
cd C:\testfolder
del *.* /S /Q
This will delete any files in the "testfolder" and any files in the subfolders but will leave the subfolder there.
Given the directory structure c:\a\b\c\d\ - what DOS command will delete all files contained in c:\a along with any files in the subdirectories b, c, and d? We do not want to delete any directories - just files.
The DEL command only deletes files, not directories, so the following command will do as you asked:
DEL /S C:\a
You need to use RMDIR to remove directories.
Use:
del *.* /s /q
Check the directories are still there:
tree
#del /S /Q /F a\* >nul
#for /d %%i in (a\*) do #rmdir /s /q "%%i"
What do I add to my DEL batch file if I want to exclude a folder inside a folder I want to delete?
I have this code to delete my all the contents of the temp folder
DEL /F /Q C:\temp
Now, I want to exclude a folder named imptfolder inside. This shouldn't be deleted whether it exists or not inside the temp folder. How do I do this?
Here's some batch script code that'll do what you're asking.
for /d %%I in (c:\temp\*) do (
if /i not "%%~nxI" equ "imptfolder" rmdir /q /s "%%~I"
)
del /q c:\temp\*
If you're just entering these commands from the console, use single percents rather than double.
cd /d c:\temp
for /d %I in (*) do #(if /i not "%~I" equ "imptfolder" rmdir /q /s "%~I")
del /q *
I'm trying find a way to delete a specific folder name and delete all of its contents in Windows. So my D: drive has a 100 folders, inside each of these folders are sub folders called folder1, folder2, folder3, etc. I want to be able to run a command at the root of D: that will search through each 100 folders and delete say folder3, and folder9 and all of its contents.
RD /s /q "folder1"
The above command doesnt like to search through subdirectories.
Anyway of doing with with CMD or do I need a .vbs script or something?
Thanks!
You can use the FOR command to go through the list of subfolders from a given location and run commands on each subfolder. e.g.:
for /F "delims=\" %%I in ('dir /ad /b <someFolder>') DO (
cd "<someFolder>\%%I"
rd /S /Q "folder1"
rd /S /Q "folder3"
)
It would get the list of folders in someFolder, and delete the directories named "folder1" and "folder3" from each subfolder.
Based on the OPs comments, I believe the following will do the trick:
#echo off
for /f "delims=" %%F in (
'dir /b /s /ad "d:\My files"^|findstr /ie /c:"\folder1" /c:"\folder9"'
) do if exist "%%F" rd /s /q "%%F"