Search and delete via batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hooby1
Posts: 17
Joined: 27 Sep 2020 08:28

Search and delete via batch

#1 Post by Hooby1 » 01 Oct 2020 07:59

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?

aGerman
Expert
Posts: 3973
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Search and delete via batch

#2 Post by aGerman » 01 Oct 2020 08:42

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

Hooby1
Posts: 17
Joined: 27 Sep 2020 08:28

Re: Search and delete via batch

#3 Post by Hooby1 » 01 Oct 2020 09:34

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.

aGerman
Expert
Posts: 3973
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Search and delete via batch

#4 Post by aGerman » 01 Oct 2020 09:41

Works for me already. Option /S of the DIR command makes it run through subdirectories recursively. Dunno what's going wrong ¯\_(ツ)_/¯

Steffen

Hooby1
Posts: 17
Joined: 27 Sep 2020 08:28

Re: Search and delete via batch

#5 Post by Hooby1 » 01 Oct 2020 10:16

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:

Hooby1
Posts: 17
Joined: 27 Sep 2020 08:28

Re: Search and delete via batch

#6 Post by Hooby1 » 08 Oct 2020 04:00

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?

aGerman
Expert
Posts: 3973
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Search and delete via batch

#7 Post by aGerman » 08 Oct 2020 04:25

DEL should be able to see every file if you just use it along with option /A

Steffen

Hooby1
Posts: 17
Joined: 27 Sep 2020 08:28

Re: Search and delete via batch

#8 Post by Hooby1 » 08 Oct 2020 05:59

Steffen

Many thanks.

I added the switch to the line DEL /a:H "%%~i"

and that now deletes the hidden files.

aGerman
Expert
Posts: 3973
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Search and delete via batch

#9 Post by aGerman » 08 Oct 2020 06:17

Only /a (without :H) would have been probably good enough, and likely even better because it includes all file attributes.

Steffen

Hooby1
Posts: 17
Joined: 27 Sep 2020 08:28

Re: Search and delete via batch

#10 Post by Hooby1 » 08 Oct 2020 06:47

Steffen

Many thanks.

Works a treat.

Post Reply