Link to home
Start Free TrialLog in
Avatar of wasabi3689
wasabi3689Flag for United States of America

asked on

how to get the largest file size?

I want to get the largest file size for ".tif" file in a directory including many sub-directories.

How ?
Avatar of NVIT
NVIT
Flag of United States of America image

This shows all tif files sorted by size, largest last.
dir *.tif /s /od

Open in new window

Avatar of wasabi3689

ASKER

can I get just the largest one? not sort and list all of them
for /f "tokens=1* delims=:" %%a in ('dir *.tif/o:-s/b/s ^| findstr /n .') do if %%a leq 1 echo.%%b

Open in new window

no points: NVIT  /od is organize by date  /os is by size /o-s shows largest first
@David... Oops. Sorry about that.

@Wasabi... Do you want just the ONE largest out of all found, including subfolders?
Or, do you want the largest of EACH folder?
Do you want just the ONE largest out of all found, including subfolders?

Yes. I just want the largest .tif file inclduing subfolders
A bit of combining (assist) of the above:
@echo off
for /f "delims==" %%a in ('dir *.tif /s /os') do set biggest=%%a
echo %biggest%

Open in new window

Gerwin Jansen

This must be a batch file to run?
Yes indeed, I assume this isn't a problem?
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is my batch

@echo off
for /f "delims==" %%a in ('\\test3\NGProductionDocs\NGFinalDocs\ICSStorage\*.tif /s /os') do set biggest=%%a
echo %biggest%

pause

Open in new window


I have this error
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

What error?

All of the BAT examples above use the DIR command and you don't?
DO you mean I don't need a full path to the below script?

@echo off
for /f "delims==" %%a in ('dir *.tif /s /os') do set biggest=%%a
echo %biggest%


If I want to check this path \\test3\NGProductionDocs\NGFinalDocs\ICSStorage, how can I do that?
@echo off
for /f "delims==" %%a in ('dir "\\test3\NGProductionDocs\NGFinalDocs\ICSStorage\*.tif" /s /os') do set biggest=%%a
echo %biggest%

pause

Open in new window

I have a return like below

               0 Dir(s)  10,869,984,972,800 bytes free
Press any key to continue . . .
My BAT programming is pretty rusty but I don't think dir /s /os will get you where you want it to.  It seems to sort each sub folder's files independent or any other.

a BAT solution would likely involve much more programming.

Did you try the powershell version I posted?  Powershell also typically out performs BAT.
Somewhat different from your need, this bat version gets the largest of the folder and it's subfolders:
@echo off
setlocal enabledelayedexpansion

set allfiles=all.txt
dir "*.tif" /s /b /o-s > %allfiles%
for /f "tokens=*" %%a in ('type %allfiles%') do (
   set fdv=%%~da
   set fndir=%%~pa
   set fnamext=%%~nxa
   if "!fodir!" equ "" (echo %%~za,!fdv!!fndir!!fnamext!
   ) else (if "!fndir!" neq "!fodir!" echo %%~za,!fdv!!fndir!!fnamext!
   )
   set fodir=!fndir!
)

Open in new window

Agree that slightvw's powershell version is so much simpler & easier.
NVIT,

your script only shows a list of all files, not indicate which one is the largest one
Do you have something against Powershell?
Prefer DOS script,

no against PS

if used PS, do you have a simple script to have it?
>>if used PS, do you have a simple script to have it?

I posted the Powershell code to use back up in: #a41906484

Normally Powershell scripts end with a .PS1 extension.
#a41906484 solution is I need to go to that directory, if I stay in the parent dir and want to look all subfolders, how to modify your PS?
>>solution is I need to go to that directory,

The first command is from current location.

I then posted a second command:  "To find the file in c:\temp:"

replace C:\temp with \\test3\NGProductionDocs\NGFinalDocs\ICSStorage
I put your solution into .PS1 file, I ran it with the following result

PS D:\NGProductionDocs\Config\Maintenance> .\FindLargestFilePS.PS1
File D:\NGProductionDocs\Config\Maintenance\FindLargestFilePS.PS1 cannot be loaded because the execution of scripts
disabled on this system. Please see "get-help about_signing" for more details.
At line:1 char:24
+ .\FindLargestFilePS.PS1 <<<<
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException

PS D:\NGProductionDocs\Config\Maintenance>
From the powershell prompt try this:
Set-ExecutionPolicy RemoteSigned

Then retry the script.
> ...your script only shows a list of all files, not indicate which one is the largest one

Here, it shows like...

9059028,c:\test\PSGet_Cmd_All_Full.tif
1174,c:\test\Dir1\CatDumpTestSrc.tif
446237,c:\test\Dir1\Dump\{A414BE00-C782-4039-9E84-F89A2D2E62DA}_1.tif
483606,c:\test\Dir2\DirSiz-2016_04_08.tif
9059028,c:\test\Dir3\PSGet_Cmd_All_Full.tif
189,c:\test\Dir3\combine\2.tif

Open in new window

>>Here, it shows like...

The requirement is:  From the starting folder and sub folders return the ONE SINGLE largest file.

It isn't to return the largest in each folder.
I know that netminder. As I posted with the code. No worries.
Expert Commentby:slightwv (䄆 Netminder) or other expoerts

I ran your PS script, it works to capture the largest files. But, now I want to make some changes. If I want two groups 1. files larger than 20MB, and group larger than between 10 MB and 20 MB, how to modify it. I want to list all the files for each group.

Here is my PS script
Get-ChildItem -recurse F:\NGProductionDocs\NGSourceDocs\ICSStorage | Where-Object {$_.Extension -eq ".tif"} | Sort Length -desc | Select-Object -first 1

Open in new window

That should be a new related question.

Before asking the new question, I would also suggest looking around on the Internet to see if there is a way to check sizes.

The first thing I would check is the Where-Object that is restricting the file types.  Maybe there is a way to also restrict based on the size or length of the object.

I would guess since you need two outputs, there would need to be two different commands.
two commands or two PS batch files are fine. So, how to check file size in PS?
>>So, how to check file size in PS?

Again, that needs to be a new question.

Again:  Before asking the new question, read what I posted...  everything you need to answer this yourself is in that post.