I have a VBScript that checks to see if MS Word is running hidden, makes it visible, then hides it again.
here is the script code that works fine when I double click the file in Explorer:
dim oWord
Dim wshShell, btn
Set wshShell = WScript.CreateObject("WScript.Shell")
set oWord = getobject(, "Word.Application")
if isobject(oWord) then
on error goto 0
wshShell.Popup "Word is running, making visible", 7, "ALPS Push print", &H0 + &H40
oWord.visible=true
wshShell.Popup "MS Word is now visible" & vbcrlf & vbcrlf & "Waiting 30 seconds then hiding it", 30, "ALPS Push print", &H0 + &H30
oWord.visible=false
else
wshShell.Popup "Word is not running" & vbcrlf & vbcrlf & "Quitting", 7, "ALPS Push print", &H0 + &H40
end if
It works find when I run it, but when it runs under Task Scheduler it fails so I created a batch file to launch it
wscript C:\dev\checkALPS.vbs
Now when I try to run it from the Task Scheduler, it still fails with the below error message
---------------------------
Windows Script Host
---------------------------
Script: C:\dev\checkALPS.bat
Line: 7
Char: 1
Error: ActiveX component can't create object: 'getobject'
Code: 800A01AD
Source: Microsoft VBScript runtime error
What can I do to get this working?
I have had this similar issue, I bypassed it by utilizing cscript.exe application to activate the vbscript as a console application rather than a windows application. There is a possibility that there is a limitation on the domain or computer which does not allow windows applications to be executed via wscript. As an alternative, try activating the same script via "Cscript.exe" instead.
So the code would be:
cscript C:\dev\checkALPS.vbs
And the get object method is not activated off of the wscript executable. So you would need to activate it via wscript.
dim oWord
Dim wshShell, btn
Set wshShell = WScript.CreateObject("WScript.Shell")
set oWord = Wscript.GetObject(, "Word.Application")
if isobject(oWord) then
on error goto 0
wshShell.Popup "Word is running, making visible", 7, "ALPS Push print", &H0 + &H40
oWord.visible=true
wshShell.Popup "MS Word is now visible" & vbcrlf & vbcrlf & "Waiting 30 seconds then hiding it", 30, "ALPS Push print", &H0 + &H30
oWord.visible=false
else
wshShell.Popup "Word is not running" & vbcrlf & vbcrlf & "Quitting", 7, "ALPS Push print", &H0 + &H40
end if
Give that a swing and let me know how it works.
Related
I have a large number of image files spanning nearly two decades where the subject is identified by the directory name and most of the photos themselves have a generic name however some of them have more specific names.
I am writing a script to prepend the directory name to the filename for all files in a specific directory. Well, I am trying to at least.
It has been a few years since I used VBScript and it seems I am VERY rusty.
I am facing challenges with the syntax format.
When I have Option Explicit (on line 6) it gives an error of Line 6, Char 1, Error: Expected Statement, Code: 800A0400 (In my shared code it would be line 7 because of the added Beginning of File line)
If I comment that out, I get an error on the import statements instead of Line 3, char 1, Error: Type mismatch: 'Imports', Code: 800A000D (In my shared code, it would be line 4 because of the added Beginning of File line)
I have spent a few hours googling for possible causes but to no avail and so I am turning to the community for help with getting the formatting of this script set correctly.
Any comments on a better script approach for accomplishing this task would be appreciated as well.
I am going to put in the entire code for the file because I do not know what part of it will be the relevant aspect.
In the code, it is currently set to only create a message box for each file as a test measure to ensure the variables have the values I think they have.
The commented out code for renaming the file is the truly intended purpose.
I am stuck on the proper formatting of the first part of the file however.
In general, I am executing this from the command line using: cscript.exe c:\MyTools\addDir2FileName.vbs
I launched it through windows explorer to get the more specific error codes with line numbers though.
I added the Beginning of File and End of File comments just for the purpose of clarity in this post.
' ####### Beginning of File
' Renames all files in a directory prepending the directory name to the file name
Imports System
Imports System.IO
Option Explicit
Dim WshShell, strOldFull, strNewFull, strFullPath, strCurDir
Dim strCurrentName, strNewName, strMessage, dir, fileObj, fs, fo
' Get the current directory
Set WshShell = CreateObject("WScript.Shell")
strFullPath = WshShell.CurrentDirectory
'Get folder properties to get just the name without path
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set fo=fs.GetFolder(strFullPath)
strCurDir = fo.Name
'Iterate through the directory
set dir = DirectoryInfo(strFullPath)
For Each fileObj In dir.GetFiles()
strCurrentName = fileObj.Name
strNewName = strCurDir & " - " & strCurrentName
' For testing purposes to make sure everything is as expected
' Creates a message box for each file instead of actually renaming it
strMessage = "Old Name: " & strCurrentName & chr(13) & chr(10) & "New Name: " & strNewName
MsgBox strMessage
' Renaming the file
' strOldFull = fs.BuildPath(CurrentDirectory, strCurrentName)
' strNewFull = fs.BuildPath(CurrentDirectory, strNewName)
' My.Computer.FileSystem.RenameFile(strOldFull, strNewFull)
Next
WshShell = Nothing
fo = Nothing
fs = Nothing
' ### End of File
The expectation is that a file "C:\Pictures\Trip to Nice\DCM001.jpg" will get renamed to "C:\Pictures\Trip to Nice\Trip to Nice - DCM001.jpg" and that all files in the directory that the script is run in will be similarly renamed.
Well, to be more precise, the output as currently formatted will produce a message box that says "Old Name: C:\Pictures\Trip to Nice\DCM001.jpg New Name: C:\Pictures\Trip to Nice\Trip to Nice - DCM001.jpg" and that a message box will be produced for all files in the directory. Yes, I will only run message box version in a test directory with 3 files. I would hate to get 50,000 message boxes, lol.
There is no output at current because of the formatting issues with either the Import Statement or the Option Explicit, or perhaps some other syntax piece I am missing or have wrong.
Thank you for your time and any help that anyone is able to provide. This is the first time I am posting to the community but I have long appreciated the answers provided. Usually, I can find my questions already answered, but I am stumped on this one...
Okay, through a lot of trial and error, I figured out a method to complete the task where I did not use System and so avoided the error that I was receiving before.
I thought to post the final script, in case anyone was interested.
' Renames all files in a directory prepending the directory name to the file name
Option Explicit
Dim WshShell, strOldFull, strFullPath, strCurDir, lastSlash
Dim strCurrentName, strNewName, strMessage, fileObj, fileSpec, fs, fo, ff
' Get the current directory
Set WshShell = CreateObject("WScript.Shell")
strFullPath = WshShell.CurrentDirectory
'Get folder object
Set fs = CreateObject("Scripting.FileSystemObject")
Set fo = fs.GetFolder(strFullPath)
set ff = fo.Files
'Get just the folder name
lastSlash = inStrRev(strFullPath, "\")
strCurDir = right(strFullPath, len(strFullPath) - lastSlash )
'Iterate through the directory
For Each fileObj in ff
strCurrentName = fileObj.Name
strNewName = strCurDir & " - " & strCurrentName
' For testing purposes to make sure everything is as expected
' Creates a message box for each file instead of actually renaming it
' strMessage = "Old Name: " & strCurrentName & chr(13) & chr(10) & "New Name: " & strNewName
' MsgBox strMessage
' Renaming the file
strOldFull = strFullPath & "\" & strCurrentName
set fileSpec = fs.GetFile(strOldFull)
fileSpec.Name = strNewName
Next
' Declare variables
Dim objFSO, objParentFolder, objFolder, objFile
' Set the parent directory to be processed
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objParentFolder = objFSO.GetFolder("C:\path\to\parent\directory")
' Function to process the subfolders
Sub ProcessSubFolders(folder)
' Loop through all files in the folder
For Each objFile In folder.Files
' Get the current file name
strFileName = objFile.Name
' Get the folder name
strFolderName = folder.Name
' Build the new file name
strNewFileName = strFolderName & "_" & strFileName
' Rename the file
objFile.Name = strNewFileName
Next
' Loop through all subfolders
For Each objFolder In folder.SubFolders
' Recursively call the function for the subfolder
ProcessSubFolders(objFolder)
Next
End Sub
' Call the function to process the top-level folder
ProcessSubFolders(objParentFolder)
' Clean up
Set objFile = Nothing
Set objParentFolder = Nothing
Set objFSO = Nothing
So I use task scheduler to run a specific file (c:\Newfile.exe) after a specific program has been started (c:\Program.exe).
I'd like to know how I can close "Newfile.exe" when "Program.exe" closes. I know this can't be done with task scheduler, does anyone know of a way to achieve this scenario outcome?
Kind regards
Tried using task scheduler
This program runs waiting for a program to exit, checks if it was Notepad.exe and if so terminates MyProgram.exe. Use an Exit Do after terminating your program to also quit the script.
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set objEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM Win32_ProcessStopTrace")
Do
Set objReceivedEvent = objEvents.NextEvent
If lcase(objReceivedEvent.ProcessName) = lcase("Notepad.exe") then
Msgbox "Process exited with exit code " & objReceivedEvent.ExitStatus
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process where ProcessName=MyProgram.exe")
For Each itm in ColItems
itm.Terminate
Next
End If
Loop
I have to create a vbscript with runas and hide the Dos windows.
The command in comment is working fine, but with the runas it's an another think.
this is my script :
' WshShell.Run "C:\Down\XP\Install_TV_Cmd_Line.bat", SW_HIDE,true
Set WshShell = WScript.CreateObject("WScript.Shell")
strcmd="""C:\Down\XP\Install_TV_Cmd_Line.bat"""
pass = "xxxxxx"
User = "xxxxxx\administrator"
Wshshell.run "runas.exe" & " /U:" & user & " " & strcmd
wscript.sleep(1000)
Wshshell.sendkeys pass & "{ENTER}"
How can i do for pass the SW_HIDE argument please ?
Thank you in advance
to run the command line in hide mode with vbscript
Wshshell.run "runas.exe" & " /U:" & user & " " & strcmd, 0, False
0 Hide the window (and activate another window.)
1 Activate and display the window. (restore size and position) when display a window for first time.
2 Activate & minimize.
3 Activate & maximize.
4 Restore. The active window remains active.
5 Activate & Restore.
6 Minimize & activate the next top-level window in the Z order.
7 Minimize. The active window remains active.
8 Display the window in its current state. The active window remains active.
9 Restore & Activate. Specify this flag when restoring a minimized window.
10 Sets the show-state based on the state of the program that started the application.
I've read a lot about automating WinSCP, but some of it I had trouble understanding because it presumed knowledge of other things, like .NET assembly, PowerShell, etc.
I'm wondering if, speaking strictly in VBScript and batch file file type of lingo, once I've downloaded the portable winscp.exe, how to simply open a remote site, give a user name and password, and download a list of the files in a particular directory. FTP protocol only.
There's an example for using the Session.ListDirectory from VBScript:
<job>
<reference object="WinSCP.Session"/>
<script language="VBScript">
Option Explicit
' Setup session options
Dim sessionOptions
Set sessionOptions = WScript.CreateObject("WinSCP.SessionOptions")
With sessionOptions
.Protocol = Protocol_Ftp
.HostName = "ftp.example.com"
.UserName = "user"
.Password = "mypassword"
End With
Dim session
Set session = WScript.CreateObject("WinSCP.Session")
' Connect
session.Open sessionOptions
Dim directoryInfo
Set directoryInfo = session.ListDirectory("/remote/path")
Dim fileInfo
For Each fileInfo In directoryInfo.Files
WScript.Echo fileInfo.Name & " with size " & fileInfo.Length & _
", permissions " & fileInfo.FilePermissions & _
" and last modification at " & fileInfo.LastWriteTime
Next
' Disconnect, clean up
session.Dispose
</script>
</job>
Other than that:
download the WinSCP .NET assembly package and extract it along with the script.
register the assembly for COM. Typically like:
%WINDIR%\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet32.tlb
%WINDIR%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet64.tlb
Run the script (list.wsf) like:
cscript list.wsf
You can of course also just run winscp.com scripting like:
Set shell = CreateObject("WScript.Shell")
Set exec = shell.Exec("winscp.com /command ""open ftp://username:password#ftp.example.com/"" ""ls /remote/path"" ""exit""")
WScript.Echo(exec.StdOut.ReadAll())
For more details on this approach see guide to advanced FTP scripting from VB script.
I have an MS Access (ACCDB File) that is corrupted. The error is the generic "The Microsoft Access Database Engine could not find the object 'Databases'. I have tried searching Google, tried using Stellar Phoenix software to recover the database, and also tried importing the database into a new one. None of which have worked to recover the Forms. The data is safe as it's stored in SharePoint.
Does anyone know of a way to export the Forms from this corrupted database to BAS Files? I cannot get this database to open at all and the most recent backup is 3 weeks ago (apparently my computer wasn't backing up to the remote server it should have been and what a way to discover that).
Any help is appreciated!
If you save the following script to a VBScript file, for example, Decompile.vbs, and drag and drop a copy of your corrupt database on to the script file, or just use the message box to enter the path for the copy, there is a possibility you can recover your forms. I am using Windows 7 64 bit, but I think the path to MS Access should work.
Option Explicit
Dim WSHShell
Dim fs, sPath, sPathTemp, sAccessPath, sKey
Set fs = CreateObject("Scripting.FileSystemObject")
If WScript.Arguments.Count > 0 Then
sPath = WScript.Arguments.Item(0)
Else
sPathTemp = Left(WScript.ScriptFullname, _
InStrRev(WScript.ScriptFullname, "\"))
sPath = InputBox("Enter Path and Name of .mdb or accdb", "Decompile", sPathTemp)
End If
If sPath = "" Or fs.FileExists(sPath) = False Then
MsgBox "Not a valid file: " & vbCrLf & sPath, 64, "Decompile" '64=vbInformation
Else
Set WSHShell = CreateObject("WScript.Shell")
sKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MSACCESS.EXE\"
sAccessPath = WSHShell.RegRead(sKey)
WSHShell.Run Chr(34) & sAccessPath & Chr(34) & " " & Chr(34) & sPath & Chr(34) & " /decompile"
End If