0

出于某种原因,以下代码对我不起作用...我正在尝试学习 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
4

2 回答 2

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
于 2013-02-14T21:38:29.633 回答
0

忽略您是否(或应该)允许在 C:\ 中创建文件夹的问题 - 没有类似的东西

objFSO.CreateFolder strDirectory

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

于 2013-02-14T19:31:29.633 回答