I'm trying to delete all subdirectories within %path% except for the subdirectory foobar, using a batch file:
for /D %%d in ("%path%\*") do if NOT "%%d" == "foobar" rd /S /Q "%%d"
However, all subdirectories in %path% are being deleted, including foobar.
What can be happening?
Try it from the command line.
for /d %I in ("path\*") do #echo %I
Notice any clues? path\ is prepended to all results; and therefore, "path\foobar" will never equal "foobar".
Try the %%~nxI trick to get the leaf of the path.
for /d %%d in ("path\*") do if not "%%~nxd"=="foobar" rd /q /s "%%d"
Also, use a different variable name for your path. %PATH% already has special meaning, and you'll have problems if you step on its toes.
Related
I have a folder with multiple subfolders under it. I would like to delete all test folders under the subfolders/example. How can I do this using windows batch script? Note that there is no test folder in some.
I know how to delete mainfolder/subfolder1/example/test. But am stuck with recursively deleting under each subfolder i.e delete mainfolder/*/example/test.
TIA
E.g:
mainfolder
subfolder1
source
example
test
subfolder2
source
example
test
subfolderX
source
example
for /r "c:\sourcedir" /d %a in (*) do if /i "%~nxa"=="test" echo rd /s /q "%a"
direct from the prompt - double each % to use as a batch line.
replace c:\sourcedir as appropriate
required rd is merely echoed to show what he script intends to do. Remove the echo keyword after testing to actually perform the deletion.
I recommend looking at ss64.com's CMD.EXE reference, specifically the DIR, FINDSTR, and FOR commands.
for /f %i in ('dir /s /a:d /b ^| findstr /i /e "example\test"') do rmdir /s %i
appears to be the appropriate command for what you have requested.
Notes on DIR: /S - Subdirectories, /A:D - directories only ("Attribute:Directory"), /B - "bare", no headers or footers, just the full pathname.
Notes on FINDSTR: /I - Case-insensitive, /E - Match at end-of-string
Verbose descriptive summary of command: Create a list of directories, including all subdirectories, and select only those that end in "example\test", then remove each of them, including all files and subdirectories in them.
for /R "mainfolder" /D %%a in (example\te?t) do rd /S "%a"
The only inconvenient of this method is that the name of the target folder must be given as a wild-card, so you must give a name that does not include any other undesired folder. If the name is given with no wild-card, the for command may include other folders.
You just need to iterate through the main directories, and check for the existence of the directory that you want to delete:
for /D %%d in (mainfolder\*) do if exist "%%d\example\test" rd /s /q "%%d\example\test"
I need to write a .bat file that deletes all directories in a specified directory, but not files. How could I do that? Thanks in advance.
You could try something like
for /f %%d in ('dir /b /ad') do rmdir %%d
to delete all empty directories in the current working directory.
The /b switch gives just the summary, so just one entry per line.
The /ad switch gives only directories.
rd (or rmdir) deletes only empty directories by default.
Edit:
As deadlyDev pointed out, you could add /S /Q to RD to remove non-empty directories, resulting in
for /f %%d in ('dir /b /ad') do rmdir /s /q %%d
I'm trying to delete all .svn folders ONLY if they are in a CVS folder. The pattern should be something like this "*CVS\.svn".
However, my attempts at writing a batch script at this is not working at the moment. Here is what I have so far although it doesn't work.
FOR /D /R %%X IN (*CVS\.svn) DO RD /S /Q "%%X"
or
FOR /R CVS %%X IN (.svn) DO (RD /S /Q "%%X")
This works if you start one level higher than the "CVS" directory (it's a little more complex otherwise):
for /f %d in ('dir /a:d /b /s CVS') do (
if exist "%d\.svn\." rd /s /q "%d\.svn"
)
The first line finds all the directories named "CVS" recursively, then the second deletes the sub-directory ".svn" if it exists. If you're running it from a batch/shell script, use %%d instead of %d.
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."
I need to write a command in a .bat file that recursively deletes all the folders starting with a certain string. How may I achieve this ?
This is the complete answer you are looking for:
FOR /D /R %%X IN (certain_string*) DO RD /S /Q "%%X"
where obviously you need to replace certain_string with the string your folders start with.
This deletes RECURSIVELY as you asked (I mean it goes throught all folders and subfolders).
How about:
for /d %a in (certain_string*) do rd /s %a
This will work from the command prompt. Inside a batch file, you would have to double the %s, as usual:
#echo off
for /d %%a in (certain_string*) do rd /s %%a
Unfinished, I think. If you meant "Recursively go down a directory hierarchy to delete all folders starting with a certain string", then the following might suffice:
for /f "delims=" %%x in ('dir /b /ad abc*') do rd /s /q "%%x"
This will recurse into the directory tree, finding all folders starting with "abc", iterate over that list and removing each folder.
Maybe you need to wrap an if exist around the rd depending on the order in which directories are found and returned. In general, iterating over something and changing it at the same time is rarely a good idea but sometimes it works :-)
rm -rf -- "Directory name"
Ex : rm -rf -- "-2096378"
Above command will deletes the folders/directories starting with - or wildcard characters
FOR /F "tokens=*" %i IN ('DIR **[[SearchTerm]]** /A:D /s /b') do rd /S / Q %i