VBS无法创建文件夹

时间:2013-02-14 19:24:14

标签: vbscript

出于某种原因,以下代码对我不起作用......我正在尝试学习VBS,并且我一直在拼凑来自Google资源的一些代码。我无法弄清楚为什么我不能让它发挥作用。

Option Explicit
Dim objNetwork, objComputer
Dim objFSO, objFSOText, objFolder, objFile
Dim strDirectory, strFile, MakeObject

strDirectory = "C:\test"
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(strDirectory) Then
Wscript.Echo strDirectory & " already exists"
Else
Wscript.Echo "The folder " & strDirectory & " does not exist"
Wscript.Echo "The folder " & strDirectory & " has just been created"
End if

Wscript.Quit

2 个答案:

答案 0 :(得分:1)

我认为Ekkehard.Horner试图说的是:

如果您将以下内容添加到脚本中,它将起作用。新秀的错误,但好的工作,继续尝试它...它最终会有意义!

  

设置objFolder = objFSO.CreateFolder(strDirectory)

这是您的脚本,其中包含添加的代码以及添加代码的注释...

Option Explicit
Dim objNetwork, objComputer
Dim objFSO, objFSOText, objFolder, objFile
Dim strDirectory, strFile, MakeObject

strDirectory = "C:\test"
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(strDirectory) Then
Wscript.Echo strDirectory & " already exists"
Else
Wscript.Echo "The folder " & strDirectory & " does not exist"
'Below is the added line
Set objFolder = objFSO.CreateFolder(strDirectory)
Wscript.Echo "The folder " & strDirectory & " has just been created"
End if

Wscript.Quit

答案 1 :(得分:0)

忽略您是否(或应该)允许在C:\中创建文件夹的问题,而不是

objFSO.CreateFolder strDirectory

(也许在你的WScript.Echo分支中的那两个Else之间)什么都不会发生。