VBScript command to wait for files to be extracted before launching EXE to install program - vbscript

I'm looking at having a script that decompresses a file (PDMsetup.zip) and then launch the executable that it extracts.
ZipFile="PDMsetup.zip"
ExtractTo=".\"
Set fso = CreateObject("Scripting.FileSystemObject")
sourceFile = fso.GetAbsolutePathName(ZipFile)
destFolder = fso.GetAbsolutePathName(ExtractTo)
Set objShell = CreateObject("Shell.Application")
Set FilesInZip=objShell.NameSpace(sourceFile).Items()
objShell.NameSpace(destFolder).copyHere FilesInZip, 16
Set fso = Nothing
Set objShell = Nothing
Set FilesInZip = Nothing
wscript.sleep 480000
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
strPath = strFolder & "\Startwinstall.exe"
objShell.Run strPath
I want to get rid of;
wscript.sleep 480000
and replace it with a command that tells the script wait until the extraction is done before launching startwinstall.exe
I've kept adjusting the wait time to make up for differences in PC performance with the extraction, but a command to just 'wait' until it's done would be preferential.

Delete any previous copy of the installer exe in the target folder and then wait for that file to be created. Create your objects once at the top of the script. And there's no need to set the objects to Nothing. That will happen automatically when the script ends. The edited script is below:
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWSH = CreateObject("Wscript.Shell")
Set oApp = CreateObject("Shell.Application")
MyFolder = oFSO.GetParentFolderName(WScript.ScriptFullName)
ExtractTo = ".\"
ZipFile = "PDMsetup.zip"
StartApp = ExtractTo & "Startwinstall.exe"
On Error Resume Next
oFSO.DeleteFile StartApp
On Error Goto 0
sourceFile = oFSO.GetAbsolutePathName(ZipFile)
destFolder = oFSO.GetAbsolutePathName(ExtractTo)
Set FilesInZip = oApp.NameSpace(sourceFile).Items()
oApp.NameSpace(destFolder).copyHere FilesInZip, 16
Do Until oFSO.FileExists(StartApp)
WScript.Sleep 1000
Loop
oWSH.Run StartApp
Note: I assigned a MyFolder variable, but it's not currently being used. ExtractTo = ".\" could be changed to ExtractTo = MyFolder. You could also eliminate the GetAbsolutePathName lines if you are using MyFolder with the ZipFile name. There are always many ways to do the same thing.
Note: I think the above can be done with a much briefer (probably two line) PowerShell script. Let me know if you're interested in that solution.

Related

Create a text file in %temp% and write content in it using vbs

Basically, I want to create a new file and write in it in a directory on the PC, pointed to by the %TEMP% variable. However, the revised code below does not work:
Dim oFile
Dim shell
Set oShell = CreateObject("WScript.Shell")
user = oShell.ExpandEnvironmentStrings("%Temp%")
Set oFile = CreateObject("Wscript.Shell")
Set oFile = oFile.CreateTextFile("%Temp%\d.txt")
oFile.WriteLine "here is my contant"
oFile.Close
Error Message:
run time error
line no: 3
object required
Old Code
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
FileName = "%TEMP%\myfile.txt"
Set tf = fso.CreateTextFile(FileName, True)
If I use the file name "C:\myfile.txt" it works fine.
Error Message:
Path not found
In VBA, you can just use Environ("TEMP") to expand the Environment variable - if this does not work in VBScript, you may need to bind the WScript.Shell object and use the ExpandEnvironmentStrings property instead, like so:
Set oShell = CreateObject("WScript.Shell")
FileName = oShell.ExpandEnvironmentStrings("%TEMP%") & "\myfile.txt"
Set oShell = Nothing
Following from comments below
Here is a "fully fixed" code:
'Declare variables/objects first
Dim fso AS Object, oFile AS Object
Dim oShell AS Object, FileName AS String
'This bit turns "%TEMP%" into a real file path
Set oShell = CreateObject("WScript.Shell")
FileName = oShell.ExpandEnvironmentStrings("%Temp%\d.txt")
Set oShell = Nothing 'Tidy up the Objects we no longer need
'This bit creates the file
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.CreateTextFile(FileName)
oFile.WriteLine "here is my content"
oFile.Close
Set oFile = Nothing 'Tidy up the Objects we no longer need
Set fso = Nothing 'Tidy up the Objects we no longer need
you can use Environ("temp") to write to C:\Users\[username]\AppData\Local\Temp

How to Copy a file that was read from a list

Hello guys I have an issue or issues with my code above
I'm trying to get "sExtension" to be search in a different folder other that the one I'm using to save my script since this script will be use as a Startup Script on many computers
(It works only if I run the script in the same folder "sExtension", "ExtAssign.txt" and sComputername are otherwise it wont find the path)
This is what it should do
Read a file called "ExtAssign.txt" (There is a full list of computer names in that file) and if it find the computer name on that file then it should copy a file with the with the extension number assigned to that computer name from a file server to "C:\" Drive
For this example I'm trying to do this locally, If I can make it then I'll try it from my File Server
Set objFSO = CreateObject("Scripting.FileSystemObject")
set oFso = CreateObject("Scripting.FileSystemObject")
Set objFS = CreateObject("Scripting.FileSystemObject")
Set fso = CreateObject("Scripting.FileSystemObject")
set oShell = WScript.CreateObject("WScript.Shell")
set oShellEnv = oShell.Environment("Process")
Set folder = Fso.GetFolder("C:\Users\XXXXX\Desktop\Test\Extensions\")
Set wshshell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set ObjEnv = WshShell.Environment("Process")
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
Scomputername = ObjEnv("COMPUTERNAME")
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objWShell = wScript.createObject("WScript.Shell")
Dim strFile
'File to scan
strFile = "C:\Users\XXXXX\Desktop\Test\Extensions\Extassign\ExtAssign.txt"
Dim strPattern
'Look for computer name in file
strPattern = scomputername
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
Dim strLine
'Read each line and store it in strLine
strLine = objFile.ReadLine
'If the line matches the computer name, save the line to ExtArray
If InStr(strLine,strPattern)>0 Then
Dim ExtArray
'Split the line and separate the extension
ExtArray = Split(strLine,"|", -1, 1)
Dim sExtension
'Save the extension to sExtension
sExtension=ExtArray(1)
End If
Loop
'If the sExtension is empty, computer was not found, send message and terminate script.
If sExtension="" Then
WScript.Echo "ERROR: Computer "& scomputername &" not found in Extension Assignment List, so no extension has been set. Avaya will not be launched. Please contact your IT department for assistance."
Else
'If the sExtension contains a number, Copy that file to C:\ and rename it to Config.xml
fso.CopyFile "C:\Users\XXXXX\Desktop\Test\Extensions\ "& sExtension &"", "C:\Config.xml", True
End If
at the end it if it finds the file sExtension it will rename it to Config.xml but it wont do it unless I run the script in the same folder sExtension and sComputername.
I get File not found error
Thank you in advance and Happy new year!
The culprit is most likely this line:
fso.CopyFile "C:\Users\XXXXX\Desktop\Test\Extensions\ "& sExtension &"", "C:\Config.xml", True
There is a trailing space after the last backslash in the path, so you're creating a path
C:\Users\XXXXX\Desktop\Test\Extensions\ 12345
^
when you actually want a path
C:\Users\XXXXX\Desktop\Test\Extensions\12345
On a more general note: why are you creating 7(!) FileSystemObject instances (replacing one of them three times on top of that)? And 3(!) WScript.Shell instances? You don't even use most of them, not to mention that you don't need the Shell object in the first place. You only use it for determining the computer name, which could be done just fine using the WScript.Network object (that you don't use at all).
Also, please don't ever use comments like this:
'Read each line and store it in strLine
strLine = objFile.ReadLine
It's quite obvious that you read each line and assign it to the variable strLine. Comments shouldn't rephrase what you're doing (the code already does that, at least when you're using speaking variable and function names), but why you're doing it, i.e. what the purpose of a particular code section is.
Your code could be reduced to something as simple as this:
Set fso = CreateObject("Scripting.FileSystemObject")
Set net = CreateObject("WScript.Network")
computername = net.ComputerName
foldername = "C:\Users\XXXXX\Desktop\Test\Extensions"
filename = fso.BuildPath(foldername, "Extassign\ExtAssign.txt")
Set f = fso.OpenTextFile(filename)
Do Until f.AtEndOfStream
line = f.ReadLine
If InStr(line, computername) > 0 Then
arr = Split(line, "|", -1, 1)
If UBound(arr) >= 1 Then extension = arr(1)
End If
Loop
f.Close
If IsEmpty(extension) Then
WScript.Echo "ERROR: Computer "& computername &" not found in ..."
Else
fso.CopyFile fso.BuildPath(foldername, extension), "C:\Config.xml", True
End If

how to delete multiple folders,desktop and start menu shortcut using vbscript

I never did any vbscript before, so i don't know if my question is very easy one. Following is the flow of steps that has to be done :
Check if exist and delete a folder at c:\test1 if found and continue. If not found continue.
Check if exist and delete a folder at c:\programfiles\test2 if found and continue. If not found continue.
Check if a desktop shortcut and start menu shortcut exist and delete if found. If not exit.
I could delete 2 folders with the following code:
strPath1 = "C:\test1"
strPath1 = "C:\test1"
DeleteFolder strPath1
DeleteFolder strPath1
Function DeleteFolder(strFolderPath1)
Dim objFSO, objFolder
Set objFSO = CreateObject ("Scripting.FileSystemObject")
If objFSO.FolderExists(strFolderPath) Then
objFSO.DeleteFolder strFolderPath, True
End If
Set objFSO = Nothing
But i need to run one script to delete 2 folders in different paths, 2 shortcuts one in start menu and one on desktop.
I was experimenting with this code to delete the shortcut on my desktop:
Dim WSHShell, DesktopPath
Set WSHShell = WScript.CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")
on error resume next
Icon = DesktopPath & "\sample.txt"
Set fs = CreateObject("Scripting.FileSystemObject")
Set A = fs.GetFile(Icon)
A.Delete
WScript.Quit
It works fine for txt file on desktop, but how do i delete a shortcut for an application from desktop as well as start menu.
strPath1 = "C:\test1"
strPath2 = "C:\test2"
DeleteFolder strPath1
DeleteFolder strPath2
DeleteShortcut
'-------------------------------------------------------
Sub DeleteFolder(strFolderPath)
Set fso = CreateObject ("Scripting.FileSystemObject")
If fso.FolderExists(strFolderPath) Then
fso.DeleteFolder strFolderPath, True
End If
End Sub
'-------------------------------------------------------
Sub DeleteShortcut()
Set WSHShell = WScript.CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")
shortcutPath = DesktopPath & "\MyShortcut.lnk"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(shortcutPath) Then
Set myFile = fso.GetFile(shortcutPath)
myFile.Delete
End If
End Sub

How to make VBScript copy permissions along with files

This is all in Windows XP utilizing VBScript.
I have a directory with several files inside. The files have varying permissions set. I need to be able to copy the files to a new directory while retaining the permissions. Using the script below the copy works fine but the permissions are overwritten by the new parent folder.
I am aware of xcopy but I am unsure how to make it work within the script. Using robocopy is a slight possibility but should be avoided if at all possible. Other utilities are out of the question due to network contraints.
Any help is greatly appreciated.
Dim CopyFromPath, CopyToPath
Const WINDOW_HANDLE = 0
Const NO_OPTIONS = 0
Const OverwriteExisting = TRUE
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE,"Select folder to copy:",NO_OPTIONS,ssfDRIVES)
if (not objFolder is nothing) then
Set objFolderItem = objFolder.Self
CopyFromPath = objFolderItem.Path
else
Set objShell = nothing
WScript.Quit(0)
end if
Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE, "Where should the folder be copied to?:", NO_OPTIONS, ssfDRIVES)
if (not objFolder is nothing) then
Set objFolderItem = objFolder.Self
CopyToPath = objFolderItem.Path
else
Set objShell = nothing
WScript.Quit(0)
end if
Set objFolder = nothing
Set objFolderItem = nothing
Set objShell = nothing
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile CopyFromPath & "\*.*", CopyToPath & "\", OverwriteExisting
msgbox "The folder has now been copied to " & CopyToPath
xcopy is a good idea for it.
An example to how make it work within vbscript.
Set oWSHShell = CreateObject("WScript.Shell")
oWSHShell.Exec "xcopy C:\source C:\destination /O /X /H /K /Y"
Set oWSHShell = Nothing

VBScript current directory + sub directory?

I am trying to get the path of a file that is within a sub-directory of the current directory in VBScript. The following does not seem to work?
currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
FileToCopy = currentDirectory & "\test\user.js"
Here is the entire code:
Set oFSO = CreateObject("Scripting.FileSystemObject")
strFolder = oFSO.GetParentFolderName(WScript.ScriptFullName)
FileToCopy = oFSO.BuildPath(strFolder, "unproxy\user.js")
''# get AppdataPath
Set WshShell = CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("PROCESS")
AppdataPath = WshSysEnv("APPDATA")
FoxProfilePath = AppdataPath & "\Mozilla\Firefox\Profiles\"
'"# is firefox and user.js present?
if oFSO.FolderExists(FoxProfilePath) AND oFSO.FileExists(FileToCopy) Then
''# copy user.js in all profilefolders to get around those random profile names =)
For Each ProfileFolder In oFSO.GetFolder(FoxProfilePath).Subfolders
oFSO.GetFile(FileToCopy).Copy ProfileFolder & "\" & FileToCopy, True
Next
End If
'"# clean up
Set oFSO = Nothing
Set WshShell = Nothing
Set WshSysEnv = Nothing
I recommend using FileSystemObject when dealing with file paths:
Set oFSO = CreateObject("Scripting.FileSystemObject")
strFolder = oFSO.GetParentFolderName(WScript.ScriptFullName)
FileToCopy = oFSO.BuildPath(strFolder, "test\user.js")
Edit: The problem is in this line of your script:
oFSO.GetFile(FileToCopy).Copy ProfileFolder & "\" & FileToCopy, True
Since FileToCopy contains a full file name, when you concatenate it with ProfileFolder you get an invalid file name, like this:
C:\Documents and Settings\username\Application Data\Mozilla\Firefox\Profiles\mlreq6kv.default\D:\unproxy\user.js
Change this line to the one below, and your script should work fine. (Note: the trailing path separator at the end of ProfileFolder is required to indicate that the profile folder, e.g. mlreq6kv.default, is indeed a folder and not a file.)
oFSO.GetFile(FileToCopy).Copy ProfileFolder & "\", True
You can get the current directory with :
Set WSHShell = WScript.CreateObject("WScript.Shell")
WScript.Echo WshShell.CurrentDirectory

Resources