报错:object required :‘sourceFolder
时间: 2024-04-16 20:26:52 浏览: 120
非常抱歉,我之前的回答中的代码还是有误。以下是修正后的代码:
```vbscript
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("input.txt", True)
strText = "这是要保存的字符串内容"
objFile.Write strText
objFile.Close
zipPath = "compressed.zip"
Set objShell = CreateObject("Shell.Application")
Set sourceFolder = objShell.NameSpace(objFSO.GetParentFolderName(zipPath))
Set compressedFile = sourceFolder.ParseName(objFSO.GetFileName(zipPath))
objShell.NameSpace(zipPath).CopyHere compressedFile
Set objStream = CreateObject("ADODB.Stream")
objStream.Type = 1 ' Binary
objStream.Open
objStream.LoadFromFile zipPath
base64String = objStream.Read( -1 )
objStream.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile zipPath
Set scriptControl = CreateObject("MSScriptControl.ScriptControl")
scriptControl.Language = "JScript"
base64String = scriptControl.Eval("(function(){var base64EncodeChars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';var out='',i=0,len=base64String.length;while(i<len){var c1,c2,c3,enc1,enc2,enc3,enc4;c1=base64String.charCodeAt(i++);c2=base64String.charCodeAt(i++);c3=base64String.charCodeAt(i++);enc1=c1>>2;enc2=((c1&3)<<4)|(c2>>4);enc3=((c2&15)<<2)|(c3>>6);enc4=c3&63;if(isNaN(c2)){enc3=enc4=64;}else if(isNaN(c3)){enc4=64;}out=out+base64EncodeChars.charAt(enc1)+base64EncodeChars.charAt(enc2)+base64EncodeChars.charAt(enc3)+base64EncodeChars.charAt(enc4);}return out;})()")
WScript.Echo base64String
```
这段修正后的代码通过`ParseName`方法获取ZIP文件对象,并将其添加到ZIP文件中。然后,使用`ADODB.Stream`对象加载ZIP文件内容,并将其转换为Base64字符串。最后,通过`ScriptControl`对象执行JScript代码进行Base64编码,并输出Base64字符串。
请确保在运行VBScript之前已经安装了相应的运行环境,如Windows操作系统。
非常抱歉之前的错误带来的困扰,希望这次能够解决问题!
阅读全文