directory is not empty error - batch-file

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.

Related

How to delete everything inside a notwork folder

I would like to delete everything inside a folder on a network drive, but not the folder itself. It's school drive, therefore I can't delete the folder and replace it. If I try to delete the folder, it will look like i deleted it, until I restart my pc. I need to have access to the folder afterwards, and a restart in between is just too inconvenient.
Things I've tried:
cd \\LOCATION
rd . /q /s
However this error occurs
CMD does not support UNC paths as current directories.
I hope I explained this well enough, if there was something you didn't understand then just write, then I'll try to clarify.
Create a batch file and loop through every folder inside the folder you want to keep. Consider you have \\LOCATION\Folder\folder2 then folder3 etc. then this will remove all folders from within \\LOCATION\Folder but keep Folder:
set "myunc=\\LOCATION\Folder"
pushd "%myunc%" && (
for /d %%i in (*) do rmdir "%%i" /q /s
popd
)
Set sets a variable name of myfunc with a value which in this case is your path.
For /d is a loop that goes through each directory inside your %myunc% path and then simply do a rd on each.
Pushd allows you to pretty much cd to a network UNC path. pushd will create a temp drive letter for the UNC path. So it is almost like you doing net use X: \\servername\path
popd will then just remove the temp drive letter for you.
Open cmd.exe and type pushd /? and popd /?
To learn more on batch commands, from cmd.exe do help which will list all cmd commands, for each command you can run the /? switch to learn more about it.
Open the Start Menu and in the text box, type cmd.exe and hit Enter (or open the command prompt using your preferred method)
Switch to the network drive by typing Z: (where Z is the letter of the network drive)
Change to the parent directory of the directory you're trying to delete using cd path\to\parent\directory
Delete the directory using rmdir /S giantdir
For example, if you want to delete the directory O:\MG\WTF\BBQ\SOMANYFILES:
C:\Documents And Settings\Me> O:
O:> cd MG\WTF\BBQ
O:\MG\WTF\BBQ> rmdir /S SOMANYFILES
Or now that I think about it, I think you could just do
C:\Documents And Settings\Me> O:
O:> rmdir /S MG\WTF\BBQ\SOMANYFILES
but you would miss out on the chance to see Windows spell out OMGWTFBBQ in terminal font ;-)
By the way, rmdir (or del) does not move things to the Recycle Bin, it just deletes them, so be careful not to delete things you don't really want to.

Batch File Rd Error: The directory is not empty

I've been working on a code to delete the files from a Location inputted in .txt file.
#echo off
cd C:\Users\Troy\Desktop\Details
set /p loc=<Location.txt
rd /s /q "%loc%"
echo %loc%
pause
This code returns me the following output
The directory is not empty.
C:\Users\Troy\Downloads\TV Shows
Press any key to continue ...
Now the file Location.txt, when opened contains following
C:\Users\Troy\Downloads\TV Shows which is in accordance with the echo output I get in the second line (of the above output)
Also note that I have saved the batch file at C:\Users\Troy\Desktop
So there arises no reason for any interference due to the same location.
The weird part is when I run the following code from another batch file at the same location it runs perfectly fine and deletes all the files.
#echo off
set loc=C:\Users\Troy\Downloads\TV Shows
rd /s /q "%loc%"
echo %loc%
pause
So the only difference between the two codes is that the first one sets the variable location from a specific file, whereas the other one has a pre- inputted variable.
Also I have tried to delete files from the location using the following code
#echo off
cd C:\Users\Troy\Desktop\Details
set /p loc=<Location.txt
cd %loc%
del /s /q * >nul 2>&1
cd C:\Users\Troy\Desktop\Details
rd /s /q "%loc%"
echo %loc%
pause
In the above code, the delete command works perfectly fine and deletes all the files within. However folders and subfolders are all that are left, which means that rd command is not working
I've even tried the attrib -h thing, but that does not work either.
Also note that I've tried this with various permutations and combinations of rmdir /s /q too. But does not work.
Any help appreciated.
You could be suffering from some corruption in the file system. Try running chkdsk /f. You'll have to reboot in order to run it, but see if it finds something that it can correct, then see if your problem goes away.

How to Mass Delete Files in Windows using batch

I recently refreshed my PC and upon finally finishing, I realized a pesky file named thumbs.db had been left in every folder and directory. I'm trying to find a simple way to DELETE ALL thumbs.db from my ENTIRE C: drive.
Code I've tried (Only removed thumbs.db from desktop)
dir thumbs.db /a /b /s
del thumbs.db /a /s
PS: I'm open to any Power Shell Ideas Also.
You can achieve this by running:
C:
cd /
del /s /q /f /a:h Thumbs.db
The first line sets the drive you want to run it on.
The second line points to the root directory of the drive.
The third line force deletes (/f) hidden (/a:h) thumbs.db without prompting (/q) in all subfolders (/s)

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"

Resources