we run WinSQL with DB2's begin and end statements. We have multiple scripts in one file. I was wondering if there was a way to hold down shift with SendKeys to use the right arrow/down arrows to highlight the text I want to run. Does it need a loop? And how do I do a 5 second delay before it starts so I can switch the windows when I run it? I tried:
set /P "=Wait and send a command: " < NUL
ping -n 1 -w 1 127.0.0.1 > NUL
%SendKeys% "^{RIGHT}"
for the highlighting, multiple times, but it forgets about the shift key and just moves right multiple times.
Thanks
Try this for your sendkeys.
Example
%SendKeys% "+{right}"
^ is for CTRL key rather than shift.
Then for delay try cmd command timeout /t 5
On another note since you're using vbs also, you can use that to switch your windows as well.
Dim ObjShell :Set ObjShell = CreateObject("Wscript.Shell")
ObjShell.AppActivate("Notepad")
Related
I made a simplified version of my code that directly highlights the issue.
I have read dozens of similar issues/solution.
Part of my workflow in VBA in Microsoft Access involves calling an R script that does some logic and returns information to a table in the same database.
It was working until we moved the location of the R installation to a new drive. Changing the path to this new install location does not work. No other code is changed.
cmd = "C:\R\bin\i386\Rscript.exe C:\R\test.R"
Debug.Print cmd
Shell cmd
I get
runtime error '5'
I am using the immediate window to check the paths are correct and copying them into RUN to verify that they do work.
The above outputs:
C:\R\bin\i386\Rscript.exe C:\R\test.R
It works in RUN.
The first thing I found when searching online is to add more (") as shell can handle them weirdly:
cmd = """C:\R\bin\i386\Rscript.exe""" & " " & """C:\R\test.R"""
Or any iterations of using "s in different places, output:
"C:\R\bin\i386\Rscript.exe" "C:\R\test.R"
Same error but works in RUN. I also tried them all successfully in CMD.
It seems just Shell refuses to launch R from that path. I have moved it elsewhere on my C drive with same effect.
I cannot recreate the original R installation path as that shared drive is now completely dead.
EDIT:
I changed to using ShellExecute simply to try and make Notepad ++ open, again works in cmd.
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "C:\N\notepad++.exe", "C:\R\test_in.csv", "", "open", 1
This time I hit a "suspicious macro error" that leads me to believe that it may be an antivirus setting (macros are enabled in Access) blocking Shell from calling anything.
After days of testing I have found the solution, hopefully this can help anyone else in a similar situation. Windows Defender only blocks shell calls to non-Microsoft products, so I nested a call to PowerShell within the call to Shell:
Shell ("powershell.exe C:\R\bin\i386\Rscript.exe C:\R\test.R")
Take note you need to play around with the "s a lot ot get it working, my actual pipeline has more arguments and I had to enclose them in 5 sets of "s for it to pass through to powershell properly. IE:
Dim codePath As String: codePath = """""\\example\example"""""
Try these variations using Start or a second Command:
cmd = "Start C:\R\bin\i386\Rscript.exe C:\R\test.R"
or:
cmd = "cmd /c ""C:\R\bin\i386\Rscript.exe C:\R\test.R"""
I've got a tricky thing to do here.
I'm using task scheduler to auto-reboot all the computer during the weekend by calling a simple .bat file containing a shutdown command.
I wanted to give the user the possiblilty to cancel that reboot by displaying them a popup saying "Computer is about to restart, do you wish to continue ? Ok or Cancel". I have done that with VB using the popupbox method, it works perfectly.
Here is the problem i'm facing: The task is running under the System account and unless the task is set to run with the current logged on user, the popup box won't appear. I could change the account set in the task but I've got hundreds of users so impossible.
I've done some kind of a workaround, calling the VBS from Psexec... Works, but it's not perfect.
Here is my Psexec command that calls the VB:
Psexec -accepteula -s -i cmd /c C:\Windows\System32\Weekly_Reboot.vbs
Here is my VB:
Dim WshShell, BtnCode
Set WshShell = WScript.CreateObject("WScript.Shell")
BtnCode = WshShell.Popup("Computer is about to restart, do you wish to continue?", 30, "/!\ Weekly Restart /!\", 4 + 32)
Select Case BtnCode
case 6
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "c:\Windows\System32\Weekly_Reboot.bat"
case 7
WScript.Echo "No prob - the computer won't restart"
case -1
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "c:\Windows\System32\Weekly_Reboot.bat"
End Select
And here is the actual reboot command:
Shutdown /f /r /c "This is the weekly reboot"
Any idea would be really awesome ! I really tried googling it, but no luck.
Here is the problem i'm facing: The task is running under the System account and unless the task is set to run with the current logged on user, the popup box won't appear. I could change the account set in the task but I've got hundreds of users so impossible.
Change the task to run under the group called Users. It will make the task run for any logged in user.
I'm not certain if this is needed: but you might want to run it using the highest privileges.
So I am trying to get my application to delete itself on exit.
I am currently using this (found here):
Process.Start("cmd.exe",
"/C choice /C Y /N /D Y /T 3 & Del " + Application.ExecutablePath);
Application.Exit();
The CMD window comes up, but will not remove the application. Any ideas why not?
EDIT:
So it works sometimes... other times it does not... ideas?
I think the problem is that you cannot delete an application while it's running.
You could introduce some delay in the cmd command(I don't know how), or you could simply make another application with a timer set on 5 sec. And insert the code:
System.IO.File.Delete(Application.ExecutablePath)
Me.Close
Then just use the Process.Start("your/Delete/App/Path.exe") in the first app.
A two-part question but they tie in together. - I'm experimenting with VB.net console applications and I'm wandering how I send command line commands to the console window. Take the below Sub.
Sub initialLoad()
Console.WriteLine("Started # " + %date% %time%)
Console.WriteLine("Test")
End Sub
I'm trying to add the equivalent of echo. which would add a line break, I tried using the string Console.WriteLine("Test", vbCrLf) but the vbCrLF made no difference and did not add a line break after the text.
The %date% and %time% variables when used on a Batch script for example will give the current date and time and is recognised and defined automatically within the CMD window. Am I able to send these CMD commands to the console or must I use VB.net equivalents to achieve this.
I found that instead of pause>nul I had to use Console.ReadLine() to make the console window pause.
Am I incorrect in assuming I can use CMD commands? I know in C++ I can send all the commands but is there something similar in VB.net? Thanks.
For environment variables use Environment.GetEnvironmentVariable
For new line use Environment.NewLine
For commands see here: How To: Execute command line in C#, get STD OUT results - this is in C# but you can use the same objects in VB.NET.
My VBA macro has writtin this cmd file
cd/d H:\QUB\2dcfd
"C:\ANSYS Inc\v130\icemcfd\win64_amd\bin\icemcfd.bat" -batch -script test0deg.rpl
"C:\ANSYS Inc\v130\fluent\ntbin\win64\fluent.exe" 2d -i test0deg.jou
goto directory, execute one program, execute the next program.
If I run it as it is, the first program executes and works fine and the cmd window closes
I'm having problems getting the second on to wait until the first one is finished.
start /wait doesn't work. When I use it it pops a new command window, does the first program (in the new window) then the new command window hangs there. when I manually close this new window it says continue batch file? and i type y and hit return and the second program starts.
Please help. I just want the first one to run and then the second to run after it.
p.s. the second program needs files that the first program makes. That's why I need it in such a way
Thanks in advance
Oliver
Use call to call a batch file:
call "C:\ANSYS Inc\v130\icemcfd\win64_amd\bin\icemcfd.bat" ...
I fixed it using the following:
cd/d H:\QUB\2dcfd
cmd /c "C:\ANSYS Inc\v130\icemcfd\win64_amd\bin\icemcfd.bat" -batch -script test0deg.rpl
"C:\ANSYS Inc\v130\fluent\ntbin\win64\fluent.exe" 2d -i test0deg.jou
The first one runs, then when it closes, the second one runs.