2011-11-03 102 views
1

我有一個VBS文件,我試圖用它來確定哪些文件夾和文件在某個目錄中。我相信我的代碼可以正確編寫,但每當我嘗試寫出文件或當前目錄時,我都會得到一個空白的文本文檔,只有根目錄已寫出。任何建議將不勝感激。創建一個能夠寫出文本文件的VBS文件

Dim NewFile 

Function GetFolders (strFolderPath) 
Dim objCurrentFolder, colSubfolders, objFolder, files 

Set objCurrentFolder = objFSO.GetFolder(strFolderPath) 
Set colSubfolders = objCurrentFolder.SubFolders 

For Each objFolder In colSubfolders 

NewFile.WriteLine(" - " & objFolder.Path) 

Set files = folder.Files 
For each folderIdx In files 
    NewFile.WriteLine("  - "& folderIdx.Name) 
Next 

Call GetFolders (objFolder.Path) 

Next 

End Function 

Dim fso, sFolder 

Set fso = CreateObject("Scripting.FileSystemObject") 
sFolder = Wscript.Arguments.Item(0) 
If sFolder = "" Then 
    Wscript.Echo "No Folder parameter was passed" 
    Wscript.Quit 
End If 
Set NewFile = fso.CreateTextFile(sFolder&"\FileList.txt", True) 

NewFile.WriteLine(sFolder) 

Call GetFolders(sFolder) 

NewFile.Close 

回答

2

您沒有足夠的所許注意你的變量命名。你的腳本是爲什麼所有的VB腳本應與行啓動的原因一個很好的例子: -

Option Explicit 

這將突出尚未宣佈這反過來會指出,在變量命名錯別字和不一致的所有變量。這裏是我將如何寫它: -

Option Explicit 

Dim msFolder : msFolder = Wscript.Arguments.Item(0) 

If msFolder = "" Then  
    Wscript.Echo "No Folder parameter was passed"  
    Wscript.Quit  
End If 

Dim mfso : Set mfso = CreateObject("Scripting.FileSystemObject") 
Dim moTextStream : Set moTextStream = mfso.CreateTextFile(msFolder & "\FileList.txt", True) 

moTextStream.WriteLine(msFolder) 

WriteFolders mfso.GetFolder(msFolder) 

moTextStream.Close 

Sub WriteFolders(oParentFolder) 

    Dim oFolder 
    For Each oFolder In oParentFolder.SubFolders 

     moTextStream.WriteLine(" - " & oFolder.Path) 

     Dim oFile 
     For Each oFile In oFolder.Files 
      moTextStream.WriteLine("  - " & oFile.Name) 
     Next 

     WriteFolders oFolder 

    Next 

End Sub 
+0

謝謝你。我嘗試了Option Explicit方法,但仍然允許我的腳本文件在不返回錯誤的情況下運行。我在Notepad ++中這樣做,所以如果有另一個程序可以讓Option Explicit函數返回錯誤,我很樂意嘗試。 – Seb