4

我有一个脚本,可以获取有关特定目录中当前文件夹和子文件夹的信息。它工作得很好,但我偶然发现了一个奇怪的问题:

dim FSO, objFolder, datafolder, foldername, objSubfolder, totalSize
dim objSubfolder2, objFolder2, mSize, size, today, dateLastMod

foldername = "D:\folder\subfolder"
set FSO = CreateObject("Scripting.FileSystemObject")
set objFolder = FSO.GetFolder(foldername) 
set colSubfolders = objFolder.Subfolders
today = Now
ShowFolderDetails objFolder

Function ShowFolderDetails(oF)

    datafolder = oF.Size/1073741824
    wscript.echo oF.Name & " :Size= " & datafolder & " GB"
    wscript.echo oF.Name & " #Files= " & oF.Files.Count
    wscript.echo oF.Name & " #Folders= " & oF.Subfolders.count
    wscript.echo oF.Name & " Date Last Modified= " & oF.DateLastModified
    totalSize = totalSize + datafolder

end Function

还有更多要遵循的,但我的问题是path not found当我调用该函数时会得到一个。

该文件夹不在 C:\ 驱动器上 - 这应该不是问题。我已经完成了相同的脚本,但更改了foldername = D:\folder\differentsubfolder它完美的工作。但是当我把它改回另一个文件夹时,它给了我一个找不到路径的错误。

我还尝试将 set FSO = CreatObject("Scripting.FileSytemObject") 下面的所有内容放在 IF 语句中:

IF FSO.FolderExists(文件夹名) 然后 ....

确实进入了那个 IF 语句,这让我相信 VBS 看到了它,但我仍然在line 17 (datafolder = oF.Size/1073741824).

我尝试放入变量文件夹名称所在的完整文件夹路径(用引号括起来)。

我尝试运行指向其他目录的 vbs,它运行 100%。它只是那个特定的文件夹。文件夹名称中没有空格。还有什么我想念的吗?我对 D:\

4

2 回答 2

2

我敢打赌,原因不是隐藏文件或文件夹,而是诸如 Thumbs.db 之类的系统文件位于许多包含图像的文件夹中,也可以看到它们编辑文件夹选项以查看系统文件,删除系统文件并重试。当您询问文件夹的大小时,您需要访问其下的所有文件夹和文件。如果您在例如您的个人资料文件夹上尝试您的脚本,您肯定会得到错误,该文件夹的管理员或所有者与否。

如果有帮助,这里有一个脚本,它提供文件的属性

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("c:\ruby193.zip")
If objFile.Attributes AND 0 Then
    Wscript.Echo "No attributes set."
End If    
If objFile.Attributes AND 1 Then
    Wscript.Echo "Read-only."
End If    
If objFile.Attributes AND 2 Then
    Wscript.Echo "Hidden file."
End If    
If objFile.Attributes AND 4 Then
    Wscript.Echo "System file."
End If    
If objFile.Attributes AND 32 Then
    Wscript.Echo "Archive bit set."
End If    
If objFile.Attributes AND 64 Then
    Wscript.Echo "Link or shortcut."
End If    
If objFile.Attributes AND 2048 Then
    Wscript.Echo "Compressed file."
End If
于 2012-06-14T15:53:14.400 回答
1

文件夹的 .Size 属性是动态计算的。因此,该子树中的所有分支和叶子对于操作的成功至关重要。我的 C:\ 包含一个隐藏的系统文件夹“系统卷信息”,我可以获得以下的 .Name 但不是 .Size:

>> sFP = "System Volume Information"
>> If goFS.FolderExists(sFP) Then WScript.Echo goFS.GetFolder(sFP).Name
>>
System Volume Information
>> If goFS.FolderExists(sFP) Then WScript.Echo goFS.GetFolder(sFP).Size
>>
Error Number:       70
Error Description:  Permission denied

我承认“权限被拒绝”不是“找不到路径”,但似乎某些子文件夹的属性或权限可能是罪魁祸首。

更新

为了进行测试,我让我的 root 搞乱了我的 linux 共享上映射为 e:\bin 的文件夹。根看到:

bin
[-rwx------ eh         16]  bin/dragit-ssh.sh
[lrwxrwxrwx eh         33]  bin/komodo -> /home/eh/Komodo-Edit-6/bin/komodo
[drwxr-xr-x eh       4.0K]  bin/pics
[-rwxr--r-- eh       6.0K]  bin/pics/Thumbs.db
[-rwxr--r-- eh        20K]  bin/pics/jsa.JPG
[-rwx------ root      10K]  bin/pics/x
[-rwxr-xr-x eh         45]  bin/rhinos.sh
[drwx------ root     4.0K]  bin/rootsown
[-rwxr-xr-x root      10K]  bin/rootsown/x
[-rwx------ eh        523]  bin/showpath.sh
[-rwxr--r-- eh        325]  bin/sp6p.sh

2 directories, 9 files

14392   /home/eh/bin/rootsown
40595   /home/eh/bin/pics
60025   /home/eh/bin

在 linux 上,我可以看到:

bin
[-rwx------ eh         16]  bin/dragit-ssh.sh
[lrwxrwxrwx eh         33]  bin/komodo -> /home/eh/Komodo-Edit-6/bin/komodo
[drwxr-xr-x eh       4.0K]  bin/pics
[-rwxr--r-- eh       6.0K]  bin/pics/Thumbs.db
[-rwxr--r-- eh        20K]  bin/pics/jsa.JPG
[-rwx------ root      10K]  bin/pics/x
[-rwxr-xr-x eh         45]  bin/rhinos.sh
[drwx------ root     4.0K]  bin/rootsown [error opening dir]
[-rwx------ eh        523]  bin/showpath.sh
[-rwxr--r-- eh        325]  bin/sp6p.sh

2 directories, 8 files

4096    bin/rootsown
40595   bin/pics
49729   bin

两个重要的事实:我不允许“查看”rootsown 目录,所以我看不到 bin/rootsown/x 或调整其大小;但是 bin/pics/x 的大小并不是什么秘密,尽管我被禁止阅读、更改或执行它。

VB脚本:

>> sf = "e:\bin\pics"
>> WScript.Echo goFS.GetFolder(sf).Size
>>
36499

您可以获得包含讨厌文件的文件夹的 .Size 。

>> sf = "e:\bin"
>> WScript.Echo goFS.GetFolder(sf).Size
>>
Error Number:       70
Error Description:  Permission denied

您无法获得包含讨厌的子(子...)文件夹的文件夹的 .Size 。

>> sf = "e:\bin\pics\rootsown"
>> WScript.Echo goFS.GetFolder(sf).Size
>>
Error Number:       76
Error Description:  Path not found

当您询问讨厌的文件夹的 .Size 时,您会收到“找不到路径”错误

基于此,我愿意接受彼得的赌注。如果您可以通过更改文件的属性或权限来证明可以使父文件夹的 .Size 分别成功。失败,我将支付 10 欧元,- 给我遇到的下一个无家可归者。

不起眼的目录:

要获得文件夹的大小,我会先尝试

dir /s e:\bin
Volume in drive E is eh
Volume Serial Number is 0ED6-233C

Directory of e:\bin

4.06.2012  18:42    <DIR>          .
4.06.2012  08:04    <DIR>          ..
2.01.2012  12:21                45 rhinos.sh
3.06.2012  22:55    <DIR>          rootsown
3.10.2011  16:42               325 sp6p.sh
4.06.2012  19:46    <DIR>          pics
1.07.2010  23:34               523 showpath.sh
8.10.2010  16:57               582 komodo
4.05.2010  12:53                16 dragit-ssh.sh
              5 File(s)          1.491 bytes

Directory of e:\bin\pics

4.06.2012  19:46    <DIR>          .
4.06.2012  18:42    <DIR>          ..
5.08.2011  10:22            10.296 x
0.07.2008  03:44             6.144 Thumbs.db
9.06.2012  23:29            20.059 jsa.JPG
              3 File(s)         36.499 bytes

    Total Files Listed:
              8 File(s)         37.990 bytes
              6 Dir(s)  29.060.050.944 bytes free

似乎 dir 知道我被允许知道的内容,并且它并没有被讨厌的文件夹过度干扰。

使用 dir 的脚本:

Option Explicit

Dim reX  : Set reX  = New RegExp
reX.Pattern = "Directory\s+of\s+(.+?)\r[\s\S]+?Total[\s\S]+?([.\d]+\sbytes)"
Dim oMTS : Set oMTS = reX.Execute(WScript.StdIn.ReadAll())
If 1 = oMTS.Count Then
   WScript.Echo "Size of", oMTS(0).SubMatches(0), "=>", oMTS(0).SubMatches(1)
Else
   WScript.Echo "Bingo!"
End If

样品用途:

dir /s e:\bin | cscript folsiz2.vbs
Size of e:\bin => 37.990 bytes

RegExp 模式搜索

Directory\s+of\s+            The first "Directory of "
(.+?)                        capture the path of the folder, that is
                             the sequence of 'everything except \n' but non greedy, so
\r                           the first \r will not be included in the capture
[\s\S]+?                     non greedy sequence of 'really everything (space or non-space)'
Total                        until "Total" is found
[\s\S]+?                     advance but stop for the first
([.\d]+\sbytes)              sequence of . or digits followed by " bytes", capture
                             that because that is the first sum after Total
于 2012-06-13T20:43:29.117 回答