VMware Cloud Community
dalo
Hot Shot
Hot Shot
Jump to solution

filesize

How can I get the filesize of a specific file on a VMFS?

Something like: \[datastore1\]/folder1/file1

I tried around with "SearchDatastoreSubFolders" but I didn't found a way. I found some examples, but they all list all files on the datastore.

Daniel

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Have a look at .

In the script I wrote for that thread you could change the matchpattern property of the HostDatastoreBrowserSearchSpec object to contain the name of the file for which you want to know the size.

...
$searchSpec.matchpattern = "file1"
...

Another option is to get all files in the folder ("\[datastore1\]/folder1" and then use a loop to find the specific file you are looking for.

...
foreach ($result in $task.info.Result){
  foreach($file in $result.File){
    if($file.Path -eq "file1"){
      $file.Path $file.fileSize
    }
  }
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Have a look at .

In the script I wrote for that thread you could change the matchpattern property of the HostDatastoreBrowserSearchSpec object to contain the name of the file for which you want to know the size.

...
$searchSpec.matchpattern = "file1"
...

Another option is to get all files in the folder ("\[datastore1\]/folder1" and then use a loop to find the specific file you are looking for.

...
foreach ($result in $task.info.Result){
  foreach($file in $result.File){
    if($file.Path -eq "file1"){
      $file.Path $file.fileSize
    }
  }
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dalo
Hot Shot
Hot Shot
Jump to solution

Thank you LucD, this was very helpful. I modified your code and it works as expected:

$DSName = "VMFS_01"
$sPattern = "*delta.vmdk"

$ds = Get-Datastore -Name $DSName | Get-View
$dsBrowser = Get-View -Id $ds.Browser

$datastorepath = ""

$searchspec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchSpec.matchpattern = $sPattern
$searchSpec.details = New-Object VMware.Vim.FileQueryFlags
$searchSpec.details.fileSize = $true

$taskMoRef = $dsBrowser.SearchDatastoreSubFolders_Task($datastorePath, $searchSpec)
$task = Get-View $taskMoRef
while ($task.Info.State -eq "running"){$task = Get-View $taskMoRef}

foreach ($result in $task.info.Result){
  foreach($file in $result.File){
    Write-Host $file.Path $file.FileSize
  }
}

0 Kudos