I'm trying find a way to delete a specific folder name and delete all of its contents in Windows. So my D: drive has a 100 folders, inside each of these folders are sub folders called folder1, folder2, folder3, etc. I want to be able to run a command at the root of D: that will search through each 100 folders and delete say folder3, and folder9 and all of its contents.
RD /s /q "folder1"
The above command doesnt like to search through subdirectories.
Anyway of doing with with CMD or do I need a .vbs script or something?
Thanks!
You can use the FOR command to go through the list of subfolders from a given location and run commands on each subfolder. e.g.:
for /F "delims=\" %%I in ('dir /ad /b <someFolder>') DO (
cd "<someFolder>\%%I"
rd /S /Q "folder1"
rd /S /Q "folder3"
)
It would get the list of folders in someFolder, and delete the directories named "folder1" and "folder3" from each subfolder.
Based on the OPs comments, I believe the following will do the trick:
#echo off
for /f "delims=" %%F in (
'dir /b /s /ad "d:\My files"^|findstr /ie /c:"\folder1" /c:"\folder9"'
) do if exist "%%F" rd /s /q "%%F"
Related
From
How to delete files containing certain string using batch file in Windows?
I learned how to mass delete files which contain certain strings. What I did is
del *(2)* /f /s
but this did not delete directories. It only delete files.
How can I also mass-delete directories which contain certain strings?
There's no standard Windows command to delete files and directories on the same level. DEL is used for files, RMDIR / RD is used for directories (however it can delete files within directories).
RMDIR / RD does not work with wildcards, so you need to use a FOR loop. As it is, the below code will print out the commandos to delete the directories in your question. Remove the ECHO when you're confident the deletion will do what you want.
#ECHO OFF
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S "*(2)*"') DO (
ECHO RMDIR /S /Q "%%G"
)
You can also reduce this to a one-liner...
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S "*(2)*"') DO ECHO RMDIR /S /Q "%%G"
...and if you want to execute it directly in the shell (as opposed to from a .bat file), do:
FOR /F "tokens=*" %G IN ('DIR /B /AD /S "*(2)*"') DO ECHO RMDIR /S /Q "%G"
Flags explanation:
FOR
/F: iterate over a fileset
DIR
/B: bare format (needed so it works with FOR)
/AD: filter for directories
/S: work recursive
RMDIR
/S: work recursive
/Q: quiet mode
I have directories like:
c:\Project\Current\stage1\somefiles and some folders
c:\Project\Current\stage2\somefiles and some folders
c:\Project\Current\stage3\somefiles and some folders
c:\Project\Current\stage4\somefiles and some folders
c:\Project\Current\stage5\somefiles and some folders
.
.
.
c:\Project\Current\stage500\somefiles and some folders
I want to create a batch file so that everything inside stage1, stage2,..., stage500 will get deleted but not any of other folders so that I can still see the above directories but empty.
Can someone please help?
Try this:
#echo off
CD c:\Project\Current /d
for /f "tokens=*" %%f in ('dir /a-d /s /b') do (
del "%%f" /q /f
)
There are three important parts:
for /f "tokens=*" %%f means we are iterating over all lines that are generated by the following command and temporarily save each line in the variable %%f for each iteration.
dir /a-d /s /b is the core of the code. This will list all files inside c:\Project\Current\ including all subfolders. /a-d means that directories will be ignored as we don't want them to be erased. /s means we are searching any subfolder. /b sets the output format to simple mode so that each line of the output will contain nothing but the full path to a file.
del "%%f" /q /f simply deletes the file which is stored in %%f. /q means "don't ask me if I'm sure, just erase it" and /f means that any file - even if it is marked as system file or as invisible or protected - will be deleted. Don't miss the quotation marks around %%f as otherwise paths containing spaces will cause trouble.
I found the answer and is very simple
for /d %%X in (c:\Project\Current*) Do (
for /D %%I in ("%%X\*") do rmdir /s/q "%%I"
del /F /q "%%X\*")
Thanks for everyone's help..
I'm trying to create a batch file that will do the following:
I have multiple directories and in each one of those directories there is a folder called '06-2015'. I want to create a batch script that will go thru all of those directories and copy the folder '06-2015' and its files and nothing else.
Example:
C:\Files\Accounts\06-2015
C:\Files\Sales\06-2015
C:\Files\IT\06-2015
Is there a way I can create a script that will go something like:
xcopy C:\Files\*\06-2015 C:\Backup\*\06-2015 /s
Or is there a different/better way to do this?
A wildcard characters allowed only in the last path item.
#ECHO OFF
SET "target=06-2015"
IF NOT EXIST c:\backup\%target% MKDIR c:\backup\%target%
FOR /F "delims=" %%G IN ('DIR /B /AD "c:\files"') DO (
if exist "c:\files\%%G\%target%\" (
:: create backup directory if necessary
MKDIR "c:\backup\%%G\%target%\" 2>NUL
XCOPY /S /E /Y "c:\files\%%G\%target%\" "c:\backup\%%G\%target%\"
)
)
Test this: remove the /s and /e if you don't want any folders inside 06-2015 copied.
#echo off
for /d /r "c:\files" %%a in (06-2015?) do (
if /i "%%~nxa"=="06-2015" xcopy "%%a" "C:\Backup\%%~pnxa\" /s/h/e/k/f/c
)
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
I need the correct way to get a simple process to repeat till it reaches end of folder. Inside my Main Folder are multiple subfolders. Inside each of these are more subfolders along with a few files.
I need to run the batch from inside the Main folder and have it enter each subfolder in turn and simply run "dir *. > %date%.txt", then do the same to the next subfolder till all are done.
The only part I cannot get to work right is the change directory to each in turn till all are done.
Thanks
#echo off
for /f %%D in ('dir /b /s /ad') do (
pushd %%D
dir *. >"%date%.txt"
popd
)
to append to a existing file you should use >>, try this:
cd /d X:\main &rem put the path to your main folder here
for /r /d %%i in (*) do dir "%%~fi\*.">>"%date%.txt"
This reports all subfolder from X:\main and there subfolder recursively.
Try these options, if you weren't aware of them:
dir "c:\main folder">"%date%.txt"
dir /a-d "c:\main folder">"%date%.txt"
dir /b /s /a-d "c:\main folder">"%date%.txt"
dir /b /s /ad "c:\main folder">"%date%.txt"