1

I am trying to write a script that tell me if a file file size is greater than a number, below is the script I have written, but it keeps showing 1999 even though I know the file size is greater than the number.

@echo off setlocal enabledelayedexpansion for /f "delims=" %%a in (' powershell -c "(Get-ChildItem -Recurse "C:\XSTOREDB\xstore_data.mdf" | Measure-Object -Property Length -Sum).Sum" ') do set "foldsize=%%a" if "!foldsize!" GTR "6000000000" ( exit /B 0 ) else ( exit /B 1999 ) endlocal 
2
  • sharepointsky.com/check-file-size-using-powershell here are some examples of what you want to do Commented Jan 31, 2020 at 21:53
  • 2
    ummm, errr ... WHY are you mixing Bat/CMD with powershell? this is easily doable in PoSh all on it's own. Commented Jan 31, 2020 at 23:10

3 Answers 3

1

I'm pretty sure you have 2 problems. The first problem is that you are doing an alphabetical comparison. Thus, "7" would be considered GTR "6000000000".

Your second problem is that numeric operations in cmd are limited to 32 bit integers. The maximum number you can use is 2^31 - 1, which is 2147483647, which is already less than the number you are interested in.

The typical workaround here is to either compare the "important" parts of the string, or truncate the N least significant digits.

Here is a quick example. It may contain issues/errors in implementation. I did echo exit /b so the script would run and you can see the results instead of actual exit /b.

@echo off setlocal enabledelayedexpansion rem first we will inspect the 10th and 11th digits from the right set foldsize=7000000010 rem If there is any number in the 11th spot, then it must be greater if not "!foldsize:~0,-10!"=="" ( rem the number is at least 10000000000 which is greater so we exit echo exit /B 0 ) rem check if the 10th digit is 6 or greater rem add a zero to avoid issues with an undefined variable set smallfoldsize=!foldsize:~0,-9! if !smallfoldsize!0 GTR 50 ( rem technically the number is now greater or equal to 6000000000 rem I will leave it up to you if you really need it to be greater than echo exit /B 0 ) else ( rem it is smaller echo exit /B 1999 ) rem an alternative is to discard the N least significant digits rem I am stripping the last 7 digits and then adding a zero to avoid rem an undefined variable set smallfoldsize=!foldsize:~0,-7!0 if !smallfoldsize! GEQ 6000 ( echo exit /B 0 ) else ( echo exit /B 1999 ) rem again, this is actual greater or equal. If you rem need greater than, you have to look at the less rem significant digits when smallfoldsize = 6000 
Sign up to request clarification or add additional context in comments.

1 Comment

Well, the maximum number is 2 ^ 31 - 1, and this equals 2147483647...
0

avery_larry's helpful answer does a great job of explaining the fundamental problems with your approach (lexical rather than numerical comparison, numbers that are too large).

If your comparison doesn't need to be accurate down to the byte level, you can try the following approach, which enables a simpler solution:

  • Make PowerShell return a value in mebibytes (MiB) rather than bytes.

  • This should return a small enough number that you can compare (numerically) with cmd's GTR operator.

@echo off setlocal enabledelayedexpansion for /f "delims=" %%a in (' powershell -noprofile -c ^ "[math]::Ceiling((Get-ChildItem -File -Recurse \"C:\XSTOREDB\xstore_data.mdf\" | Measure-Object -Property Length -Sum).Sum / 1mb)" ') do set "foldsize=%%a" :: # 6000000000 bytes is ca. 5722 MB if !foldsize! GTR 5722 ( exit /B 0 ) else ( exit /B 1999 ) 

Note how the PowerShell command divides the result by 1mb (1 MiB (mebibyte) = 2 ^ 20 = 1,048,576 bytes) and then rounds it up via [math]::Ceiling()

Also note how the embedded " chars. were escaped as \" to ensure that PowerShell sees them.

Finally, note how, as a small optimization, -File was passed to the Get-ChildItem cmdlet in order to skip directories (themselves, not their files), given that directories themselves have no Length property.

Comments

0

Despite your question body, text, and title, not matching the code you have supplied, I do not see any need for a for this task.

Based upon your question body text, and title, to exit with an errorlevel of 0 if file C:\XSTOREDB\xstore_data.mdf is greater than 6GB:

@"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoP "If(Test-Path -L "C:\XSTOREDB\xstore_data.mdf"){(GI -L "C:\XSTOREDB\xstore_data.mdf").Length -GT 6GB}" 2>NUL|"%__AppDir__%find.exe" "T">NUL&&(Exit /B 0)||Exit /B 1999 

Based upon your question code, to exit with an errorlevel of 0 if directory C:\XSTOREDB totals more than 6GB:

@"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoP "If(Test-Path -L "C:\XSTOREDB"){(GCI "C:\XSTOREDB" -Rec|Measure Length -Su -EA Stop).Sum -GT 6GB}" 2>NUL|"%__AppDir__%find.exe" "T">NUL&&(Exit /B 0)||Exit /B 1999 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.