Strona główna CMD C++ Hardware HTML i javascript Java Software SQL flag_united_kingdom flag_poland

CMD (Win7) - search specific file size.

Added: 2011-11-14

Finding files specific size in destination folder, and doing this operation often using system program to search for files, may be too time-consuming. Faster way is creating script, which searches for files of a certain size.

To get the file size from the path, best to use a parameter %~z[pathToFile(string)]. More parameters are on page Command line parameters.

In order to check the file size you can use the command dir, or loop (this is not a good idea if you want to view the size of one file):

for %a in ("c:\temp\file.txt") DO (@echo size: %~za)

Bellow is information about parameters which takes script:

  1. Method find:
    • equ : equal to
    • neq : not equal to
    • lss : less than
    • leq : less than or equal to
    • gtr : greater than
    • geq : greater than or equal to
  2. Size file in bytes
  3. Destination folder

Usage of the script:

find_file_size.bat equ 1024 c:\temp\

Code of script bellow:

Print Select code
find_file_size.bat
@echo off
setlocal EnableDelayedExpansion


if "%1%" EQU "" (
	GOTO HELP
) else if "%3%" EQU "" (
	GOTO HELP
) 


set sc=!%~d$PATH:3%~s$PATH:3!

if not exist !sc! (
	@echo Nieprawidlowa sciezka: %3
	GOTO EXIT
) 

@echo Przeszukiwanie "%1" plikow o wadze %2
@echo Start: %3
@echo Start: !sc!

if exist "find_file_size.result" (
	del find_file_size.result
) 

if "%1%" equ "equ" (
	dir !sc! /s/b > find_file_size.out
	for /f "tokens=* delims= " %%a in (find_file_size.out) DO (
		if %%~za equ %2 (
			@echo %%a  
			@echo %%a >> find_file_size.result
			@echo size: %%~za
		)
	)
	GOTO EXIT
)

if "%1%" equ "neq" (
	dir !sc! /s/b > find_file_size.out
	for /f "tokens=* delims= " %%a in (find_file_size.out) DO (
		if %%~za neq %2 (
			@echo %%a  
			@echo %%a >> find_file_size.result
			@echo size: %%~za
		)
	)
	GOTO EXIT
)

if "%1%" equ "lss" (
	dir !sc! /s/b > find_file_size.out
	for /f "tokens=* delims= " %%a in (find_file_size.out) DO (
		if %%~za lss %2 (
			@echo %%a  
			@echo %%a >> find_file_size.result
			@echo size: %%~za
		)
	)
	GOTO EXIT
)

if "%1%" equ "leq" (
	dir !sc! /s/b > find_file_size.out
	for /f "tokens=* delims= " %%a in (find_file_size.out) DO (
		if %%~za leq %2 (
			@echo %%a  
			@echo %%a >> find_file_size.result
			@echo size: %%~za
		)
	)
	GOTO EXIT
)

if "%1%" equ "gtr" (
	dir !sc! /s/b > find_file_size.out
	for /f "tokens=* delims= " %%a in (find_file_size.out) DO (
		if %%~za gtr %2 (
			@echo %%a  
			@echo %%a >> find_file_size.result
			@echo size: %%~za
		)
	)
	GOTO EXIT
)

if "%1%" equ "geq" (
	dir !sc! /s/b > find_file_size.out
	for /f "tokens=* delims= " %%a in (find_file_size.out) DO (
		if %%~za geq %2 (
			@echo %%a  
			@echo %%a >> find_file_size.result
			@echo size: %%~za
		)
	)
	GOTO EXIT
)

:HELP
@echo ***************************************************************************** 
@echo Bellow is information about parameters which takes script:
@echo [first] 		-   equ : equal to
@echo                neq : not equal to
@echo                lss : less than
@echo                leq : less than or equal to
@echo                gtr : greater than
@echo                geq : greater than or equal to
@echo [secend]    	- Size file in bytes
@echo [third]  		- Destination folder
@echo.
@echo Przyklad
@echo findS.bat equ 1024 d:
@echo ***************************************************************************** 


:EXIT

If you find unintelligible text, write to me corrected words, please - lis.la(at)wp.pl