VBS can't find directory - 'Path Not Found'
-
14-06-2021 - |
문제
I have a script that obtains information about the current folders and subfolders within a specific directory. It works great, but I have stumbled across a strange issue:
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
And there is more to follow, but my issue is I get a path not found
when I call that function.
The folder is not located on the C:\ drive - which shouldn't be an issue.
I have done this same script but changed foldername = D:\folder\differentsubfolder
which works perfectly. But when I change it back to the other folder, it gives me a path not found error.
I also tried putting everything below set FSO = CreatObject("Scripting.FileSytemObject") within an IF statement:
IF FSO.FolderExists(foldername) Then ....
This does enter in that IF statement, which makes me believe the VBS sees it, but I still get that error at line 17 (datafolder = oF.Size/1073741824)
.
I have tried putting in the full folder path where the variable foldername is located (surrounded in quotes).
I tried running my vbs pointed to other directories and it runs 100%. Its just that specific folder. There are no spaces in the folder name. Is there anything else I am missing? I have full admin access to the D:\
해결책
I bet it isn't a hidden file or folder that is the cause but a system file like eg Thumbs.db thas sits in many folders containing images, too see them edit your folder options to see systemfiles, remove the systemfile and try again. When you ask the size of a folder you need access to ALL folders and files beneath it. If you try your script on eg your profilefolder you certainly will get errors, administrator or owner of that folder or not.
In case it could help, here a script that gives the attributes from a file
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
다른 팁
The .Size property of a folder is computed on the fly. So all branches and leaves in that sub tree are critical for the success of the operation. My C:\ contains a hidden system folder "System Volume Information" which I can get the .Name but not .Size of:
>> 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
I admit that "Permission denied" is not "path not found", but it seems probable that the attributes or the permissions for some subfolder(s) are to blame.
UPDATE
For testing I asked my root to mess up a folder on my linux share that is mapped as e:\bin. root sees:
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
On linux, I'm allowed to see:
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
Two important facts: I'm not allowed to 'look into' the rootsown directory, so I can't see or size bin/rootsown/x; but the size of bin/pics/x is no secret, although I'm forbidden to read, change, or execute it.
VBScript:
>> sf = "e:\bin\pics"
>> WScript.Echo goFS.GetFolder(sf).Size
>>
36499
You can get the .Size of folder containing nasty files.
>> sf = "e:\bin"
>> WScript.Echo goFS.GetFolder(sf).Size
>>
Error Number: 70
Error Description: Permission denied
You can't get the .Size of a folder containing a nasty sub (sub...) folder.
>> sf = "e:\bin\pics\rootsown"
>> WScript.Echo goFS.GetFolder(sf).Size
>>
Error Number: 76
Error Description: Path not found
You get a "Path not found" error, when you ask for the .Size of a nasty folder
Based on this, I am willing to pick up peter's bet. If you can show that by changing the attributes or permissions of a file you can make the parent folder's .Size succeed resp. fail, I'll pay Euro 10,- to the next homeless person I meet.
The humble dir:
To obtain the size of a folder, I would first try
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
It seems that dir knows what I'm allowed to know and that it isn't unduly disturbed by nasty folders.
A script that uses 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
sample use:
dir /s e:\bin | cscript folsiz2.vbs
Size of e:\bin => 37.990 bytes
The RegExp pattern searchs for
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