如何获得的实际大小,在磁盘上的PowerShell中的文件? [英] How to get the actual size-on-disk of a file from PowerShell?

查看:230
本文介绍了如何获得的实际大小,在磁盘上的PowerShell中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个管理脚本,我需要计算磁盘上的文件的大小。

I am writing an administrative script, and I need to calculate the size of files on disk.

这些文件是在一个COM pressed NTFS卷。

These files are on a compressed NTFS volume.

我不能使用 FileInfo.Length ,因为这是文件的大小,而不是大小在磁盘上。举例来说,如果我有一个100MB的文件,但它仅使用25MB由于NTFS COM pression,我需要我的脚本返回25MB。

I can't use FileInfo.Length, because that is file size and not size on disk. For example, if I have a 100MB file, but it is only using 25MB due to NTFS compression, I need my script to return 25MB.

有没有办法做到这一点在PowerShell中?

Is there a way to do this in Powershell?

(我了解 GetCom pressedFileSize() Win32调用,但我希望这已经wrappered在一定程度上。)

(I know about the GetCompressedFileSize() Win32 call, but I was hoping that this is already wrappered at some level.)

推荐答案

(编辑)

我想通了,如何动态的属性(称为脚本属性)添加到文件对象,所以现在,我可以使用语法:$ theFileObject.Com pressedSize读取尺寸

I figured out how to dynamically add a property (called a "script property") to the Fileobject, so now, I can use the syntax: $theFileObject.CompressedSize to read the size.

(编辑完)

阅读Goyuix的反应,我觉得很酷,但不存在某种在PowerShell中类型的扩展能力?。所以,后来我发现这个在Scott Hanselman帖子:的http://www.hanselman.com/blog/MakingJunctionsReparsePointsVisibleInPowerShell.aspx

Read Goyuix's response, and I thought "Cool, but isn't there some kind of type-extension capability in Powershell?". So then I found this Scott Hanselman post: http://www.hanselman.com/blog/MakingJunctionsReparsePointsVisibleInPowerShell.aspx

和我创建了一个脚本属性的FileInfo对象:.com的pressedSize

And I created a Script Property for the FileInfo object: CompressedSize.

下面是我所做的:(注:我是很新的PowerShell的,或者至少我不使用它太多这也许可以做出好了很多,但这里是我所做的:

Here's what I did: (note: I'm quite new to Powershell, or at least I don't use it much. This could probably be made a lot better, but here's what I did:

首先,我从Goyuix的编译后的Ntfs.ExtendedFileInfo。我把DLL在我的PowerShell配置文件目录(文件\ WindowsPowershell)

First, I compiled the Ntfs.ExtendedFileInfo from Goyuix's post. I put the DLL in my Powershell profile directory (Documents\WindowsPowershell)

接下来,我创建了我的个人资料目录中的文件名为My.Types.ps1xml。

Next, I created a file in my profile directory named My.Types.ps1xml.

我把下面的XML到文件:

I put the following XML into the file:

<Types>
<Type>
    <Name>System.IO.FileInfo</Name>
    <Members>
        <ScriptProperty>
          <Name>CompressedSize</Name>
          <GetScriptBlock>
            [Ntfs.ExtendedFileInfo]::GetCompressedFileSize($this.FullName)
          </GetScriptBlock>
        </ScriptProperty>
    </Members>
</Type>
</Types>

这code(合并后入式系统),将一个名为com pressedSize属性动态添加到通过GET-childitem / DIR返回FileInfo的对象。但是PowerShell不会了解code着呢,不知道我的DLL呢。我们处理,在下一步的:

That code (once merged into the type system) will dynamically add a property named CompressedSize to the FileInfo objects that are returned by get-childitem/dir. But Powershell doesn't know about the code yet, and it doesn't know about my DLL yet. We handle that in the next step:

编辑Profile.ps1。在同一目录下。现在,我的档案文件恰好已经在它的一些东西,因为我有社区扩展了PowerShell的安装。但愿,我包括你需要在接下来的code段,这样它会工作,即使一台机器,不具有扩展的一切。添加以下code到Profile.ps1:

Edit Profile.ps1. in the same directory. Now, my Profile file happens to already have some stuff in it because I have the Community Extensions for powershell installed. Hopefully, I'm including everything that you need in this next code snippet, so that it will work even on a machine that does not have the extensions. Add the following code to Profile.ps1:

#This will load the ExtendedfileInfo assembly to enable the GetCompressedFileSize method.  this method is used by the
#PSCompressedSize Script Property attached to the FileInfo object.
$null = [System.Reflection.Assembly]::LoadFile("$ProfileDir\ntfs.extendedfileinfo.dll") 

#merge in my extended types
$profileTypes = $ProfileDir | join-path -childpath "My.Types.ps1xml"
Update-TypeData $profileTypes

现在,我引用了$ ProfileDir变量之前定义在我Profile.ps1脚本。万一它不是你的,这里的定义是:

Now, the $ProfileDir variable that I reference is defined earlier in my Profile.ps1 script. Just in case it's not in yours, here is the definition:

$ProfileDir = split-path $MyInvocation.MyCommand.Path -Parent

就是这样。接下来,您运行PowerShell的时候,你可以访问FileInfo对象上的COM pressedSize财产就好像它是任何其他财产。 例如:

That's it. The next time that you run Powershell, you can access the CompressedSize property on the FileInfo object just as though it is any other property. Example:

$ MYFILE = DIR C:\ TEMP \ myfile.txt的

$myFile = dir c:\temp\myfile.txt

$ myFile.Com pressedSize

$myFile.CompressedSize

这工作(在我的机器上,反正),但我很乐意听到它是否适合在与最佳实践。有一件事我知道我做错了:在Profile.ps1文件,我返回的LoadFile的结果转化成我不会用($空=等等等等)的变量。我这样做是为了晚饭preSS加载文件到控制台的结果的显示。有可能是一个更好的办法来做到这一点。

This works (on my machine, anyway), but I would love to hear whether it fits in with best practices. One thing I know I'm doing wrong: in the Profile.ps1 file, I return the results of LoadFile into a variable that I'm not going to use ($null = blah blah). I did that to suppress the display of result of load file to the console. There is probably a better way to do it.

这篇关于如何获得的实际大小,在磁盘上的PowerShell中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆