In a batch file, how to delete all NOT files of a specific type
7 replies
I wrote this batch file after a colleague asked for help with removing all EXCEPT files for those of a specific type from a folder and all subfolders, while preserving the directory structure. I would recommend backing up your folder before attempting this. Just open notepad and paste the following, save it as .bat instead of .txt Good luck! ~ Carolyn
REM Use at your own risk, it does a mass DELETE of everything!
SET /p ExcludeFiles=What file type should be kept (NOT deleted)? Type the file name(s) inside parantheses. example: (pdf) or (shp dbf shx)
SET /p MapDrive=What drive letter is the folder in? example: c or n
SET /p Directory=Drag the folder you would like to modify into this command prompt then press ENTER.
%MapDrive%:
cd %Directory%
attrib +a *.* /s
echo %date%
for %%i in %ExcludeFiles% do attrib -a *.%%i /s
echo %date%
del %Directory%\*.* /s /a:a /q
echo %date%
attrib +a %Directory%\*.* /s
echo %date%
+2
a source to share
You can try something along the lines
for /f "usebackq delims=" %i in (`dir /s /b *`) do if not %~xi==.txt del %i
For your question in the comment, you can try this:
robocopy source_folder target_folder *.java /s
or
xcopy *.java target_folder /s
which preserves the directory structure but only copies files .java
.
+1
a source to share
I am using the solution found here: How do I delete all files / sub-files except some files in DOS?
let it go :)
+1
t-
a source
to share