2010-02-19 189 views
0

我有一個vbs腳本,它捕獲文件信息,然後將它導出到csv文件。我需要在C:\,E:\,I:\等主驅動器上運行腳本,但是每次運行主目錄時,我都會嘗試運行子文件夾示例的「權限被拒絕」 C:\ Program Files文件工作正常。我已經通過完整的管理員帳戶在不同的臺式機和服務器上測試過這些,並且仍然可以獲得。權限在運行VBScript時被拒絕

這段代碼有什麼問題。 test.vbs

Option Explicit 
Dim objFS, objFld 
Dim objArgs 
Dim strFolder, strDestFile, blnRecursiveSearch 
Dim strLines() 
Dim i 
Dim strCsv 

    i = 0 

' 'Get the commandline parameters 
' Set objArgs = WScript.Arguments 
' strFolder = objArgs(0) 
' strDestFile = objArgs(1) 
' blnRecursiveSearch = objArgs(2) 

    '################################### 
    'MAKE SURE THESE VALUES ARE CORRECT 
    '################################### 
    strFolder = "C:\" 
    strDestFile = "C:\Output.csv" 
    blnRecursiveSearch = True 

    'Create the FileSystemObject 
    Set objFS=CreateObject("Scripting.FileSystemObject") 
    'Get the directory you are working in 
    Set objFld = objFS.GetFolder(strFolder) 

    'Now get the file details 
    GetFileDetails objFld, blnRecursiveSearch 

    'Write the csv file 
    Set strCsv = objFS.CreateTextFile(strDestFile, True) 
    strCsv.Write Join(strLines, vbCrLf) 

    'Close and cleanup objects 
    strCsv.Close 
    Set strCsv = Nothing 
    Set objFld = Nothing 
    Set strFolder = Nothing 
    Set objArgs = Nothing 


Private Sub GetFileDetails(fold, blnRecursive) 
Dim fld, fil 
dim strLine(5) 

    If blnRecursive Then 
     'Work through all the folders and subfolders 
     For Each fld In fold.SubFolders 
      GetFileDetails fld, True 
     Next 
    End If 

    'Now work on the files 
    For Each fil in fold.Files 
     strLine(0) = fil.Path 
     strLine(1) = fil.Type 
     strLine(2) = fil.Size 
     strLine(3) = fil.DateCreated 
     strLine(4) = fil.DateLastModified 
     strLine(5) = fil.DateLastAccessed 

     Redim Preserve strLines(i) 
     strLines(i) = Join(strLine, ",") 
     i = i + 1 
    Next 
end sub 

如果您知道問題所在,請告知並修改代碼。

+0

你用什麼命令來運行該文件? – 2010-02-19 14:04:01

+0

cscript.exe C:\ temp \ script \ test.vbs – MalsiaPro 2010-02-19 14:05:27

+0

你會問多少次同樣的問題? http://stackoverflow.com/questions/2279705/vbscript-issue-help-required http://stackoverflow.com/questions/2272317/vbs-script-help – Tester101 2010-02-19 14:46:16

回答

0

如果這是一個權限問題,我會強烈建議從Sysinternals Process Monitor診斷它。你應該能夠看到cscript進程(或者任何正在執行你的腳本),並找出你有什麼樣的權限問題。

+0

我沒有得到這個問題與任何其他腳本思想,並不能看代碼出現問題的位置。我認爲它只是指定搜索文件夾而不是主驅動器目錄。錯誤似乎在第50行,第3行。 – MalsiaPro 2010-02-19 14:08:57

+0

C:\ script \ test.vbs(50,3)Microsoft VBScript運行時錯誤:權限被拒絕 – MalsiaPro 2010-02-19 14:14:31