Page 1 of 1

Search and delete via batch

Posted: 01 Oct 2020 07:59
by Hooby1
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?

Re: Search and delete via batch

Posted: 01 Oct 2020 08:42
by aGerman
Give that a go:

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"
)
Steffen

Re: Search and delete via batch

Posted: 01 Oct 2020 09:34
by Hooby1
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.

Re: Search and delete via batch

Posted: 01 Oct 2020 09:41
by aGerman
Works for me already. Option /S of the DIR command makes it run through subdirectories recursively. Dunno what's going wrong ¯\_(ツ)_/¯

Steffen

Re: Search and delete via batch

Posted: 01 Oct 2020 10:16
by Hooby1
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 :roll:

Re: Search and delete via batch

Posted: 08 Oct 2020 04:00
by Hooby1
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?

Re: Search and delete via batch

Posted: 08 Oct 2020 04:25
by aGerman
DEL should be able to see every file if you just use it along with option /A

Steffen

Re: Search and delete via batch

Posted: 08 Oct 2020 05:59
by Hooby1
Steffen

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

Posted: 08 Oct 2020 06:17
by aGerman
Only /a (without :H) would have been probably good enough, and likely even better because it includes all file attributes.

Steffen

Re: Search and delete via batch

Posted: 08 Oct 2020 06:47
by Hooby1
Steffen

Many thanks.

Works a treat.