How to force delete a folder in batch file? - batch-file

How to delete a folder in a batch file forcely ?
(Note- C:\Anything is a folder)
My code is:
#echo off
del C:\Anything
pause
But it's always asking-
Do you want to delete "C:\Anything" Y/N ?
I want that C:\Anything should delete with askimg for permision to do that !
Please help to optimize my problem !!!

Use the rd command to delete folders:
rd /s /q "C:\My Folder\"
/s: Deletes all files and folder from selected path.
/q: Suppress any message.
The official docs are here.

set backupDir="D:\db_backup"
rmdir /s /q %backupDir%
here /q will eliminate that confirmation question.

Related

How to delete a batch file along with all other files in the folder?

Can I delete a file that is the one running the batch that is deleting the file?
I need to make a batch file that will run if the username for the program isn't right. It's like recaptcha but more annoying, when it is run it deletes all the files in the same folder then self destructs.
To clarify, you are looking to remove the batch file after completion.
I think this StackOverflow post can help you.
EDIT
I don't know too much about Batch but a quick search gave me this:
#Echo off
set folder="C:\test"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
del "%~f0"
This would delete all files and folders inside the folder variable you specify.
Then you can append to this script with one of the answers from the StackOverflow post I mentioned at the start. I picked one at random, hence the del "%~f0"
It is pretty damn simple. Just write this into the batch and place it in the directory.
del /Q /F *
Which will delete everything in the folder, including the batch file itself.
Note!! I take no responsibility if you delete something you were not supposed to.
This is all you need at end of file
rmdir "C:\directory" /S /Q
del "C:\my.bat"
Make sure the program and the directory you are deleting are different. I personally have file in the startup to delete my program once set to self-destruct on restart

Delete Minecraft program with a batch code?

I need some help with a batch program to delete Minecraft on start up, because my friend likes to prank me with downloading Minecraft to my computer when I am away. Anyways, here is the code.
#echo off
cd/
CLS
rmdir /S /Q C:\Users\William\Downloads\Minecraft.exe
rmdir /S /Q C:\Users\William\Desktop\Minecraft.exe
rmdir /S /Q C:\Users\William\Appdata\Roaming\.Minecraft
MOVE "C:\Users\William\This pc\Documents\Minecraftdel.bat" "C:\Users\William\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
pause
I get the errors:
The directory names is invalid. The system can not find the specified file.
The system can not find the specified file. The syntax of the command is incorrect.
The errors are in the order of the commands!
I have checked that the files are there and I did copy the path from the File Explorer.
You are trying to use rmdir to delete a file. Use the DEL command instead.
DEL C:\Users\William\Downloads\Minecraft.exe
DEL C:\Users\William\Desktop\Minecraft.exe

BAT Command to Delete specific sub-folder

I have a follow bat command which will delete unnecessary files when i give respective file extension
#ECHO OFF
REM Change the path and the extension...
DEL /s /f /q "C:\Users\dell\Desktop\test\*.pdf"
DEL /s /f /q "C:\Users\dell\Desktop\test\*.csv"
ECHO Are you sure ? Press a key to continue.
PAUSE > NUL
ECHO Done
PAUSE
I am in need to insert specific sub-folder to delete along with files. Assume specific sub-folder name may be abc
Can any body help me on this how to insert "delete specific sub-folder"
Use the RD command to remove a (sub)directory. Since a directory may not be empty, you may want to add the /S switch to remove the entire directory tree. And since you are also going to use the command in a batch script, you may need the /Q switch as well, to avoid the confirmation prompt.
So add a command like this to your script:
RD /S /Q "C:\Users\dell\Desktop\test\Specific\Subfolder"

directory is not empty error

I try in my batch file o delete folder(BR) with many files and subdirectories, I try the following:
if exist C:\BR (
rmdir "C:\BR" /S /q
)
but sometimes I get an error that a specific folder is not empty.these folder contains files of CSS.
what the problem??
rd /s /q DIRNAME
rmdir /s /q DIRNAME
The files that you can't delete are in use.
Close whatever program is holding them open, probably your browser, and try again.
Let me guess, your trying to delete your %TMP% folder.
EDIT: To answer zipi's question.
It will delete every file and folder that it can. So, if c:\tmp\dir2\dir3\open.txt is open, c:\tmp\emptyDir is an empty directory, and you do this:
c:\>dir c:\tmp /b /s
c:\tmp\a.txt
c:\tmp\dir2\b.txt
c:\tmp\dir2\dir3\open.txt
c:\>rd /q /s c:\tmp
c:\>dir /s /b c:\tmp
c:\tmp\dir2\dir3\open.txt
You will have deleted:
c:\tmp\a.txt
c:\tmp\dir2\b.txt
And removed:
c:\tmp\emptyDir
But still have the directories...
c:\tmp
c:\tmp\dir2
c:\tmp\dir2\dir3
...an the file:
c:\tmp\dir2\dir3\open.txt
If instead, a.txt was open, you'd only have:
c:\tmp\
and
c:\tmp\a.txt
On win7 I use a simple bat file to go around the problem:
call :rmdir "my_directory_01"
call :rmdir "my_directory_02"
pause
goto :EOF
:rmdir
if exist %1 rmdir /s /q %1
if exist %1 goto :rmdir
goto :EOF
I had a similar problem. Tried lots of different solutions, but ultimately only the following worked:
rmdir c:\<directory> /s /q
Previously using other methods in CMD I was getting the following:
The directory is not empty.
I had the same issue and the solution is very silly. Please use /Q first and the /S to resolve your issue. so command should be something like:
IF EXIST %build_folder% RD /Q /S %build_folder%
Please let me know if this solves your issue.
Regards
Anuj
To remove directory in command line, you have to remove all of files and subsolders it cointainst at the first place. The problem may occur if some of those items are read-only. /f will try to force delete them.
Try
if exists C:\BR (del "C:\BR" /f /s /q)
Here you have MS docs of the DEL command: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/del.mspx?mfr=true
This worked for me
you will need to go to any drive where the folder is. Then right click on drive > properties > Check Scan disk or scan drive and for windows8 scan and repair
then go back to your folder and delete it
Batch - Getting "The directory is not empty" on rmdir command
In my case the failure to remove a directory using rd /Q /S and getting Directory not empty was down to file permissions. The batch job was doing a backup and removing oldest backup folder at the end to keep latest 10 backups. The normal user account only had read and execute permission on certain files in the sub folders. Running the batch file containing the rd commands under Task Scheduler with a tick in the option "run with highest privileges" enabled directory's to be removed.
You could achieve something similar as a one off if you run your batch file under cmd and choose run as administrator. In Windows 7 type CMD in the Search programs and files box, then right click on cmd.exe at the top of the popup window and click Run as Administrator. Then find and run your batch file in the cmd "black background" window.

What is the windows batch command to remove a path regardless it is file or folder?

Let's say we have a file or a folder named c:\temp and we need to remove it if it exists.
What is the command to do it?
Err... why not do both? :)
rd /q /s c:\temp
del /q c:\temp
One of them will succeed if C:\temp is either a file or a folder and the other one will fail. If both fails, it means that it couldn't be deleted or not found. You can even make it a batch file if you don't want to type both commands in all the time.
Err, have you tried
rd c:\temp /q /s
C:\>rmdir /?
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
It is del c:\temp. Sorry I thought it was only used for files and not applicable for folders

Resources