How do I pass and return arguments from a VBScript WITHOUT using cscript.exe?
For example, I want to call script2 from script1 that returns a value to script1 without any involvement of cscript.exe.
I have searched various answers but they somehow involve the usage of cscript.exe.
This script gets installed voices and sets the one provided in the file voice.txt.
Set WshShell = CreateObject("WScript.Shell")
WShShell.CurrentDirectory = "..\Confirmatory Texts"
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists("voice.txt") Then
Set temp = FSO.OpenTextFile("voice.txt")
confirm_voice = temp.ReadLine()
temp.Close
Set Sapi = CreateObject("SAPI.SpVoice")
For Each Voice In Sapi.GetVoices
i = i + 1
Next
For loopvar = 0 To i-1
If loopvar = CInt(confirm_voice) Then
Set Sapi.Voice = Sapi.GetVoices.Item(loopvar)
End If
Next
Else
WScript.Echo "An Error Occured"
End If
If I call this script from another script, how can I make this script to return some value to the script that invoked it?
VBScript doesn't really provide call or import mechanisms for other VBScript files. The closest thing is to read the contents of the other file and run them via ExecuteGlobal.
Demonstration:
Put the following two files in the same directory and run script1.vbs. It will read the contents of script2.vbs and make the function Square available in the global scope by running the code via ExecuteGlobal. Once the function is available in the global scope you it can be used in the rest of the script.
script1.vbs:
Set fso = CreateObject("Scripting.FileSystemObject")
dir = fso.GetParentFolderName(WScript.ScriptFullName)
script = fso.BuildPath(dir, "script2.vbs")
ExecuteGlobal fso.OpenTextFile(script).ReadAll '"import" code into global scope
WScript.Echo Square(3)
script2.vbs:
Function Square(i)
Square = i*i
End Function
Related
I have on Outlook rule that kicks off a batch statement which starts a VBScript that then kicks off other VBScripts based on the sender and subject heading. If two emails from the same sender come into the in box simultaneously, it will start the first instance correctly. However, the second will kick off and return an error stating "Permission denied". I would like to run each email consecutively.
I have already tried the sleep functions and other time bound delays, but the query times are not consistent due to the size of the data.
Here is the basic script I have been using.
Set olApp = CreateObject("Outlook.Application")
Set olMAPI = olApp.GetNameSpace("MAPI")
Set oFolder = olMAPI.Folders("FieldFinanceAutomatedReports#xxxxx.com").Folders("Inbox").Folders("Requested Report People")
Set allEmails = oFolder.Items
Set firstemail = allEmails.GetLast
unreadCount = 0
For Each email In oFolder.Items
If email.Unread = True Then
If email.Sender= "Sender_of_email#email.com" Then
Set objcreate = CreateObject("Scripting.FileSystemObject")
Set objread = objcreate.OpenTextFile("C:\path_to_script\script.vbs")
request = objread.ReadAll()
objread.Close
Set objread = Nothing
Execute request
Set request = Nothing
Set objcreate = Nothing
End If
WScript.Sleep 500
If email.Sender = "Another_Sender_of_email#email.com" Then
Set objcreate = CreateObject("Scripting.FileSystemObject")
Set objread = objcreate.OpenTextFile("C:\path_to_script\another_script.vbs")
another_request = objread.ReadAll()
objread.Close
Set objread = Nothing
Execute another_request
Set another_request = Nothing
Set objcreate = Nothing
End If
WScript.Sleep 500
unreadCount = unreadCount + 1
End If
Next
I would like for each of the instances to wait until it the first process is complete.
What you need is called a semaphore or mutex. Essentially that's a resource that can be held by only one process or thread at a time. In VBScript you could implement that by attempting to create a (temporary) file. First process to do that wins, subsequent attempts will fail because the file already exists.
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
filename = sh.ExpandEnvironmentStrings("%TEMP%\mutex.txt")
'this will throw an error if the file is already opened
Set f = fso.OpenTextFile(filename, 2, True)
Note that you need to open the file for writing (second parameter set to 2). Opening it for reading (second parameter set to 1) does not suffice.
Run the operation in a loop and you can wait for another process to finish and release the mutex.
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
filename = sh.ExpandEnvironmentStrings("%TEMP%\mutex.txt")
On Error Resume Next
Do
Err.Clear
Set f = fso.OpenTextFile(filename, 2, True)
If Err Then WScript.Sleep 100
Loop While Err
On Error Goto 0
Make sure you remove the file when your script leaves the critical section (or terminates), otherwise other scripts might have trouble acquiring the mutex later. A convenient way of doing this is to implement the mutex as a class. That way it will automatically be cleaned up, even if the script should terminate unexpectedly.
Class Mutex
Private f_
Private Sub Class_Initialize
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
filename = sh.ExpandEnvironmentStrings("%TEMP%\mutex.txt")
On Error Resume Next
Do
Err.Clear
Set f_ = fso.OpenTextFile(filename, 2, True)
If Err Then WScript.Sleep 100
Loop While Err
On Error Goto 0
End Sub
Private Sub Class_Terminate
f_.Close
End Sub
End Class
You use that Mutex class in your code like this:
Set m = New Mutex 'acquire mutex
'...
'critical section goes here
'...
Set m = Nothing 'release mutex
I am using VBscript to scan folders, create zip files and add files to them (compress), but as I run my script on folders with a lot of files, I get the following error: "Compressed (zip) Cannot create output file"
my zip handling code is as follows:
Dim objFSO
Set objFSO= CreateObject("Scripting.FileSystemObject"
Function PreformZip(objFile,target,zip_name, number_of_file)
Set shell = CreateObject("WScript.Shell")
zip_target = target + "\" + zip_name +".zip"
If Not objFSO.FileExists(zip_target) Then
MakePathIfNotExist(target)
NewZip(zip_target)
Else
If number_of_file=0 Then
objFSO.DeleteFile(zip_target)
NewZip(zip_target)
End if
End If
Set zipApp = CreateObject("Shell.Application")
aSourceName = Split(objFile, "\")
sSourceName = (aSourceName(Ubound(aSourceName)))
zip_file_count = zipApp.NameSpace(zip_target).items.Count
zipApp.NameSpace(zip_target).Copyhere objFile, 16
On Error Resume Next
sLoop = 0
Do Until zip_file_count < zipApp.NameSpace(zip_target).Items.Count
Wscript.Sleep(100)
sLoop = sLoop + 1
Loop
On Error GoTo 0
End Function
Sub NewZip(zip)
Set new_zip = objFSO.CreateTextFile(zip)
new_zip.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
new_zip.Close
Set new_zip = Nothing
WScript.Sleep(5000)
End Sub
Function MakePathIfNotExist(DirPath)
Dim FSO, aDirectories, sCreateDirectory, iDirectory
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(DirPath) Then
Exit Function
End If
aDirectories = Split(DirPath, "\")
sCreateDirectory = aDirectories(0)
For iDirectory = 1 To UBound(aDirectories)
sCreateDirectory = sCreateDirectory & "\" & aDirectories(iDirectory)
If Not FSO.FolderExists(sCreateDirectory) Then
FSO.CreateFolder(sCreateDirectory)
End If
Next
End Function
Function Recursion(DirectoryPath)
Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(DirectoryPath) Then Exit Function
Call Recursion(FSO.GetParentFolderName(DirectoryPath))
FSO.CreateFolder(DirectoryPath)
End Function
I first thought I'm not waiting long enough after creating the zip, but I even tried it with 10 seconds wait after each zip and I still get the same error.
How can I solve it?
If there is no solution, is there an alternative way to make a zip? The script is not only for my own use so I don't want ro relay on a software which needs to be installed?
Although Folder.CopyHere method does not return a value and no notification is given to the calling program to indicate that the copy has completed, you could wait with next code snippet and I hope you can see proper (re)placement in your script:
On Error GoTo 0
zipApp.NameSpace(zip_target).Copyhere objFile _
, 4 +8 +16 +256 +512 +1024
Wscript.Sleep( 100)
On Error GoTo 0
Notice: no waiting Do..Loop, this Wscript.Sleep( 100) is sufficient to zip small files or start progress dialog box in case of huge files - and your script will wait for it...
Notice: no 'On Error Resume Next. Avoid invoking On Error Resume Next if you do not handle errors...
Flags used as follows.
Const FOF_SILENT = &h0004 'ineffective?
Const FOF_RENAMEONCOLLISION = &h0008 'ineffective?
Const FOF_NOCONFIRMATION = &h0010 '
Const FOF_SIMPLEPROGRESS = &h0100 'ineffective?
Const FOF_NOCONFIRMMKDIR = &h0200 '
Const FOF_NOERRORUI = &h0400 '
Unfortunately, in some cases, such as compressed (.zip) files, some option flags may be ignored by design (sic!) by MSDN!
If FOF_SILENT flag ineffective, then user could Cancel zipping process...
If FOF_RENAMEONCOLLISION flag ineffective, then newer file of the same name is not zipped, existing zip file keeps previous version without caution against; only existing folder brings on an extra error message...
Those could be fixed up as well, but it's subject of another question...
Well, after a great amount of research I found out that there is no possible way to fix this problem when using shell to perform zip.
I solved this issue by using za7.exe (7-zip) in the following way:
Dim zipParams
zipParams = "a -tzip"
Dim objShell: Set objShell = CreateObject("WScript.Shell")
command = zip_exe_location + " " + zipParams + " " + zip_target + " " + SourceFile
objShell.Run Command, 0 ,true
the "a" in the zip parameters means "add to file" and -tzip sets the type of the file as zip.
This question already has an answer here:
How to get a path with the variable user in VBscript
(1 answer)
Closed 4 years ago.
I have been trying to get the below script to work with the current user that's logged on:
On Error Resume Next
Set oFileSys = WScript.CreateObject("Scripting.FileSystemObject")
sRoot = "C:\users\MyUsername\downloads"
today = Date
nMaxFileAge = 30
DeleteFiles(sRoot)
Function DeleteFiles(ByVal sFolder)
Set oFolder = oFileSys.GetFolder(sFolder)
Set aFiles = oFolder.Files
Set aSubFolders = oFolder.SubFolders
For Each file in aFiles
dFileCreated = FormatDateTime(file.DateCreated, "2")
If DateDiff("d", dFileCreated, today) > nMaxFileAge Then
file.Delete(True)
End If
Next
For Each folder in aSubFolders
DeleteFiles(folder.Path)
Next
End Function
Now this works fine when I specifically define the "MyUsername" part of sRoot=, but I want this to be a variable. I have tried many suggestions in other posts but just can't seem to get this to work.
I'm going to deploy this to a few machines on startup that have different users so that's why it needs to be a variable as I don't know who will be logged into those computers at any given time.
From Help
Returns an environment variable's expanded value.
object.ExpandEnvironmentStrings(strString)
object
WshShell object.
str
String
String value indicating the name of the environment variable you want to expand.
The ExpandEnvironmentStrings method expands environment variables
defined in the PROCESS environment space only. Environment variable
names, which must be enclosed between "%" characters, are not
case-sensitive.
The following code expands the Windows Directory environment variable
and displays it:
Visual Basic Script
set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo "WinDir is " & WshShell.ExpandEnvironmentStrings("%WinDir%")
The preferred way is to
Returns the name of a user.
object.UserName
object
WshNetwork object.
*Returns a string.*
If you are using this property in a login script, see Creating an
Automated Login Script.
The following example demonstrates the use of the UserName property:
Set WshNetwork = WScript.CreateObject("WScript.Network")
WScript.Echo "Domain = " & WshNetwork.UserDomain
WScript.Echo "Computer Name = " & WshNetwork.ComputerName
WScript.Echo "User Name = " & WshNetwork.UserName
I have discovered the answer in the following article:
How to get a path with the variable user in VBscript
In short for anyone else needing this, here is the corrected code (for my example):
On Error Resume Next
Set oShell = CreateObject( "WScript.Shell" )
userprofile = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
Set oFileSys = WScript.CreateObject("Scripting.FileSystemObject")
sRoot = userprofile & "\downloads"
today = Date
nMaxFileAge = 2
Thanks.
I'm trying to write a VBScript that will check whether a file exists in a folder or not based on a partial number. If anything in the folder has this number in the string it can continue, if not an error needs to display saying it's not in the system. I've gotten a code that lets me know that the file DOES exist, but I can't get a NOT version to work. Any ideas?
Dim FSO, str1, fileName
str1 = "001234"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = FSO.GetFolder("C:\Users\GDoe\Desktop\FolderA\")
For Each objFile In objFolder.Files
fileName = objFile.Name
If InStr(fileName, str1) Then
MsgBox("Proceed")
Exit For
End If
Next
Unfortunately the FileSystemObject's FileExists method does not support wildcards, so the straightforward approach is not possible here.
The code you posted in your question is basically how one would check for the existence of a file with a partial name with VBScript and the FileSystemObject. You can modify that code into a check for the absence of a file with some minor changes. Define a variable before the loop and set it to False, then instead of displaying a message box set that vriable to True when you find a matching file:
fileFound = False
For Each objFile In objFolder.Files
fileName = objFile.Name
If InStr(fileName, str1) Then
fileFound = True
Exit For
End If
Next
If fileFound Then
MsgBox("Proceed")
Else
MsgBox("File doesn't exist.")
End If
Alternatively, you could shell out and check the exit code of the dir command:
Function FileExists(path, namepart)
Set sh = CreateObject("WScript.Shell")
rc = sh.Run("cmd /c dir ""*" & path & "\" & namepart & "*""", 0, True)
FileExists = Not CBool(rc)
End Function
dir returns 0 if it finds matching file(s) and 1 if it doesn't. CBool() converts the integer return code into a boolean value (0 → False, 1 → True). The negation then corrects the logic from "false if found" to "true if found".
Of course you could also name the function FileMissing and remove the negation, so that the function returns True if no matching file is found. That's just a matter of what logic works best in your code.
Note that you need to run the command with cmd /c, because dir is a cmd.exe builtin command, not an executable.
I actually just found a way to answer my own question but if there's a better way I'd also love to know.
Dim FSO, str1, fileName
str1 = "-001239"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = FSO.GetFolder("C:\Users\GDoe\Desktop\FolderA\")
For Each objFile In objFolder.Files
fileName=objFile.Name
If InStr(fileName, str1) Then
MsgBox("Proceed")
Exit For
End If
Next
If InStr(fileName, str1) = 0 Then
MsgBox("File doesn't Exist")
End If
I took the rule that if string2 is not found in an InStr command it returns 0. Setting the result = 0 shows if I don't have the file.
Is there a way to refer to the current file running in VBScript? I could just use the name of the file, but it needs to be operable despite directory changes and renames. The purpose of this is to use the file in a file I/O operation. If not possible, are there any potential alternatives, such as making a file non-re-namable, or non-movable?
WScript.ScriptFullName gives you the full path to your running script. You can use the FileSystemObject to parse this path further, if you'd like. For example:
' Assuming the script is at c:\scripts\test.vbs
strFile = WScript.ScriptFullName
With CreateObject("Scripting.FileSystemObject")
MsgBox .GetDriveName(strFile) ' => c:
MsgBox .GetParentFolderName(strFile) ' => c:\scripts
MsgBox .GetFileName(strFile) ' => test.vbs
MsgBox .GetBaseName(strFile) ' => test
MsgBox .GetExtensionName(strFile) ' => vbs
End With
You can use WScript.ScriptFullName to access the full path of the running script at run time.
You can also use Wscript.ScriptName if you just want the script part
This link explains in more detail
I could just use the name of the file, but it needs to be operable
despite directory changes and renames.
It is true that Wscript.ScriptName & WScript.ScriptFullName can provide detailed information on the script that is running, but if you want the code to be executed from an external I/O file that you intend on changing. You can utilize the ExecuteGlobal statement which will allow you to move Subs and Functions into the vbscript namespace.
myFunctions.vbs
Function GetDate()
GetDate = DateValue(Now)
End Function
ExecuteInNameSpace.vbs:
Dim fsObj : Set fsObj = CreateObject("Scripting.FileSystemObject")
Dim vbsFile : Set vbsFile = fsObj.OpenTextFile("myfunctions.vbs", 1, False)
Dim myFunctionsStr : myFunctionsStr = vbsFile.ReadAll
vbsFile.Close
Set vbsFile = Nothing
Set fsObj = Nothing
ExecuteGlobal myFunctionsStr
Wscript.echo "Todays Date is: " & GetDate
By moving all the processing of the code to an external file, you can flexibly configure and change the file all you want in your processing.