我有一个脚本可以获取有关特定目录中当前文件夹和子文件夹的信息。它运作得很好,但我偶然发现了一个奇怪的问题:
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
,它完美无缺。但当我将其更改回另一个文件夹时,它会给我一个找不到错误的路径。
我还尝试在IF语句中设置FSO = CreatObject(“Scripting.FileSytemObject”)下面的所有内容:
如果FSO.FolderExists(foldername)那么......
确实输入该IF语句,这让我相信VBS会看到它,但我仍然在line 17 (datafolder = oF.Size/1073741824)
收到该错误。
我试过放入变量foldername所在的完整文件夹路径(用引号括起来)。
我尝试运行我的vbs指向其他目录,它运行100%。它就是那个特定的文件夹。文件夹名称中没有空格。 还有什么我想念的吗?我拥有对D:\
的完全管理员权限答案 0 :(得分:2)
我敢打赌它不是隐藏文件或文件夹的原因,但系统文件如Thumbs.db thas位于包含图像的许多文件夹中,也看到它们编辑文件夹选项以查看系统文件,删除系统文件和再试一次。当您询问文件夹的大小时,您需要访问其下的所有文件夹和文件。如果你在例如你的个人资料文件夹上试用你的脚本,你肯定会得到该文件夹的错误,管理员或所有者。
如果它可以提供帮助,这里有一个脚本,它提供文件中的属性
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
答案 1 :(得分:1)
文件夹的.Size属性是即时计算的。因此,该子树中的所有分支和叶子对于操作的成功至关重要。我的C:\包含一个隐藏的系统文件夹“System Volume Information”,我可以获得.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
我承认“Permission denied”不是“找不到路径”,但似乎有些子文件夹的属性或权限可能是罪魁祸首。
<强>更新强>
为了测试,我让我的root弄乱了我映射的linux共享上的文件夹 如e:\ bin。 root见:
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
两个重要的事实:我不允许'调查'根据目录,所以 我看不到或大小bin / rootsown / x;但是bin / pics / x的大小并不是秘密, 虽然我被禁止阅读,更改或执行它。
的VBScript:
>> 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知道我允许知道什么,并且它不是过度的 被令人讨厌的文件夹打扰。
使用目录的脚本:
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