2013-04-01 107 views
-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屬性它說它無法找到屬性的方法?我運行的文件沒有運行部分,它的工作原理和創建文件,所以最後混淆了爲什麼運行無法工作,如果它首先創建文件?爲什麼不會objshell工作

回答

0
ObjFile.Attributes = objFile.Attributes XOR 2 

「對象不支持此屬性或方法:‘objFile.Attributes’」在這行誤差應在其信以爲真。 TextStream類沒有屬性或方法名爲屬性。因此,錯誤消息。

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

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