Search and delete via batch
Moderator: DosItHelp
Search and delete via batch
Is it possible to search and delete files running a batch file that contain a certain character in the filename and an extension?
For example I have 2 folders (docs and backup). Within windows I would use search and search for ~*.tmp and that would search for file names with a ~ and the extension ending in .tmp. I select them all and press delete.
Is it possible to run a batch that searches in a certain folder (always going to be the same one as above but in different locations) that will then delete them?
Additionally can it be written to a text file just to show which files it has deleted as this will then be run via scheduled tasks and if anything does need recovering I can see what files were deleted?
For example I have 2 folders (docs and backup). Within windows I would use search and search for ~*.tmp and that would search for file names with a ~ and the extension ending in .tmp. I select them all and press delete.
Is it possible to run a batch that searches in a certain folder (always going to be the same one as above but in different locations) that will then delete them?
Additionally can it be written to a text file just to show which files it has deleted as this will then be run via scheduled tasks and if anything does need recovering I can see what files were deleted?
Re: Search and delete via batch
Give that a go:
Steffen
Code: Select all
@echo off &setlocal& cd /d "%~dp0"
set "root=C:\somewhere"
set "log=C:\somewhere else\tmp_del.log"
for /f "delims=" %%i in ('dir /a-d /b /s "%root%\*.tmp"^|findstr /eirc:"\\[^\\]*~[^\\]*\.tmp"') do (
>>"%log%" echo %date% %time% "%%~i"
del "%%~i"
)
Re: Search and delete via batch
Steffen
Many thanks for that. I have tested it and it works. However, is it possible to scan through folders and sub folders?
Apologies in my original post I said a single folder but after looking into it there is another folder within that. When I ran the batch it works on that folder but not on any folders within that folder. Also there will be no doubt folders within folders in another folder I haven't come across yet.
Many thanks. This certainly will speed up my searching.
Many thanks for that. I have tested it and it works. However, is it possible to scan through folders and sub folders?
Apologies in my original post I said a single folder but after looking into it there is another folder within that. When I ran the batch it works on that folder but not on any folders within that folder. Also there will be no doubt folders within folders in another folder I haven't come across yet.
Many thanks. This certainly will speed up my searching.
Re: Search and delete via batch
Works for me already. Option /S of the DIR command makes it run through subdirectories recursively. Dunno what's going wrong ¯\_(ツ)_/¯
Steffen
Steffen
Re: Search and delete via batch
Steffen
My mistake. You're right it does do sub folders. I didn't change the line - set "root=C:\Temp\" to the folder after I tested it on my first folder (without any sub folders) I then added a dummy file in but in a different folder
My mistake. You're right it does do sub folders. I didn't change the line - set "root=C:\Temp\" to the folder after I tested it on my first folder (without any sub folders) I then added a dummy file in but in a different folder
Re: Search and delete via batch
Steffen
Many thanks for this. Works great and speeds up searching for files.
However, the deletion doesn't happen when the files have an attribute as "Hidden" It writes to the log file but doesn't delete them.
Is it possible to make this batch delete the files when this attribute is set as that?
Many thanks for this. Works great and speeds up searching for files.
However, the deletion doesn't happen when the files have an attribute as "Hidden" It writes to the log file but doesn't delete them.
Is it possible to make this batch delete the files when this attribute is set as that?
Re: Search and delete via batch
DEL should be able to see every file if you just use it along with option /A
Steffen
Steffen
Re: Search and delete via batch
Steffen
Many thanks.
I added the switch to the line DEL /a:H "%%~i"
and that now deletes the hidden files.
Many thanks.
I added the switch to the line DEL /a:H "%%~i"
and that now deletes the hidden files.
Re: Search and delete via batch
Only /a (without :H) would have been probably good enough, and likely even better because it includes all file attributes.
Steffen
Steffen
Re: Search and delete via batch
Steffen
Many thanks.
Works a treat.
Many thanks.
Works a treat.