How to run a .vbs from a .bat - vbscript

Created an extensive batch script program to handle some automated file management and printing and I need to call a vbs file for its sendkeys operation. Is there a way to accomplish this without freezing the program?
I've tried START /WAIT my.vbs and the script freezes when it enters the .vbs
Anyone have other methods or switches you would recommend?
I would like it to run silently if at all possible, and i need the /WAIT switch because I need the sendkeys operation to complete prior to the next step in the batch file.

Instead of using START /WAIT my.vbs you could try using cscript //NoLogo //B my.vbs. You can also pass other options to cscript that way.

Just Call The vbs file correct path
The BAT file Edit it...!!!
wscript "file-path"
Example:
wscript "D:\KmaniZoro\PGM\N++\VBS\inputbox.vbs"

timeout 5
timeout /?
TIMEOUT [/T] timeout [/NOBREAK]
Description:
This utility accepts a timeout parameter to wait for the specified
time period (in seconds) or until any key is pressed. It also
accepts a parameter to ignore the key press.
Parameter List:
/T timeout Specifies the number of seconds to wait.
Valid range is -1 to 99999 seconds.
/NOBREAK Ignore key presses and wait specified time.
/? Displays this help message.
NOTE: A timeout value of -1 means to wait indefinitely for a key press.
Examples:
TIMEOUT /?
TIMEOUT /T 10
TIMEOUT /T 300 /NOBREAK
TIMEOUT /T -1

Create the .vbs file Then open the Batch file and enter START "" "FILE PATH"
EG: Start "" "C:\Users\%Username%\Desktop\Spiritual Aid\Program\2.vbs"
IT WORKED PERFERCTLY IN MY COMPUTER.

Related

Run .bat as administrator without manual confirmation

I want my .bat file to always run as administrator without me having to manually allow it to make changes to my device each time. It's part of a larger automated process and simply clears and resets the clipboard. Since it's automated I can't be there each time to hit yes.
wmic service where "name like '%%cbdhsvc_%%'" call stopservice
%windir%\System32\cmd.exe /c "echo off | clip"
wmic service where "name like '%%cbdhsvc_%%'" call startservice
Alternatively is there a way I could set it to run say every 5 seconds in the background with only a single confirmation until I tell it to stop or for a set period? Thanks
For only having to allow administrator once, you could use the timeout command.
:top
"code"
timeout /t 5 /NOBREAK > nul
goto :top
do timeout/? in cmd to see the usage.

Close and restart a process after a certain time period in batch

I'm trying to make a batch file that opens a certain exe and then, after (for example) 5 minutes, closes it then reopens it again. I have tried this:
#echo off
:loop
cd /d %~dp0
certain exe
timeout /t "time" (by minutes)
taskkill /f /im "certain exe"
goto loop
but it wouldn't close the exe nor open it, what can I do?
The problem is that cmd will wait for the current command to end before executing the next command, so timeout (and taskill) will not be executed until example.exe closes by itself.
To ensure that cmd doesn't wait example.exe, you'll need to use start /B, as in this example:
#echo off
:loop
start /B "example.exe"
ping -n seconds_of_delay+1 127.0.0.1>nul
taskkill /f /im "example.exe"
goto :loop
Replace seconds_of_delay+1 with the number of seconds you want the time period to last plus one.
/B is needed because, without it, start would execute example.exe in a new window (if you want that, simply use start).
I used ping as the way to create a time delay because it has been found to consume less processor time than sleep or timeout (you can find more details here).

Need a batch file to start, delay a close, and restart another batch file.

I have searched and searched and this is the closest code I have found:
#echo off
:loop
C:\CryptoCurrency\nexus_cpuminer\start.bat
timeout /t 30 >null
taskkill /f /im nexus_cpuminer.exe >nul
goto loop
A few things: notice the start.bat. The .exe I need to launch has to start via the .bat file because the .bat file contains information the .exe needs.
Secondly, the .exe launches a CMD prompt window which shows me what's going on.
(keep this in mind because this is not your normal .exe, I WANT that CMD prompt window to close when it's KILLED)
I am aware I have it set for 30 seconds. I'm just testing right now. I'd like to set it for 4 hours before the kill command is called. Also, I'd like to set a "delay" of 30 seconds before the whole process starts over. I am running Windows 7 x 64.
You must change the name of the second Batch file to other name (i.e. starter.bat) and execute it via the start internal command in order to execute it in parallel:
#echo off
:loop
start "" cmd /C "C:\CryptoCurrency\nexus_cpuminer\starter.bat"
timeout /t 30 >null
taskkill /f /im nexus_cpuminer.exe >nul
goto loop
The last line in starter.bat file must be the execution of nexus_cpuminer.exe, so when it is killed via taskkill, the .bat file ends immediately.
Another simpler approach is to directly execute nexus_cpuminer.exe in this Batch file, via start "" cmd /C nexus_cpuminer.exe command, so this process be opened in its own cmd.exe window.
If you CALL start.bat, it will return to your 'calling' script.
If you give start.bat a TITLE, you can /FIlter your TASKKILL command to EQ that WINDOWTITLE

Windows: Need to force close a process every few minutes so that batch script can start it up again

I have a batch script that starts up a process whenever it detects that the process was closed. I need to force close the process somehow on a set interval loop to see if the batch script is working as it should. What would be the best way to do this? I tried using another batch script to close the process, but didn't get the result as expected since both batch scripts open in the same command window.
In a second script, loop with goto and use a ping for pause.
:loop
:: 301 = 5 minutes. The first ping response is instant.
ping -n 301 0.0.0.0 >NUL
taskkill /im "program.exe" /f
goto loop

Making command prompt wait

#call JBOSSbuildWar.bat > ..\logs\JBOSSbuildWar_out 2> ..\logs\JBOSSbuildWar_err
start cmd /c #call WeblogicbuildWar.bat > ..\logs\WeblogicbuildWar_out 2> ..\logs\WeblogicbuildWar_err
start cmd /c #call FEBAPortletWebWar.bat > ..\logs\FEBAPortletWebWar_out 2> ..\logs\FEBAPortletWebWar_err
start cmd /c #call buildStaticJBoss.bat > ..\logs\JBOSSFEBAStaticWar_out 2> ..\logs\JBOSSFEBAStaticWar_err
I have this set of batch files getting executed in order. I want to fork out this so that they execute in parallel. I have done that using start cmd /c. Now this will fork out new command prompt window for each of them. Assume there are some set of statements after this. I want to make sure they get executed only after all the forked batch files are finished. How to achieve this?
Lets say average time taken by each file is:
JBOSSbuildWar- 30 minutes
WeblogicbuildWar- 35 minutes
FEBAPortletWebWar- 30 minutes
buildStaticJBoss- 35 minutes
Since the main command prompt window has completed its task in 30 minutes, and the forked batch files needs another 5 minutes to complete, I want the main cmd to wait until the others are done. Kindly help.
there's multiple commands, you can choose what one to use,
Pause
Will pause cmd until the user presses any key.
Displays message, "Press any key to continue..."
I use a certain variation of this command called pause>nul.
What it does is pauses the .bat, but doesn't show a message, You just get a blank screen!
TIMEOUT
Multiple syntaxes, very useful command, use this one quite a bit.
Sample code: TIMEOUT 10
Can be bypassed by a key press
Pauses the execution of command by 10 seconds.
You can also structure it like this: TIMEOUT 10 /nobreak
Which does not allow the user to press a key to skip it, they must endure it!
Ping
Very nice one.
Syntax is like this: PING 1.1.1.1 -n 1 -w 60000 >NUL
Probably most complex of the three.
PING can be used to delay execution for a number of seconds.
Hope I helped!
-Logan
Easy way:
In your main batch,
del "%temp%\*.processfinished" >nul 2>nul
...
start ....whatever1.bat...
start ....whatever2.bat...
start ....whatever3.bat...
...
wait4all:
for /L %%a in (1,1,3) do if not exist "%temp%\%%a.processfinished" timeout /t 1 >nul &goto wait4all
:: continues here when all (3) processes have finished.
del "%temp%\*.processfinished" >nul 2>nul
Then in each of the (3) subsidiary .bat files, create a file "%temp%\x.processfinished" where x=1 for the first process, 2 for the second and so on.
When the sub-processes have started, the procedure waits until each has created its own ".processcompleted" file by checking whether any of the 3 is missing, if it it, timeout for 1 second and look again. Only if the 3 files are present will it continue.
In all probability, it would be best if the subsidiary processes could take an extra parameter (the name of this sub-process's "processfinished" file) rather than having a fixed number for each.
You could extend this, and use say the date and time to augment the filename so that this entire process could itself be run many times in parallel.
BTW - by starting the procedure with
#echo off
you can remove all of the leading #s (all that does is suppress the command-reporting for that one line.)
Also, start is happier as start "" ....., that is, with an empty window title in quotes as its first argument. This allows other arguments to be "quoted" as necessary - the very first "quoted" argument used is used as the "window title" for the process and is likely to be lost to the sub-process. Routinely assigning a dummy (empty if necessary) "window title" means you don't trip over this problem in the future.
You can add this timer function before each of the commands:
#call JBOSSbuildWar.bat > ..\logs\JBOSSbuildWar_out 2> ..\logs\JBOSSbuildWar_err
Timeout /t 60 /nobreak >nul
start cmd /c #call WeblogicbuildWar.bat > ..\logs\WeblogicbuildWar_out 2> ..\logs\WeblogicbuildWar_err
Timeout /t 60 /nobreak >nul
start cmd /c #call FEBAPortletWebWar.bat > ..\logs\FEBAPortletWebWar_out 2> ..\logs\FEBAPortletWebWar_err
Timeout /t 60 /nobreak >nul
start cmd /c #call buildStaticJBoss.bat > ..\logs\JBOSSFEBAStaticWar_out 2> ..\logs\JBOSSFEBAStaticWar_err
Ps: 60 stands for 1 minute, if you want 30 minutes, change it to 1800

Resources