Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

IF Statements in Batch Files

Go to solution Solved by wanderingfool2,
15 hours ago, LinusTechTipsMember said:

can i add 2 programs? like 

Easiest way is just nesting it

 

echo off
tasklist /fi "imagename eq firefox.exe" | find /i ".exe" >null
if not errorlevel 1 (
echo "Firefox found checking for chrome"

	tasklist /fi "imagename eq chrome.exe" | find /i ".exe" >null
	if not errorlevel 1 (
		echo "both tasks found"
		echo "Do your work here for both found"
	) else (
		echo "firefox found not chrome though"
		echo "Do your work here if firefox detected but not chrome"
	)

) else (
	echo "task not found"
	echo "Do your work here if firefox not detected"
)

 

15 hours ago, LinusTechTipsMember said:

forgive me about the ethernet part,

i mean make an option for "Ethernet" only,

i want to make a command like

Easy enough to exploit the find command again...the find command processes line by line...so this time we exploit it by creating lines only with "Ethernet" in it, and the searching for "enabled"

 

echo off
netsh interface show interface | find /i "Ethernet" | find /i "Enabled" >null
if not errorlevel 1 (
	echo "disconnect code here"
) else (
	echo "Ethernet is currently disabled or doesn't exist"
)

 

15 hours ago, RZomerman said:

that is pretty old shit.. perhaps go more towards PowerShell - it allows way more variables and options.. and seems to be more of the default with later Windows versions.. (oh and it also runs on linux).. 

 

Or go Bash shellscript... but leave CMD in its grave..

It's old, but so many people overlook it just because of that.  I'd like to see something like this done in powershell with less effort (and the ability to just double click to run).  Lots of computers have powershell disabled or you need to do set-executionpolicy remotesigned when running it...which to me doesn't seem nearly as elegant and seems more cumbersome

 

Powershell is more powerful, but honestly given that it's not just double click and run is its downfall...along with the fact that it has a lot higher learning curve the cmd.

Hello, i just started learning cmd commands, and i need help for using if statements commands.

How to make commands like :

if firefox.exe ran/detected, then (pssuspend64 firefox.exe), else (goto chrome.exe) and 9pssuspend64 chrome.exe)

 

i don't know what command should i add if the program is detected in network > resource monitor

 

and second is,i want to make a batch file to enable and disable ethernet,like this:

:1

if ethernet enabled then (netsh interface set interface "Ethernet" disable)

else (echo it has been already disabled.)


:2

if ethernet disabled (netsh interface set interface "Ethernet" enable)
else ( (echo it has been already enabled.)

 

i need help to make commands that exist in cmd to those above run.

 

Thank you so much.
 

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Hey, what you're trying to do would suck to do in .bat files.
I recommend you take a look at powershell, it's generally WAY more powerful since you can access a lot of .NET functionality.
(also powershell is also available on other OS'es as well! You can install the same tools and use the same scripts on UNIX systems for example 😉 )
I recommend using either Powershell ISE or visual studio code to make a powershell script (.ps1) file.

Powershell lets you use most commands you can use in the regular commandline in windows, 
with additional .NET tools and generally deeper integration with things like checking processes n stuff like that. 
Conditionals in batch are a pain since you're required to use GOTO and LABEL which is.. painful. It's 2022, we shouldn't have to deal with goto anymore.

I found some things to get u started:

https://stackoverflow.com/questions/28481811/how-to-correctly-check-if-a-process-is-running-and-stop-it
 

and there's a netadapter module that will allow you to get info on your ethernet connection/adapter.

https://docs.microsoft.com/en-us/powershell/module/netadapter/?view=windowsserver2022-ps

good luck!

 


Sas

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, Sasbom said:

Hey, what you're trying to do would suck to do in .bat files.
I recommend you take a look at powershell, it's generally WAY more powerful since you can access a lot of .NET functionality.
(also powershell is also available on other OS'es as well! You can install the same tools and use the same scripts on UNIX systems for example 😉 )
I recommend using either Powershell ISE or visual studio code to make a powershell script (.ps1) file.

Powershell lets you use most commands you can use in the regular commandline in windows, 
with additional .NET tools and generally deeper integration with things like checking processes n stuff like that. 
Conditionals in batch are a pain since you're required to use GOTO and LABEL which is.. painful. It's 2022, we shouldn't have to deal with goto anymore.

I found some things to get u started:

https://stackoverflow.com/questions/28481811/how-to-correctly-check-if-a-process-is-running-and-stop-it
 

and there's a netadapter module that will allow you to get info on your ethernet connection/adapter.

https://docs.microsoft.com/en-us/powershell/module/netadapter/?view=windowsserver2022-ps

good luck!

 


Sas

thank you, powershell looks interesting, im looking for it right now, it is my first time learning like this, the links are also useful.

i have one more question, since it is for my friend, what is the other commands like "exist" in cmd, like "if exist app.exe (goto)"

a command that similar like "exist" that can detect if the program is running.

Link to comment
Share on other sites

Link to post
Share on other sites

18 hours ago, Sasbom said:

Hey, what you're trying to do would suck to do in .bat files.
I recommend you take a look at powershell, it's generally WAY more powerful since you can access a lot of .NET functionality.
(also powershell is also available on other OS'es as well! You can install the same tools and use the same scripts on UNIX systems for example 😉 )
I recommend using either Powershell ISE or visual studio code to make a powershell script (.ps1) file.

While I do understand the sentiment that you are saying, I generally disagree.  At work I use to use powershell for most of my day to day work, but anything that might have to be put into a computer and quickly run would be a bat.  The primary reason is that powershell on a large chunk of machines has a tendency to disabled, or else you need to run a command to run powershell (without signing).  At least with bat you can just double click and off you go.

 

With that said, yes bat script can be a major pain and not always the easiest to make.  If you get use to command though you can do quite a bit in workaround kind of ways and it's easier to understand (but there are some weird tricks)

 

To @LinusTechTipsMember you are looking for something like this

echo off
tasklist /fi "imagename eq firefox.exe" | find /i "firefox.exe" 
if not errorlevel 1 (
echo "task found"
echo "Do your work here if firefox is detected
) else (
echo "task not found"
echo "Do your work here if firefox not detected"
)

 

The second one, not sure exactly what you want.  Did you want to completely disable all interfaces then?  I mean you could always use the

netsh interface show interface

command and just use find for "Connected"...if you find it then you can disable that particular interface.  If you are targeting "Ethernet" interface only, then you can just feed each netsh interface show interface line one by one looking for "Ethernet" and checking that line to see if it's connected

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, wanderingfool2 said:

 

To @LinusTechTipsMember you are looking for something like this

 

echo off
tasklist /fi "imagename eq firefox.exe" | find /i "firefox.exe" 
if not errorlevel 1 (
echo "task found"
echo "Do your work here if firefox is detected
) else (
echo "task not found"
echo "Do your work here if firefox not detected"
)

 

The second one, not sure exactly what you want.  Did you want to completely disable all interfaces then?  I mean you could always use the

netsh interface show interface

command and just use find for "Connected"...if you find it then you can disable that particular interface.  If you are targeting "Ethernet" interface only, then you can just feed each netsh interface show interface line one by one looking for "Ethernet" and checking that line to see if it's connected

hello, thank you for coming,

can i add 2 programs? like 

tasklist /fi "imagename eq firefox.exe" && "imagename eq chrome.exe" | find /i "firefox.exe"  "chrome"

seem it is wrong command, hehe

 

forgive me about the ethernet part,

i mean make an option for "Ethernet" only,

i want to make a command like

 if "ethernet" enabled ,then disconnect it, else you have disabled it

thank you :D

Link to comment
Share on other sites

Link to post
Share on other sites

that is pretty old shit.. perhaps go more towards PowerShell - it allows way more variables and options.. and seems to be more of the default with later Windows versions.. (oh and it also runs on linux).. 

 

Or go Bash shellscript... but leave CMD in its grave..

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, LinusTechTipsMember said:

can i add 2 programs? like 

Easiest way is just nesting it

 

echo off
tasklist /fi "imagename eq firefox.exe" | find /i ".exe" >null
if not errorlevel 1 (
echo "Firefox found checking for chrome"

	tasklist /fi "imagename eq chrome.exe" | find /i ".exe" >null
	if not errorlevel 1 (
		echo "both tasks found"
		echo "Do your work here for both found"
	) else (
		echo "firefox found not chrome though"
		echo "Do your work here if firefox detected but not chrome"
	)

) else (
	echo "task not found"
	echo "Do your work here if firefox not detected"
)

 

15 hours ago, LinusTechTipsMember said:

forgive me about the ethernet part,

i mean make an option for "Ethernet" only,

i want to make a command like

Easy enough to exploit the find command again...the find command processes line by line...so this time we exploit it by creating lines only with "Ethernet" in it, and the searching for "enabled"

 

echo off
netsh interface show interface | find /i "Ethernet" | find /i "Enabled" >null
if not errorlevel 1 (
	echo "disconnect code here"
) else (
	echo "Ethernet is currently disabled or doesn't exist"
)

 

15 hours ago, RZomerman said:

that is pretty old shit.. perhaps go more towards PowerShell - it allows way more variables and options.. and seems to be more of the default with later Windows versions.. (oh and it also runs on linux).. 

 

Or go Bash shellscript... but leave CMD in its grave..

It's old, but so many people overlook it just because of that.  I'd like to see something like this done in powershell with less effort (and the ability to just double click to run).  Lots of computers have powershell disabled or you need to do set-executionpolicy remotesigned when running it...which to me doesn't seem nearly as elegant and seems more cumbersome

 

Powershell is more powerful, but honestly given that it's not just double click and run is its downfall...along with the fact that it has a lot higher learning curve the cmd.

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, wanderingfool2 said:

Easiest way is just nesting it

 

echo off
tasklist /fi "imagename eq firefox.exe" | find /i ".exe" >null
if not errorlevel 1 (
echo "Firefox found checking for chrome"

	tasklist /fi "imagename eq chrome.exe" | find /i ".exe" >null
	if not errorlevel 1 (
		echo "both tasks found"
		echo "Do your work here for both found"
	) else (
		echo "firefox found not chrome though"
		echo "Do your work here if firefox detected but not chrome"
	)

) else (
	echo "task not found"
	echo "Do your work here if firefox not detected"
)

 

Easy enough to exploit the find command again...the find command processes line by line...so this time we exploit it by creating lines only with "Ethernet" in it, and the searching for "enabled"

 

echo off
netsh interface show interface | find /i "Ethernet" | find /i "Enabled" >null
if not errorlevel 1 (
	echo "disconnect code here"
) else (
	echo "Ethernet is currently disabled or doesn't exist"
)

 

It's old, but so many people overlook it just because of that.  I'd like to see something like this done in powershell with less effort (and the ability to just double click to run).  Lots of computers have powershell disabled or you need to do set-executionpolicy remotesigned when running it...which to me doesn't seem nearly as elegant and seems more cumbersome

 

Powershell is more powerful, but honestly given that it's not just double click and run is its downfall...along with the fact that it has a lot higher learning curve the cmd.

thank you so much 😄 you are very kind

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×