topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday March 29, 2024, 6:44 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Combining Batch Files  (Read 5712 times)

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Combining Batch Files
« on: April 25, 2013, 11:52 AM »
I have two very simple batch files to start and stop a single program. The first uses the 'start' command, the second the 'taskkill' command.
Is there a way to combine the two to create a toggle?
I have tried numerous options but they all finish up starting the program and then stopping it.
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi

jpfx

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 155
    • View Profile
    • Donate to Member
Re: Combining Batch Files
« Reply #1 on: April 26, 2013, 03:35 PM »
use a flag so the batch knows which routine to run?
use powershell get-process?
       |\      _,,,---,,_         
ZZZzzz /,`.-'`'    -.  ;-;;, 
      |,4-  ) )-,_. ,\ (  `'-'    
     '---''(_/--'  `-'\_)

x16wda

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 888
  • what am I doing in this handbasket?
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Combining Batch Files
« Reply #2 on: April 26, 2013, 06:07 PM »
I agree with jpfx, a flag file would be the simplest.

Code: Text [Select]
  1. if exist prog-running.txt goto running
  2. rem prog is not running
  3. copy NUL>prog-running.txt
  4. start vncviewer.exe
  5. goto alldone
  6. :running
  7. rem prog is already running
  8. del prog-running.txt
  9. taskkill /IM vncviewer.exe
  10. :alldone
vi vi vi - editor of the beast

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Re: Combining Batch Files
« Reply #3 on: April 27, 2013, 05:50 AM »
Thank you both for your replies.

I'm afraid that mention of flags means nothing to me unless they are fluttering from flagpoles, I have always been a practical person and any sort of programming language loses me quite early on.
I have bookmarks and text files containing all the information I could need on both batch files and AHK, to do just about anything they are capable of doing, but putting it together is something else.
I keep being reminded of when I started using logic gates in electronics, at least then I could get diagrams of the IC's to see what each pin was for.

With regard to my original post, after many hours of searching online I found THIS (comment 1) which solved the initial problem.

I have now moved on to trying to find ways to combine the 2 batch files in comment 9 of THIS thread, which will also give me the answer to combining the 2 batch files in comment 13 of the same thread.
My efforts so far seem to indicate that I actually need to create a third batch file in each case using IF and ELSE commands to trigger the other 2, when I tried to combine them I found that only 2 of the 3 programs were being triggered and it had no effect at all on the connection.
By the time I had got that far I needed to pack up, I'll start again when I have the time.
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi

x16wda

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 888
  • what am I doing in this handbasket?
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Combining Batch Files
« Reply #4 on: April 27, 2013, 08:38 AM »
Pilgrim, what I posted was basically the same as your comment #1 link.

From looking at the thread in the second link, is your intention that you want one script to do both your Internet_ON and Internet_OFF scripts?  If so we could pull the guts from your two scripts and put them into this framework, or just change this to call your existing batch files.  This would be the first option, with everything in one place:

Code: Text [Select]
  1. rem Check if our flag file exists and act accordingly
  2. if exist internet_is_on.txt goto running
  3.  
  4. rem Turning Internet items ON
  5. echo hah>internet_is_on.txt
  6.  
  7. start "" "C:\Program Files\D4\D4.exe"
  8. sleep 1
  9. start "" "C:\Program Files\Sandboxie\SbieCtrl.exe"
  10. sleep 1
  11. start "" "C:\Program Files\PeerBlock\peerblock.exe"
  12. sleep 3
  13.  
  14. devcon enable *DEV_001C*
  15.  
  16. goto alldone
  17.  
  18. :running
  19. rem Internet is ON, turn it all OFF now
  20. del internet_is_on.txt
  21.  
  22. taskkill /F /IM peerblock.exe
  23. taskkill /F /IM SbieCtrl.exe
  24. taskkill /F /IM D4.exe
  25.  
  26. devcon disable *DEV_001C*
  27.  
  28. :alldone

That said, when you reboot your computer the status of the programs and device might be out of sync with the "internet_is_on.txt" flag file.  If that happens you should be able to just run the script an extra time to get everything in sync.
vi vi vi - editor of the beast

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Re: Combining Batch Files
« Reply #5 on: April 27, 2013, 09:42 AM »
x16wda,

I have not had the time to look at things again but I caught your post and decided to quickly reply to it .

My idea is to keep things as simple as possible (for me).

In case you never caught it comment 9 in the second link is for Windows 7, comment 13 is for XP.
My thought is to create one batch file that will trigger the other two on either OS'.
For one thing that will save messing about with the batch files I already have which are working.
As I said, when I tried to combine the ON and OFF files only parts of them worked.

What I have in mind is something along these lines:

IF SBiectrl.exe is running start Internet_OFF.bat
ELSE start Internet_ON.bat

Obviously that is not exactly how it would look but it's where I'm starting from.

If I did not get anywhere with that another idea was to try and create a toggle so that each time I pressed the same hotkey it alternated between the two files, same outcome different perspective.
If that failed as well I was going to look at using AHK although I would prefer not to in this case.

Let me see what I can come up with before you suggest anything definite or do it for me.

When I've had another go I'll post the results either way.
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Combining Batch Files
« Reply #6 on: April 27, 2013, 10:55 AM »
Would this SO question help? (Not on a PC atm, so can't mock up smt)

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Re: Combining Batch Files
« Reply #7 on: April 28, 2013, 09:12 AM »
Ath,

Thanks for that, I've spent a lot of time on that site recently, along with Computer Hope it's where I've found the most information.

x16wda,

I've been over everything and solved all the problems except for one which I'll get to in a minute.
I can toggle between 2 batch files with the following:

if exist prog-running.txt goto running
rem prog is not running
copy NUL>prog-running.txt
start /D "C:\Batch Files\AHKOSK ON-OFF" AHKOSK_ON.bat
goto alldone
:running
rem prog is already running
del prog-running.txt
start /D "C:\Batch Files\AHKOSK ON-OFF" AHKOSK_OFF.bat
:alldone

I ran your script from reply 4, on XP it runs as is, on 7 I had to modify it.
Besides the differences in NIC details I cannot get the "" setting to run on 7, I  have to change it to /D, then it runs perfectly.
If I use "" I either get an error message or it opens an Explorer window to the relevant folder.
So the file on 7 reads thus:

rem Check if our flag file exists and act accordingly
if exist internet_is_on.txt goto running

rem Turning Internet items ON
echo hah>internet_is_on.txt

start /D "C:\Program Files (x86)\D4" D4.exe
start /D "C:\Program Files (x86)\Sandboxie" SbieCtrl.exe
start /D "C:\Program Files (x86)\PeerBlock" peerblock.exe

netsh interface set interface "Router Connection" ENABLED

goto alldone

:running
rem Internet is ON, turn it all OFF now
del internet_is_on.txt

taskkill /F /IM peerblock.exe
taskkill /F /IM SbieCtrl.exe
taskkill /F /IM D4.exe

netsh interface set interface "Router Connection" DISABLED

:alldone

The problem is that just like my original Internet ON-OFF files it leaves the icons in the system tray after the programs shut down on both OS'.
But, I have two files which toggle a single program:

@echo off

taskkill /f /im ahk_osk.exe || start /D "C:\Program Files\AHKOSK" ahk_osk.exe

This leaves the icon in place.

@echo off

tasklist /fi "ImageName eq ahk_osk.exe" | find /i "ahk_osk.exe" && taskkill /fi "ImageName eq ahk_osk.exe" || start /D "C:\Program Files\AHKOSK" ahk_osk.exe

This removes the icon.

My question is whether it is possible to incorporate something from this second file, or anything else for that matter, into your script from reply 4 so that it will automatically remove the icons when the programs are closed?
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi