11

I have tried $filesize=(($fileXP).length/1KB) but this does not retrieve the correct answer. As for my understanding, it is just dividing the number of characters in files with KB and giving the answer which is wrong.

There has to be some function or attribute to define the size directly and display as we want in KB or MB or GB as required.

Cookie Ninja
  • 1,156
  • 15
  • 29
Svatkat
  • 119
  • 1
  • 1
  • 3
  • I suspect I can find a dupe target for this but we cannot explain your problem until I see how you made `$fileXP` – Matt Jun 27 '16 at 11:52
  • 3
    *"this does not retrive the correct answer"* -- So... What is `$fileXP`? What is `($fileXP).length`? And what is `(($fileXP).length/1KB)`? Possible duplicate: http://stackoverflow.com/questions/24616806/powershell-display-file-size-as-kb-mb-or-gb – Tom Lord Jun 27 '16 at 11:52
  • If `$fileXP` is a `FileInfo` object as returned by `Get-Item` or `Get-ChildItem`, then the `Length` property is the *number of bytes* not (necessarily) "number of characters" in the file – Mathias R. Jessen Jun 27 '16 at 12:08
  • Hi All, $filexp is the variable has .xml file stored $filesize is the variable that holds the output of the size of the that .xml file So it is the simple operartion to pcalcute the size of the file and store it in a variable – Svatkat Jun 28 '16 at 10:12

4 Answers4

12

To get the file sizes in KB:

Get-ChildItem | % {[int]($_.length / 1kb)}

Or to round up to the nearest KB:

Get-ChildItem | % {[math]::ceiling($_.length / 1kb)}

To get the number characters:

Get-ChildItem | % {(Get-Content $_.FullName).length}

Compare the output and you will see that the length of Get-Childitem is in KB.

Dave Sexton
  • 10,768
  • 3
  • 42
  • 56
  • 1
    But Dave, i have the file stored in $file and now if i have to calculate the size directly and then print it out. So this cant be the solution, Can you advise the solution for that – Svatkat Jun 28 '16 at 09:56
  • Can you show the code that assigns to `$file`? Unless I know what it contains I can't help. – Dave Sexton Jun 28 '16 at 13:34
  • $fileXP = "XXSDSKILLSXP_data.xml" $Directory = "C:\Interfaces\" if (Test-Path $Directory$fileXP) $filesize=($fileXP.length/1KB) Here i have a stored file in the path i have defines the path and file in variables. i am using as above to find the size – Svatkat Jun 29 '16 at 13:02
  • So $fileXP does contain a file, just a string that happens to be a file name. The length for your variable would be just the number of chars in the string. To get a file object you will have to pass you string to something that returns a file object, something like: `(Get-ChildItem $fileXP).length`. – Dave Sexton Jun 29 '16 at 14:52
  • Yes, i have used this (Get-ChildItem $fileXP).length But i dont think i can store the output of the above into another String Variable and then Use that variable for mailing in HTML. – Svatkat Jun 30 '16 at 06:15
  • Why do think this can't be done? How about `$x = (Get-ChildItem $fileXP).length.ToString()` or even `$x = [string]((Get-ChildItem $fileXP).length)`. – Dave Sexton Jun 30 '16 at 08:33
  • Thanks Dave, I wasnt able ro produce teh correct answer because it want converted to String. Thanks for you Help. – Svatkat Jun 30 '16 at 10:45
2

First:

Get-Item -Path 'C:\Windows\notepad.exe' | Select -Property Name, Length | Foreach {
    #if($_.('Name').Trim() -eq "notepad.exe") {
        $FileSize = $_.Length
    #}
};

Wow ... directly and display as we want in KB, or MB or GB? I have not seen COM functions that provide such in-depth information. This is the limit I saw, run this:

<#The word 'Properties'/'Сво&йства'(ru) should be here, for the English version of#> 
<#Windows with "&" somewere. What is shown when the right mouse button#> 
<#to file/folder is pressed#>
$itemName = 'Сво&йства';
$fullPath='C:\Windows';
$o = new-object -com Shell.Application;
$folder = $o.NameSpace((Split-Path -Path $fullPath));
$file = $folder.ParseName((Split-Path -Leaf $fullPath));
$file.Verbs() | foreach ({
    if($_.Name -eq $itemName){$_.DoIt()}
});

Due to the fact that Powershell has the same sources as WMIC, where data is sent in bytes, I think that the solution may be to crack some dll file, well ... or completely repeat the logic of microsoft in obtaining information of this type?

I would really like it if I was wrong. This would simplify some of my past executable code decisions.

Garric
  • 591
  • 3
  • 10
  • this does not answer the Question. – Lee_Dailey Apr 07 '21 at 09:26
  • #Wow ... directly and display as we want in KB, or MB or GB? I have not seen COM functions that provide such in-depth information. This is the limit I saw, run this: $itemName = 'Сво&йства';<#The word 'Properties' should be here, for the English version of Windows with "&" somewere. What is shown when the right mouse button is pressed#> $fullPath='C:\Windows'; $o = new-object -com Shell.Application; $folder = $o.NameSpace((Split-Path -Path $fullPath)); $file = $folder.ParseName((Split-Path -Leaf $fullPath)); $file.Verbs() | foreach ({ if($_.Name -eq $itemName){$_.DoIt()} }); – Garric Apr 08 '21 at 12:52
1
CLS

$filenames=Get-Content "C:\temp\files\files-to-watch-size.txt" **#Reading the names of the files to test the existance and size of the file, name of fles with full path in each line e.g. C:\test.txt**
foreach ($filename in $filenames) 
{

if (Test-Path $filename) { (Get-Item $filename).length -gt 0kb **#Returns TRUE if size is greater than zero KB**
$size=(Get-Item $filename).length/1024  **#Getting the File size in KB**
Write-Host "$filename is $size KB" } **# display the name of the file and its size in KB**
}
M-A Charlotte
  • 325
  • 1
  • 3
  • 10
  • The question is how to get a file's size in kilobytes. This is for some reason iterating a list of files, for each one outputting a boolean indicating if it has data or not and writing to the host the size in bytes. Where is the size in kilobytes? [Dave Sexton's answer](https://stackoverflow.com/a/38058434/150605) from three years ago already addresses this, anyways. – Lance U. Matthews Jul 23 '19 at 23:19
  • 1
    Just divide the byte value by 1024 to get KB e.g. "$size=(Get-Item $filename).length/1024" you can convert the byte value to KB, MB, GB or whatever just by dividing by appropriate number! – M-A Charlotte Jul 24 '19 at 15:47
  • 1
    I've found this to be slightly speedier than get-item, though that difference is only a few Milliseconds. ([System.IO.FileInfo] $path).length / 1kb – superluminal Nov 18 '20 at 22:57
0

This will give you the bytes (* to get all files... could have used e.g. *.mp3):

 Get-ChildItem * | Measure-Object -Property Length -sum

This will give you the size in MB:

 "{0} MB" -f ((Get-ChildItem * -Recurse | Measure-Object -Property Length -S -ErrorAction Stop).Sum / 1MB)

etc.

ntg
  • 12,950
  • 7
  • 74
  • 95