search files with filesize category and delete

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

search files with filesize category and delete

Post by MigrationUser »

01 Jun 2009 16:22
guds612

hi! i'm just new here and also in cmd shell scripting.

anyway, i already searched for "search and delete" script but haven't found the right one.

what i want is a script that will search for executable files (.exe) that is equal to a certain size and delete it permanently.

example :

i want to delete all .exe file with file size of 500kb in drive C:

one-click and boom, all deleted

all help info's will be very much appreciated.

----------------------------

#2 01 Jun 2009 17:28
bluesxman

Code: Select all

[b]*** USE ENTIRELY AT YOUR OWN RISK ***[/b]

@echo off

set size=512000
set root=C:\
set file=*.exe

for /f "usebackq tokens=*" %%a in (`dir /b/s "%root%\%file%"`) do if %%~za EQU %size% echo:delete "%%~a"

pause

*** USE ENTIRELY AT YOUR OWN RISK ***
Deleting based on size alone is foolhardy at best and could have catastrophic results (you'll quite likely end up deleting something you really wish you hadn't).

I've intentionally de-fanged it, remove "echo:" to give it teeth.

cmd | *sh | ruby | chef

----------------------------

#3 01 Jun 2009 19:52
guds612


thanks for the replies. il go check these scripts and use it by folder for the meantime.

if there's a category you would like to add, what would it be?

Last edited by guds612 (01 Jun 2009 19:55)

----------------------------

#4 01 Jun 2009 21:06
guds612


@bluesxman

your script worked using it to search by folder but if i set the "root=C:\" i always get this error

"The filename, directory name, or volume label syntax is incorrect."

and when i set the "root=C:" only without the "\"

it seems the script is not running. the cmd prompt stays black for a long time and i never seen it went through.

----------------------------

#5 01 Jun 2009 22:40
avery_larry


It should be root=c: like you found.

How long did you wait? It's going to do a DIR listing on the entire hard drive and then compare every .exe file it finds to the size requested.

It could take hours just for the DIR listing -- obviously depending on how many files and how fast your storage is.

----------------------------

#6 02 Jun 2009 08:29
bluesxman


Weird, doubling "\\" doesn't seem to do any harm unless they're immediately after the ":"

C:\>dir c:\\windows
The filename, directory name, or volume label syntax is incorrect.

C:\>dir c:\windows\\system32
Volume in drive C is OS
Volume Serial Number is EC42-E88E

Directory of c:\windows\system32

Thanks for the consistency, Microsoft.

cmd | *sh | ruby | chef

----------------------------

#7 02 Jun 2009 18:21
Simon Sheppard


Raymond Chen has the answer:

https://devblogs.microsoft.com/oldnewth ... 5/?p=33243
A little-known quirk is that the file system accepts and ignores a drive letter in front of a UNC path. For example, if you have a directory called \\server\share\directory, you can say dir P:\\server\share\directory and the directory will be listed to the screen. The leading P: is ignored.

Why is that? Rewind to 1984 and the upcoming release of MS-DOS 3.1, which added networking support. Up to this point, all fully-qualified file specifications consisted of three components: A drive letter, a path, and a file name. Many programs relied on this breakdown and did things like "helpfully" prepend a drive letter if it looks like you "forgot" one. For example, if you told it to save the results to \\server\share\file.txt it would say, "Oh dear, that's not good, the user forgot the drive letter! I'll put the current drive in front to make things better," resulting in C:\\server\share\file.txt. Other programs would prompt you with "Please enter a drive letter", and you couldn't say "No, there's no drive letter, just take the path and use it." They insisted on a drive letter, and you darn sure better give them one.

In order to retain compatibility with programs that provided this sort of "unwanted help", the designers of the networking support in MS-DOS decided to allow the strange syntax

----------------------------

#8 02 Jun 2009 22:12
avery_larry
Simon Sheppard wrote:

Raymond Chen has the answer:

https://devblogs.microsoft.com/oldnewth ... 5/?p=33243
A little-known quirk is that the file system accepts and ignores a drive letter in front of a UNC path. For example, if you have a directory called \\server\share\directory, you can say dir P:\\server\share\directory and the directory will be listed to the screen. The leading P: is ignored.

Why is that? Rewind to 1984 and the upcoming release of MS-DOS 3.1, which added networking support. Up to this point, all fully-qualified file specifications consisted of three components: A drive letter, a path, and a file name. Many programs relied on this breakdown and did things like "helpfully" prepend a drive letter if it looks like you "forgot" one. For example, if you told it to save the results to \\server\share\file.txt it would say, "Oh dear, that's not good, the user forgot the drive letter! I'll put the current drive in front to make things better," resulting in C:\\server\share\file.txt. Other programs would prompt you with "Please enter a drive letter", and you couldn't say "No, there's no drive letter, just take the path and use it." They insisted on a drive letter, and you darn sure better give them one.

In order to retain compatibility with programs that provided this sort of "unwanted help", the designers of the networking support in MS-DOS decided to allow the strange syntax
Show off.

:D

----------------------------

#9 05 Jun 2009 12:03
guds612


@avery_larry

i tried to use it to scan a network drive and yeah, it took a long time. i thought it never really worked at all.

----------------------------

#10 05 Jun 2009 12:50
guds612


another thing, using the script i cannot access folder whose names includes a "&" character. for example, i have a folder named "A&B" inside my drive c: and i used "C:\A&B" as my root. the error message is like this..

"The system cannot find the path specified.
'B' is not recognized as an internal or external command, operable program or batch file."

----------------------------

#11 05 Jun 2009 13:24
bluesxman

Code: Select all

"&" is a special character in CMD.

Try this:

@echo off

set "size=512000"
set "root=C:\A&B"
set "file=*.exe"

for /f "usebackq tokens=*" %%a in (`dir /b/s "%root%\%file%"`) do if %%~za EQU %size% echo:delete "%%~a"

pause
cmd | *sh | ruby | chef

----------------------------

#12 05 Jun 2009 14:00
guds612


thanks for the reply!

----------------------------

#13 07 Jul 2009 15:50
guds612


how about with the category "date last modifed?"

----------------------------

#14 07 Jul 2009 21:17
bluesxman


Not tested...

Code: Select all

@echo off

set "size=512000"
set "date.mod=07/07/2009"
set "root=C:\A&B"
set "file=*.exe"

for /f "usebackq tokens=*" %%a in (`dir /b/s "%root%\%file%"`) do (
    for /f "tokens=1" %%d in ("%%~ta") do (
        if "%%~d"  EQU "%date.mod%" (
        if "%%~za" EQU "%size%" (echo:delete "%%~a")
    ))
)

pause
cmd | *sh | ruby | chef

----------------------------

#15 23 Jul 2009 11:06
guds612


wow blue, you're the man!

where should i put another "for loop" so that the date.mod value is changing. for example, from 07/07/2009 up to 12/12/2009?

last request man.. :-D
Post Reply