为什么不会objshell工作

时间:2013-04-01 00:28:44

标签: scripting vbscript

Set objFSO=CreateObject("Scripting.FileSystemObject") 
outFile="C:\Program Files\number2.vbs" 
Set objFile = objFSO.CreateTextFile(outFile,True) 
objFile.WriteLine "Set objWshShell = CreateObject(""WScript.Shell"")" 
objFile.WriteLine "objWshShell.RegWrite ""HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoFileMenu"", 1, ""REG_DWORD"" " 
objFile.WriteLine "Set  objWshShell = Nothing" 
objFile.Close

------------------Below part doesnt work in script-------------------------------

 ObjFile.Attributes = objFile.Attributes XOR 2

---------------------------Below part doesnt work in script-------------------

Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "C:\Program Files\number2.vbs" 
Set objShell = Nothing

当我执行objshell.run时,它说它无法找到指定的文件,并且对于objfile属性,它说它无法找到属性的方法?我运行没有运行部分的文件,它工作并创建了文件,所以最小的混淆为什么运行不会工作,如果它首先创建文件?

2 个答案:

答案 0 :(得分:1)

TextStreamFile是不同的对象。你也错过了报价。

Set objFSO = CreateObject("Scripting.FileSystemObject")
outFile    = "number2.vbs"

' if the file exist...
If objFSO.FileExists(outFile) Then
    ' if it hidden...
    If objFSO.GetFile(outFile).Attributes And 2 Then
        ' force delete with DeleteFile()
        objFSO.DeleteFile outFile, True
    End If
End If

' create TextStream object (that's not a File object)
Set objStream = objFSO.CreateTextFile(outFile, True)
WScript.Echo TypeName(objStream) '>>> TextStream
objStream.WriteLine "MsgBox ""Test"" "
objStream.Close

' get the File object
Set ObjFile = objFSO.GetFile(outFile)
WScript.Echo TypeName(ObjFile)   '>>> File
' now you can access that File properties
' and change it attributes too
ObjFile.Attributes = objFile.Attributes XOR 2

' surround your path with quotes (if needs)
outFile = objFSO.GetAbsolutePathName(outFile)
If InStr(outFile, " ") Then
    outFile = Chr(34) & outFile & Chr(34)
End If
WScript.Echo outFile

Set objShell = CreateObject("WScript.Shell")
objShell.Run outFile 'run the file

答案 1 :(得分:0)

ObjFile.Attributes = objFile.Attributes XOR 2

“对象不支持此属性或方法:'objFile.Attributes'”此行的错误应取其面值。 TextStream 类没有名为 Attributes 的属性或方法。因此,错误信息。

objShell.Run "C:\Program Files\number2.vbs"

在大多数工具和语言中,人们必须处理包含空格的参数。在这种特殊情况下,参数必须用引号括起来:

objShell.Run """C:\Program Files\number2.vbs"""

另外,您可以考虑阅读Microsoft关于使用WSH运行程序的介绍性白皮书:http://technet.microsoft.com/en-us/library/ee156605.aspx