Find Files with a Certain Extension That Exceeds a Certain File Size

Find files with a certain extension that exceeds a certain file size

find $HOME -type f -name "*.c" -size +2000c

Have a look to the -name switch in the mane page:

-name pattern
Base of file name (the path with the leading directories
removed) matches shell pattern pattern. The metacharacters
(`*', `?', and `[]') match a `.' at the start of the base name
(this is a change in findutils-4.2.2; see section STANDARDS CON‐
FORMANCE below). To ignore a directory and the files under it,
use -prune; see an example in the description of -path. Braces
are not recognised as being special, despite the fact that some
shells including Bash imbue braces with a special meaning in
shell patterns. The filename matching is performed with the use
of the fnmatch(3) library function. Don't forget to enclose
the pattern in quotes in order to protect it from expansion by
the shell.

Note the suggestion at the end to always enclose the pattern inside quotes. The order of the options is not relevant. Have, again, a look to the man page:

EXPRESSIONS
The expression is made up of options (which affect overall operation
rather than the processing of a specific file, and always return true),
tests (which return a true or false value), and actions (which have
side effects and return a true or false value), all separated by opera‐
tors. -and is assumed where the operator is omitted.

If the expression contains no actions other than -prune, -print is per‐
formed on all files for which the expression is true.

So, options are, by default, connected with and -and operator: they've to be all true in order to find a file and the order doesn't matter at all. The order could be relevant only for more complicated pattern matching where there are other operators than -and.

Find files that have an extension larger than a specific number

The @ext value includes enclosing quotes, so you need to include them in your IF comparison. But adding quotes will throw off the command line parser since the entire command is already enclosed in quotes. Use 0x22 to represent each internal quote.

forfiles /M * /C "cmd /c if @ext gtr 0x228000x22 echo @path"

However, FORFILES is very slow. It is much faster to use a simple FOR loop. The ~x modifier expands to the file extension (including the dot).

The following works on the command line.

for %f in (*) do @if %~xf gtr .800 echo %f

Double the percents if you want to use the command in a batch script.

Need to find a file with size more than 1GB with extension of file type

It seems that you are searching for files OVER 1GB in size which will exclude any files equalling 1GB in size

find /home/test -type f -size +1G -and -regex '\(.*tar\|.*gzip\|.*zip\|.*tgz\)'

To attain files that are equal to 1GB and above, use something like:

find /home/test -type f -size +999M -and -regex '\(.*tar\|.*gzip\|.*zip\|.*tgz\)'

If you need more accuracy, use c for bytes or k for kilobytes when specifying size.

Archive files with certain extension

Below one way I would do it in AutoIt since you asked. Replace the MsgBox line with whatever code you need to do whatever it is your wanting to do. AutoIt is fun stuff!

#include <File.au3>

archiveDir(InputBox("Path","Enter your start path."))

Func archiveDir($rootDirectory)
$aFiles = _FileListToArray($rootDirectory)

For $i = 1 To UBound($aFiles) - 1
If StringInStr(FileGetAttrib($aFiles[$i]),"D") Then archiveDir($rootDirectory & $aFiles[$i] & "\")
MsgBox(0,"This would be your archive step!",'"Archiving" ' & $rootDirectory & $aFiles[$i])
Next
EndFunc

Get file with particular extension and check the size of the file

string[] files = System.IO.Directory.GetFiles(@"c:\home\myfolder", "*.abc");
if (files.Length > 0)
{
}

Find directories containing only zero-sized files

Probably not the most efficient (one invocation of find per sub-directory, plus one more to find all sub-directories) but this should work:

while IFS= read -r -d $'\0' dir; do
if [[ -z "$(find "$dir" -maxdepth 1 -type f -size +0c)" ]]; then
printf '%s\n' "$dir"
fi
done < <(find . -mindepth 1 -type d -print0)

Applying javascript to check file size and extension

Rather than relying on the elements them self you should use the given event to the function to get the file(s) that triggered the call to the callback function.

Passing in the event will guarantee you that you are actually working with the current files that caused the function to be called.

For example (I changed the variable name to make it more clear):

ONLINE DEMO HERE

/// pass in event 
function checkFile(e) {

/// get list of files
var file_list = e.target.files;

/// go through the list of files
for (var i = 0, file; file = file_list[i]; i++) {

var sFileName = file.name;
var sFileExtension = sFileName.split('.')[sFileName.split('.').length - 1].toLowerCase();
var iFileSize = file.size;
var iConvert = (file.size / 1048576).toFixed(2);

/// OR together the accepted extensions and NOT it. Then OR the size cond.
/// It's easier to see this way, but just a suggestion - no requirement.
if (!(sFileExtension === "pdf" ||
sFileExtension === "doc" ||
sFileExtension === "docx") || iFileSize > 10485760) { /// 10 mb
txt = "File type : " + sFileExtension + "\n\n";
txt += "Size: " + iConvert + " MB \n\n";
txt += "Please make sure your file is in pdf or doc format and less than 10 MB.\n\n";
alert(txt);
}
}
}

Copy files with specific file size to Desktop path

Replace file_size = os.path.getsize(file) with

file_size = os.path.getsize(os.path.join(root,file))

Edit

import os
import shutil
import time

#Create Directory if don't exist in Desktop path
dir_name = "Backup"
dir_path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
#dir_path = os.path.expanduser("~/Desktop")
file_path = os.path.join(dir_path, dir_name)

if not os.path.exists(file_path):
os.mkdir(file_path)
print(file_path)

try:
path = r'C:\\'
extensions = [".jpg", ".jpeg"]
for root, dir, files in os.walk(path):
for file in files:
if file.endswith(extensions[0]) or file.endswith(extensions[1]):
file_size = os.path.getsize(os.path.join(root,file))
if file_size <= 50000:
print('File size:', file_size, 'KB')

shutil.copyfile(os.path.join(root,file), os.path.join(file_path,file))
print(f"File==> {file}")

except KeyboardInterrupt:
print("Quite program!!!")
time.sleep(5)
os._exit(0)


Related Topics



Leave a reply



Submit