-1
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 属性,它说它找不到属性的方法?我在没有运行部分的情况下运行了文件,它工作并创建了文件,所以我很困惑为什么如果它首先创建文件运行将无法工作?

4

2 回答 2

1

TextStream and File are different objects. You also missed quotes.

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
于 2013-04-01T12:24:56.273 回答
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

于 2013-04-01T08:32:14.563 回答