2010-02-22 36 views
1

我得到了下面的代碼來獲取信息的文件指定的驅動器上,我跑了劇本反對票600 GB硬盤驅動器上我們的服務器之一,一段時間後,我得到的錯誤VBScript中出的字符串空間

超出字符串空間; 「加入」。 34號線,字符2

對於此代碼,文件script.vbs

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

'' i = 0 

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

    '######################################## 
    'SPECIFY THE DRIVE YOU WANT TO SCAN BELOW 
    '######################################## 
    strFolder = "C:\" 
    strDestFile = "C:\InformationOutput.csv" 
    blnRecursiveSearch = True 

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

    'Open the csv file 
    Set strCsv = objFS.CreateTextFile(strDestFile, True) 

'' 'Write the csv file 
'' Set strCsv = objFS.CreateTextFile(strDestFile, True) 
    strCsv.WriteLine "File Path,File Size,Date Created,Date Last Modified,Date Last Accessed" 
'' strCsv.Write Join(strLines, vbCrLf) 

    'Now get the file details 
    GetFileDetails objFld, blnRecursiveSearch 

'' 'Close and cleanup objects 
'' strCsv.Close 

'' 'Write the csv file 
'' Set strCsv = objFS.CreateTextFile(strDestFile, True) 
'' For i = 0 to UBound(strLines) 
'' strCsv.WriteLine strLines(i) 
'' Next 

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


'---------------------------SCAN SPECIFIED LOCATION------------------------------- 
Private Sub GetFileDetails(fold, blnRecursive) 
Dim fld, fil 
dim strLine(4) 

on error resume next 
    If InStr(fold.Path, "System Volume Information") < 1 Then 
     If blnRecursive Then 
      'Work through all the folders and subfolders 
      For Each fld In fold.SubFolders 
       GetFileDetails fld, True 
       If err.number <> 0 then 
        LogError err.Description & vbcrlf & "Folder - " & fold.Path 
        err.Clear 
       End If 
      Next 
     End If 

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

     strCsv.WriteLine Join(strLine, ",") 


      if err.number <> 0 then 
       LogError err.Description & vbcrlf & "Folder - " & fold.Path & vbcrlf & "File - " & fil.Name 
       err.Clear 
      End If 
     Next 
    End If 
end sub 

Private sub LogError(strError) 
dim strErr 
    'Write the csv file 
    Set strErr = objFS.CreateTextFile("C:\test\err.log", false) 
    strErr.WriteLine strError 
    strErr.Close 

    Set strErr = nothing 

End Sub 

RunMe.cmd

wscript.exe "C:\temp\script\script.vbs" 

我怎樣才能避免收到此錯誤?服務器驅動器是相當有點< ????我想象,CSV文件將至少40 MB。

編輯Guffa:
我在代碼中註釋了一些行,使用雙蜱(''),所以你可以看到在哪裏。

回答

2

這條線連接所有行成一個巨大的字符串,並寫入文件:

strCsv.Write Join(strLines, vbCrLf) 

相反,寫行逐一:

For i = 0 to UBound(strLines) 
    strCsv.WriteLine strLines(i) 
Next 

編輯:
直接寫在沒有首先在存儲陣列中的線的文件,你打電話之前GetFileDetails打開文件,並寫入字符串的文件,而不是將其添加到陣列中:

... 
'Open the csv file 
Set strCsv = objFS.CreateTextFile(strDestFile, True) 

'Now get the file details 
GetFileDetails objFld, blnRecursiveSearch 

'Close and cleanup objects 
strCsv.Close 
... 

在你寫入文件

... 
For Each fil in fold.Files 
    strLine(0) = fil.Path 
    strLine(1) = fil.Size 
    strLine(2) = fil.DateCreated 
    strLine(3) = fil.DateLastModified 
    strLine(4) = fil.DateLastAccessed 

    strCsv.Write Join(strLine, ",") 
Next 
... 
+0

感謝您的快速回復子程序循環。我將修改我的腳本並再次運行它。我會讓你知道結果。歡呼 – MalsiaPro 2010-02-22 14:14:17

+0

我仍然遇到此問題錯誤「無效的過程調用或參數(第36行,字符5),您的解決方案增加字符串中的數據空間的大小,但有沒有辦法直接寫入輸出CSV文件而不是寫入陣列/串,然後生成CSV輸出。 – MalsiaPro 2010-02-23 12:31:45

+1

的感謝是的,而不是存儲在陣列中的線,你可以將它們直接寫入文件。見上面我的編輯。 – Guffa 2010-02-23 13:37:25