Please help me to delete all files and sub folder in MYFILES folder. Below is my code (it is delete MYFILES too but i want to keep it):
rmdir e:\MYFILES\ /s /q
Thanks a lot.
Try this
set targetdir=c:\example
del /q %targetdir%\*
for /d %%x in (%targetdir%\*) do #rd /s /q ^"%%x^"
pushd "e:\MYFILES" && ( rmdir . /s /q 2>nul & popd )
Change to the desired folder (pushd). If the command was successful (the && is an equivalent to if there are no errors), the folder is now the current active directory, so, remove anything inside it (rmdir . /s /q) As it is the current directory, it can not be deleted (the 2>nul hides the error output when the current folder can not be removed). Once the process ends, return to the previous active directory (popd)
Related
I have a directory as such:
D:\DATA\DATA2\Demo&_1\Demo\1\deployment
D:\DATA\DATA2\Demo&_1\Demo\2\deployment
D:\DATA\DATA2\Demo&_1\Demo\3\deployment
And I am running the below batch file command to delete all deployment folders present inside Demo folder:
cd /d D:\DATA\DATA2\Demo&_1\Demo
FOR /d /r . %%d IN (deployment) DO #IF EXIST "%%d" rd /s /q "%%d"
The above command is not working(not deleting deployment folder) when I have special character in in folder name (Demo&_1) and when I have do not have special then it is working(deleting deployment folder).
For e.g. if I change name of folder "Demo&_1" to "Demo1" the above batch file will work fine but not work if have special character.
Simply because the paths are not in quotes, but regardless here is a much simpler solution without having to do cd /d
for /D /R "D:\DATA\DATA2\Demo&_1\Demo" %%d IN (deployment) DO #IF EXIST "%%d" rd /s /q "%%d"
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 have a folder C:\Epson Scans, I am trying to figure out how to write a script that will delete the contents of the folder but leave the folder intact. I have figured out how to delete the entire folder and I could recreate it. But I wanted to know if anyone knows a way of just deleting the contents inside the folder and not actually deleting the folder. Any help with this would be greatly appreciated!
Edit: Inserting working code so I can loop through many computers and do it at once. Will someone please tell me why the code is not working where I have inserted it?
#echo off
setlocal enabledelayedexpansion
set Delete_success=0
set total=0
for /F %%G in (pclist.txt) do (
set /a total+=1
pushd "C:\Epson Scans" || exit /B 1
for /D %%I in ("*") do (
rd /S /Q "%%~I"
)
del /Q "*"
popd
if !ERRORLEVEL!==0 (
set /a Delete_success+=1
) else (
echo EpsonDelete copy failed on %%G>>EpsonDelete_FailedPCs.txt
)
)
echo Delete Success: %Delete_success%/%total% >>EpsonDelete_FileCopy.txt
del deletes files only, so del /S /Q "C:\Epson Scans" deletes all files in the given folder and sub-folders (due to /S).
rmdir deletes folders, so specifying rmdir /S /Q "C:\Epson Scans" also deletes the folder Epson Scans itself.
Of course you could execute mkdir "C:\Epson Scans" afterwards to newly create the deleted folder again1, but this was not asked for. So the correct answer is to use a for /D loop over C:\Epson Scans and delete each folder it contains, and then use del /Q to delete the files:
pushd "C:\Epson Scans" || exit /B 1
for /D %%I in ("*") do (
rd /S /Q "%%~I"
)
del /Q "*"
popd
Note that rd is the same as rmdir -- see also this post: What is the difference between MD and MKDIR batch command?
1) Regard that some folder attributes get lost if you do that, for example the owner. Also the case is lost as Windows treats paths case-insensitively.
del /S C:\Epson Scans*
(use S to delete all files and folders in selected folder)
del C:\Epson Scans*.*
if this is batch file you might want to add /Q in order to avoid a delete confirmation dialog:
del C:\Epson Scans\*.* /Q
I am trying to write a simple batch file which will recursively find and delete a folder. But the following script is not looking under sub folder. Wondering how to do that?
#echo off
cd /d "C:\"
for /d %%i in (temp) do rd /s "%%i"
pause
Thanks!
for /d /r "c:\" %%a in (temp\) do if exist "%%a" echo rmdir /s /q "%%a"
For each folder (/d), recursively (/r) under c:\ test for the presence of a temp folder and if it exist, remove it
directory removal command is only echoed to console. If the output is correct, remove the echo command
The /S switch to rd means
/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.
It does not mean it will search all directories looking for one with the specified name and delete them.
In other words, if you run rd /S Test from the C:\Temp folder, it will delete C:\Temp\Test\*.*, including all subdirectories (of any name) of C:\Temp\Test. It does not mean it will delete C:\Temp\AnotherDir\Test, because that isn't a subfolder of C:\Temp\Test.
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