I'm trying to remove a specific directory from every User's AppData directory within Win7. We had a bad install of Cisco on multiple computers, and part of fixing it is that we need to remove AppData\Local\Cisco from every user on the computer prior to uninstalling, Wouldn't be an issue except every computer has anywhere from 5-20 users on it. I've tried the following:
for /D %%f in (Cisco) do rmdir %%f /s
And running that from the Users directory. It's not working though, and I'm unsure the best route to removing this directory from all users on the computer, if I try a del or rmdir with wildcards, that breaks as well. Any ideas?
Run this in the users folder from the cmd prompt and check that it returns the correct folders.
for /d /r %a in (cisc*) do #echo %a
If so then add the RD command.
for /d /r %a in (cisc*) do rd /q /s "%a"
Related
I'm playing around with hacking on my virtual machine and in that tried to delete a folder which Windows created by default - "Videos" or "Documents", etc.
I would like to delete that folder from the cmd (I tried as an admin) but it doesn't seem to work. When deleting it from the GUI there were no issues.
I tried the following:
rmdir /s /q folderpath
And always get: Access is denied
start cmd in admin mode
takeown /F C:\{folder-name}\* /R /A
icacls C:\{folder-name}\*.* /T /grant administrators:F
rmdir /S /Q C:\{folder-name}\
From this StackOverflow answer:
RMDIR or RD if you are using the classic Command Prompt (cmd.exe):
rd /s /q "path"
If it tells you you have no access/permission to remove the directory, it's most likely an important Windows directory you shouldn't remove. You could try opening the CMD as Administrator, but be sure to check that it's safe to delete the folder.
I am running a batch file which deploys a war file to a specific directory. However, I want to empty that directory before deploying to it, recursively.
I found this script that is supposed to remove everything in the directory including folders and files but I get this error: "%%i was unexpected at this time."
FOR /D %%i IN ("C:/Program Files (x86)/Apache Software Foundation/Tomcat 6.0/abtapps/ROOT/*") DO RD /S /Q "%%i" DEL /Q "C:/Program Files (x86)/Apache Software Foundation/Tomcat 6.0/abtapps/ROOT/*.*"
I just need a simple way to recursively empty my ROOT directory either by fixing the script above or with another method. Have to be able to do this from a batch file.
I am running windows 7.
SOLUTION
rmdir ROOT /s /q
mkdir ROOT
You should use rmdir /S. Check out this link
I think what you are looking for is to clear the folder not to delete it and recreate it. This will actualy clear the folder so you don't have to recreate it.
CD "C:/Program Files (x86)/Apache Software Foundation/Tomcat 6.0/abtapps/ROOT" && FOR /D %i IN ("*") DO RD /S /Q "%i" && DEL /Q *.*
A few notes.
You only use a single % when running from the command line. doubles are for batch files.
&& is for running multiple commands on the same line.
* The &&'s are especially important in this case because you do not want the del /q . to run if the cd fails as you will delete to contents of what ever directory you started in.
Try this:
forfiles /p "folder" /s /c "cmd /c rd /q #path
replace "folder" with your folder name or drive letter
example: forfiles /p f: /s /c "cmd /c rd /q #path
This will recurse through all subfolders, and only remove the empty folders.
If you use it in command line, you'll get error messages for folders that aren't empty. If you put it in a batch script, it will take some time to run, depending on how many folders you have.
I need to delete the entire contents of a directory (nested folders and all) without deleting the directory itself. Recreating the directory after the fact is not an option as it is being locked by the running process and delete of it would fail.
So far I have the following:
rd /s /q dir1
rd /s /q dir2
rd /s /q dir3
del /q /f *
It works, but the obvious problem is that I have to update this script every time the set of first-level directories changes.
On UNIX, I would solve this like this:
rm -rf *
What is the Windows equivalent?
Assuming that you are executing the command from the top-level directory:
for /d %X in (*.*) do rd /s /q %X
If you are executing this from a script, you must use double percent signs:
for /d %%X in (*.*) do rd /s /q %%X
If you need to delete the files in the top-level directory as well, add this to the script:
del /q /f *
I know this is an old question with an old answer, but I've found a simpler way to do this and thought of sharing it.
You can step into the target directory and use the rd command. Since Windows will not allow you to delete any files or directories currently in use, and you are making use of the target directory by stepping into it, you'll delete all the contents, except the target directory itself.
cd mydir
rd /s /q .
You'll get a message saying:
The process cannot access the file because it is being used by another process.
This will occur when, after deleting all the contents, the rd command fails to delete the current directory, because you're standing in it. But you'll see this is not an actual error if you echo the last exit code, which will be 0.
echo %errorlevel%
0
It's what I'm using and it works fine. I hope this helps.
I have the directory structure /foo/bar/fooBar/.. . I want to write a Windows command where I can mention the path till foo directory and it deletes all the files and directory recursively in /foo, but it should NOT delete the foo directory.
I have been using rmdir /q /s [path to foo] but this command deletes the foo directory as well. Let me know if there is any command(s) to accomplish this.
rd /s /q /path/to/foo
md /path/to/foo
del /f /s /q DirectoryWhichContainsFilesToDelete/\*
This will delete all files in the folder DirectoryWhichContainsFilesToDelete without deleting the folder itself.
Have fun :)
I had been scratching my head on this one as well. It is easy enough to create a for loop that uses rmdir however it leaves behind folders that have spaces in the long names. It is possible to manipulate a dir list and get the 8.3 filenames however here is a much simpler solution.
Create an empty folder then;
robocopy \empty_folder \folder_with_sub_folders /PURGE
All subfolders & files will be deleted.
del X /f /s /q
rd X /s /q
this WILL remove the ROOt directory though. make it again with
md X
or make a copy of it first.
otherwise you'll have to do batch funkiness
dir X /ad /b
will give you a list of the immediate subdirectories of X. you can work out the rest
I was looking for a simple command to delete all files in a directory recursively but leaving the directory structure unchanged. So, maybe this could be interesting ;)
for /f "delims=" %i in ('dir /B /S /A:-DH') do #del /F /Q /A:H "%i"
The command 'dir /B /S /A:-D' lists only files (/A:-D) in current directory recursively (/S) without 'dir' summary report (/B). The 'for' loops through each full line (/delims=) and executes the delete command, forced and quiet. I additionally used the hidden flag (/H) both for listing and deletion for some mysterious (e.g. thumbs.db) files.
deltree /foo/* should work fine.
I have used this in a batch file in the past. It uses a for loop to navigate the directory structure.
Here I remove the cvs sub directories off of a tree, needed when copying from one branch to another.
#echo off
if /I exist CVS. rd CVS /s /q >nul
for /F %%z in ('dir cvs /ad /s /b') do echo %%z && rd /s /q %%z
echo Batchfile %0 is complete
Try to use Powershell:
powershell -Command "Remove-Item '\foo\*' -Recurse -Force"
To prevent deleting of the foo directory try change directory to foo prior to the delete such as:
cd c:\foo
rd /s /q c:\foo
This will delete all the files and folders under foo but NOT foo. An error message will be displayed as follow "The process cannot access the file because it is being used by another process."
In earlier versions of MS-DOS - I want to say version 7, but I could be wrong - there was a deltree command, which recursively deleted all subdirectories and files from a given path.
deltree no longer exists, but del didn't seem to inherit the ability to delete a tree. del /s deletes files, but not folders.
How to you easily (i.e., in one command) delete a tree from a batch file?
As others have mentioned, the rd command has the /s switch to recursively remove sub-directories. You can combine it with the /q switch to forcibly delete a sub-directory (and its contents) without prompting as so
rd /s /q c:\foobar
What everybody is missing is that rd is not an exact replacement for deltree as seemingly (almost) every page returned by Googling for windows deltree would have you believe. The deltree command worked for both directories and files, making it a single convenient, all-purpose deletion command. That is both of the following are valid:
deltree /y c:\foobar
deltree /y c:\baz.txt
However rd (not surprisingly) only works for directories. As such only the first of these commands is valid while the second gives and error and leaves the file un-deleted:
rd /s /q c:\foobar
rd /s /q c:\baz.txt
Further, the del command only works for files, not directories, so only the second command is valid while the first gives an error:
del /f /q c:\foobar
del /f /q c:\baz.txt
There is no built-in way to delete files and directories as could be done with deltree. Using rd and del individually is inconvenient at best because it requires distinguishing whether a file-system object (file-/folder-name) is a file or directory which is not always possible or practical.
You can copy the deltree command from a previous OS, however it will only work on 32-bit versions of Windows since it is a 16-bit DOS command (even in Windows 9x).
Another option is to create a batch-file that calls both del and rd; something like this:
::deltree.bat
#echo off
rd %* 2> nul
del %* 2> nul
You would call it as so:
deltree.bat /s /q /f c:\foobar
deltree.bat /s /q /f c:\baz.txt
This calls both rd and del, passing in the arguments and redirecting the output to nul to avoid the error that one of them will invariably emit.
You will probably want to customize the behavior to accomodate or simplify parameters or allow error messages, but even so, it is not ideal and not a direct replacement for deltree.
An alternative is to get a third-party tool, though finding one is a real exercise in search-query-crafting.
It was replaced with the commands: RMDIR or RD
Delete all subdirectories with /S
Use it quietly with the /Q
Example:
RMDIR /S /Q Folder2Delete
RD /S /Q Folder2Delete
Documentation:
DELTREE at Wikipedia
RMDIR at Wikipedia
RMDIR at Microsoft
Feeling nostalgic, I wrote my own deltree.exe. It works with both directories and files, and uses SHFileOperation() for speed.
https://github.com/ai7/toolbox/tree/master/deltree
deltree v1.01 [Mar 27 2015, 16:31:02] (gcc 4.9.1)
Usage: deltree [options] <path> ...
Options:
-y yes, suppresses prompting for confirmation
-s silent, do not display any progress dialog
-n do nothing, simulate the operation
-f force, no prompting/silent (for rm compatibility)
-r ignored (for rm compatibility)
Delete directories and all the subdirectories and files in it.
It takes wildcards and you can use it like unix rm:
deltree -rf *
rmdir /s /q directory
Nowadays, you can use Powershell to do the same task:
powershell -Command "Remove-Item 'PathToMyDirectory\*' -Recurse -Force"
$ help rd
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
Actually RMDIR and RD commands in modern Windows operating system merge both the commands RD and Deltree of Win 98 in a single command. It's an internal command that's why you won't find any RD.exe and RMDIR.exe.
By typing this "RD /?" in cmd without double qoutes you'll get exactly what you want.
to delete a directory and all it's contents recursively
rd /s MY_DOOMED_DIR
Use this:
cd (your directory here)
del *.* /f /s /q
Delete all files and subdirectories
cd /d Directory && rd /s /q .\
Others have already posted excellent answers. I regularly use rd /s /q in a command prompt with a for loop to delete directories under the current one: for /d %d in (*) do rd /s /q "%d" (where * can be replaced with names instead, e.g. (dir1 dir2 dirX)