How can I extract the running scripts name using PCOMM VBScript - vbscript

First time posting so please forgive me if I miss something.
I'm trying to output the name of the running script (along with other variables) to a plain text file. I've been able to get everything else I need except for the Script Name. I've tried using WScript & Echo commands but they do not appear to function in my environment. These are PCOMM .mac files.
Thanks in advance
Here are some of things I've tried so far:
FilePath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")`
<<< The above works for Filepath but does not include the macro name
FileName = CreateObject("Scripting.FileSystemObject").GetFileName(".")
<<< The above just outputs the "."
Dim scriptName : scriptName = WScript.ScriptName
<<< The above gives error message: Variable is undefined: 'WScript'
Dim scriptName : WScript.Echo WScript.ScriptName
<<< The above gives error message: Variable is undefined: 'WScript'
Function GetName()
Dim WshShell, objEnv
Set WshShell = CreateObject("WScript.Shell")
Set objEnv = WshShell.Environment("Process")
GetName = objEnv("SCRIPTNAME")
End Function
<<< The above doesn't output anything. I used similar logic to successfully extract the HOMEPATH.

I managed to figure this one out after all:
REM Initialize the session
sHandle = autECLConnMgr.autECLConnList(1).Handle
REM Connect to the current session
sHandle = autECLSession.Handle
msgbox("Variable Check: " & ThisMacroName), vbOKOnly, "Validation check"

Related

VBScript script as a wrapper for a silent Batch file

I have a batch file that gets some parameters from the command line and returns some values into STDOUT.
I want the batch file to be "silent" (such that the console is not shown), and found out probably the only possibility of using vbs script.
I used this thread for implementing the argument forwarding to the batch file in VBS.
Then, I used the following command for calling the batch file I wrapped:
CreateObject("WScript.Shell").Run batchFilePath & " " & Trim(arglist), 0, False
It turns out that my batch file does run, but its STDOUT is discarded somewhere, and does not make its way back to whom called the VBS script. I.e. the batch file's STDOUT is not redirected into the VBS script's STDOUT.
How can I make the batch file's STDOUT being redirected to the VBS script STDOUT, such that if I start the VBS script from some shell, the batch file's output will be printed to the shell too?
Use Exec instead of Run, like this:
set objShell = CreateObject( "WScript.Shell" )
cmd = "echo Hello World!"
' Run the process
set objRes = objShell.Exec( "cmd /c """ & cmd & """" )
' Wait for the child process to finish
Do While objRes.Status = 0
WScript.Sleep 100
Loop
' Show whatever it printed to its standard output
Wscript.Echo "The output was:" & vbNewLine & objRes.StdOut.ReadAll()
Try this...
Intreturn = WshShell.Run("cmd /c " & path& " " & args & ">c:\batchoutput.txt", 0, true)
Set fso = CreateObject("Scripting.FileSystemObject")
Set objfile = fso.OpenTextFile("c:\batchoutput.txt", 1)
text = objfile.ReadAll
Objfile.Close
Or try this...
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objexc = WshShell.Exec("cmd /c " & command and args) 'replace command and args with proper variables
strOutputText = ""
While Not objexc.StdOut.AtEndOfStream
strOutputText = strOutputText & objexc.StdOut.ReadLine()
Loop
Msgbox strOutputText
You may need some debugging on this.

How can I redirect my vbscript output to a file using batch file?

I am new to Windows Scripting. I have a simple script for archiving using WinRAR CLI utility. I have to schedule this script using batch file. During archiving there are some errors and I want them to write in a simple text file or at least I can write entire output of archiving in a file. How can I change my code to do this?
Dim MyDate
Dim OutputFile
const WaitUntilFinished = true, DontWaitUntilFinished = false, ShowWindow = 1, DontShowWindow = 0
MyDate = Replace(Date, "/", "-")
OutputFile = "backup-" & mydate & ".rar"
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.CurrentDirectory = "C:\Users\ABC\Desktop\"
objShell.Run "C:\windows\Rar.exe a .\VBScripts\backups\" & OutputFile & " software", ShowWindow, WaitUntilFinished
objShell.Popup "Archiving Completed Successfully!",5, "Scheduled Backup"
Set objShell = Nothing
Batch file is like this;
#echo off
start /wait C:\Users\ABC\Desktop\VBScripts\scheduled_backup.vbs
Change your command line to include redirection to a log file:
logfile = "C:\path\to\your.log"
objShell.Run "%COMSPEC% /c C:\windows\Rar.exe a .\VBScripts\backups\" & _
OutputFile & " software >""" & logfile & """", ShowWindow, WaitUntilFinished
Use this function instead of WScript.Shell.Run:
' Runs an external program and pipes it's output to
' the StdOut and StdErr streams of the current script.
' Returns the exit code of the external program.
Function Run (ByVal cmd)
Dim sh: Set sh = CreateObject("WScript.Shell")
Dim wsx: Set wsx = Sh.Exec(cmd)
If wsx.ProcessID = 0 And wsx.Status = 1 Then
' (The Win98 version of VBScript does not detect WshShell.Exec errors)
Err.Raise vbObjectError,,"WshShell.Exec failed."
End If
Do
Dim Status: Status = wsx.Status
WScript.StdOut.Write wsx.StdOut.ReadAll()
WScript.StdErr.Write wsx.StdErr.ReadAll()
If Status <> 0 Then Exit Do
WScript.Sleep 10
Loop
Run = wsx.ExitCode
End Function
Call script instead of start in your batch and use redirection:
script //nologo C:\Users\ABC\Desktop\VBScripts\scheduled_backup.vbs 2> errors.txt

awk error while running script using VBA

I have a bash script which works ok when I run it normally (using cygwin window). The code below:
B=$2 #(ie. B="50602_15:56:31_15:56:11_15:56:21")
for line in ${B//;/ } ;
do
TENT=`echo ${line} | awk '{split($0,numbers,"_"); print numbers[1]}'`"_sim_fills.txt"
done
Now I am trying to run this script using vba in excel:
Public Sub test()
Dim cmd As String
Dim ret As Double
Dim WshShell As Object
Dim plink_object As Object
' Run Linux Commands
Set WshShell = CreateObject("WScript.Shell")
On Error Resume Next
Set plink_object = WshShell.Run("C:\cygwin\bin\mintty.exe fills_new.sh 66 ""50602_15:56:31_15:56:11_15:56:21;50602_15:57:07_15:56:47_15:56:57""", 1)
On Error GoTo 0
End Sub
I got an error: "awk: command not found".
Any ideas?
As #BroSlow suggested I used full path to awk and it works ok.
B=$2 #(ie. B="50602_15:56:31_15:56:11_15:56:21")
for line in ${B//;/ } ;
do
TENT=`echo ${line} | "/cygdrive/c/cygwin/bin/awk" '{split($0,numbers,"_"); print numbers[1]}'`"_sim_fills.txt"
done

Extract all .gz file in folder using VBA Shell command

I have the following VBA code to extract all the files within a given directory.
Sub extractAllFiles()
Dim MyObj As Object, MySource As Object, file As Variant
Dim shellStr As String
file = Dir("C:\Downloads\")
While (file <> "")
If InStr(file, ".gz") > 0 Then
shellStr = "winzip32 -e C:\Downloads\" & file & " C:\Downloads\"
Call Shell(shellStr, vbHide)
End If
file = Dir
Wend
End Sub
When I execute this sub routine I get a Run-Time error 53, "File Not Found" error. When I copy the shellStr... Example: winzip32 -e C:\Downloads\file1.gz C:\Downloads\ and execute it from command prompt, it works perfectly! I get the text file within file1.gz extracted to the downloads directory. However running it from VBA doesn't seem to work.
Can anyone shed some light?
You should try with the full path of the shell command, like this, that worked for me:
Sub extractAllFiles()
Dim MyObj As Object, MySource As Object, file As Variant
Dim shellStr As String
file = Dir("C:\Downloads\")
While (file <> "")
If InStr(1, file, ".gz") > 0 Then
shellStr = "C:\Program Files (x86)\WinZip\winzip32 -e C:\Downloads\" & file & " C:\Downloads\"
Call Shell(shellStr, vbHide)
End If
file = Dir
Wend
End Sub
My winzip is installed as C:\Program Files (x86)\WinZip\winzip32. You should use yours.
Your install path may be:
C:\Program Files\WinZip\winzip32
WinZip path needs to be absolute path:
C:\Program Files\WinZip\winzip32'
Check your WinZip path. That needs to be fine fixed to the full path to your WinZip.
Using the logic from this SO post, try the below code:
Sub ExtractAllFiles()
Dim FileName As String
Dim ShellStr
FileName = Dir("C:\Downloads\*.gz")
Do While Len(FileName) > 0
ShellStr = "winzip32 -e " & FileName & " C:\Downloads"
Call Shell(ShellStr, vbHide)
FileName = Dir
Loop
End Sub
Let us know if this helps.

VB script + automate CLI command by VB script

I write the following VB script in order to run the CLI command - vpnclient.exe
my target is to automate the vpnclcient process and answer “y” when question appears,
I have WIN XP PC
During running the vpnclient.exe in CMD window we get then following question
Do you wish to continue? (y/n):
In my VB I write the “echo y” in order to answer on this question automatically
but question is still stuck in CMD window ,and I cant continue
please advice what chuld be wrong in my code and how to fix it?
MY VB script (vpnclient.exe – exist under VPN directory)
Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd /K CD C:\Program Files\Cisco\VPN & ( echo y | vpnclient.exe connect ""site moon"" )"
Set oShell = Nothing
You can try by creating a file with the commands to be executed on the command line instead of echoing the password.
Here's an example where a text file is created first with the required command and then those commands are invoked from the file.
Public Function FTPDownload(serverName, ftpuser, ftppassword, dirPath, localpath, fileName)
Dim fso, myfile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create FTP.EXE commands file
Set fso = CreateObject("Scripting.FileSystemObject")
Set myfile= fso.OpenTextFile("C:\Regression\Results\ftp_cmd.ini", 2, True)
myfile.WriteLine("open " &serverName )
myfile.WriteLine(ftpuser)
myfile.WriteLine(ftppassword)
myfile.WriteLine("lcd " & localpath )
myfile.WriteLine("cd " & dirPath)
myfile.WriteLine("prompt")
myfile.WriteLine("cr")
myfile.WriteLine("mget *" &fileName &"*" )
myfile.WriteLine("mdelete *" &fileName &"*" )
myfile.WriteLine("close")
myfile.WriteLine("quit")
myfile.Close
'====================The following code executes the FTP script. It creates a Shell object and run FTP program on top of it.===================
Set objShell = CreateObject( "WScript.Shell" )
objShell.Run ("ftp -i -s:" & chr(34) & "C:\Regression\Results\ftp_cmd.ini" & chr(34))
Set objShell = Nothing
End Function

Resources