Jump to content

batch dont work! "if file exist, run, else, exit"


Recommended Posts

im trying to get this bach work, but i cant!

it is very simple, but i have problems with the syntaxis:


IF EXIST "C:\1.exe"
RUN /wait "C:\1.exe"
ELSE
shutdown -r
EXIT

the shutdown -r is for reboot the pc if the file doesnt exist

anyone can help me?

Link to comment
Share on other sites


with this it should work

SHUTDOWN -r -f -t 01

thank you very much :thumbup

other question:

if exist "%CDROM%\Autowpi.exe" (Start /wait "%CDROM%\Autowpi.exe")ELSE (SHUTDOWN -r -f -t 01)
exit

after execute autowpi.exe i need to run other application, so how can specify that?

this is what i have tried but it doesnt work:

if exist "%CDROM%\Autowpi.exe" (Start /wait "%CDROM%\Autowpi.exe") run "C:\application.exe" ELSE (SHUTDOWN -r -f -t 01)
exit

Link to comment
Share on other sites

after execute autowpi.exe i need to run other application, so how can specify that?

this is what i have tried but it doesnt work:

if exist "%CDROM%\Autowpi.exe" (Start /wait "%CDROM%\Autowpi.exe" && start /wait "C:\application.exe")  ELSE (SHUTDOWN -r -f -t 01)
exit

the double ampersand && means run the second command if (and only if) the first command is successful.

Link to comment
Share on other sites

after execute autowpi.exe i need to run other application, so how can specify that?

this is what i have tried but it doesnt work:

if exist "%CDROM%\Autowpi.exe" (Start /wait "%CDROM%\Autowpi.exe") run "C:\application.exe" ELSE (SHUTDOWN -r -t 0)
exit

I'd suggest you don't confuse your script with concatenation!

I would also suppose that using an if else is not required, by rebooting on failure as your first command, the two other commands wouldn't run anyhow

IF NOT EXIST %CDROM%\Autowpi.exe SHUTDOWN -r -d p:4:2 -t 0
%CDROM%\Autowpi.exe
%SYSTEMDRIVE%\application.exe

If for some reason the commands start running before the supposedly immediate reboot, you could add START /WAIT before SHUTDOWN!

Link to comment
Share on other sites

  • 2 weeks later...

sorry guys for a late reply...my pc was infected with virus :realmad:

i have tried to make this run on Windows7, but it doesnt work

i still having problem with this:

IF EXIST "%CDROM%\1.exe" 
RUN /wait "%CDROM%\1.exe"
ELSE
RUN /wait "%CDROM%\2.exe"
EXIT

so..following Yzöwl instructions, i have tried this:

if exist "%CDROM%\1.exe" (Start /wait "%CDROM%\1.exe") run "C:\2.exe" ELSE (run "C:\2.exe")
exit

i have replaced shutdown command for 2.exe application...but there is no way to run this cmd ok on W7! :wacko:

dont know why! need help :o

Link to comment
Share on other sites

exact fault code is:

@echo off
FOR %%i IN (D E F G H I J K L M N O P Q R S T U V W X Y Z) DO IF EXIST %%i:\autowpi.exe (SET CDROM=%%i: && start/b/Wait %CDROM%\autowpi.exe && "%~dp0Install.exe" /silent /restart /l=Default) ELSE ("%~dp0Install.exe" /silent /restart /l=Default)

Link to comment
Share on other sites

You should have just tried my example, I have answered this question two posts back.

1) double check your parenthesis (), as they need to go around both commands before the "Else" statement. Follow the sample I posted. It is tested and working.

2) include the double ampersand && between your first and second commands, just like in my example

3) you do not "Run" anything, there is no such thing as the "run" command inside of batch environment. Instead you should "start /wait" both commands so that your script does not move on before you are done.

C:\Users\MrJinje>run

'run' is not recognized as an internal or external command,

operable program or batch file.

EDIT: Also %CDROM% is not defined in any examples here, except for Maestro's. You may be better off just editing his sample to suit your needs.

Edited by MrJinje
Link to comment
Share on other sites

so..following Yzöwl instructions, i have tried this:

I would call that "completely ignoring Yzöwl suggestions" :ph34r:

You need to get a basic understanding of IF ELSE:

IF CONDITION==TRUE DO_SOMETHING ELSE DO_ANOTHER_THING

is equivalent to:

IF CONDITION==TRUE (DO_SOMETHING) ELSE (DO_ANOTHER_THING)

If you need more than one action performed, you need to use a single ampersand:

IF CONDITION==TRUE (DO_SOMETHING&DO_THIS&DO_THAT) ELSE (DO_ANOTHER_THING&ALSO_DO_THIS)

Or, more easily and readable, structure the code on multiple lines:

IF CONDITION==TRUE (
DO_SOMETHING
) ELSE (
DO_ANOTHER_THING
)

The above allows for mutiple commands WITHIN brackets:

IF CONDITION==TRUE (
DO_SOMETHING
DO_THIS
DO_THAT
) ELSE (
DO_ANOTHER_THING
ALSO_DO_THIS
)

jaclaz

Link to comment
Share on other sites

<snip />

so..following Yzöwl instructions, i have tried this:

if exist "%CDROM%\1.exe" (Start /wait "%CDROM%\1.exe") run "C:\2.exe" ELSE (run "C:\2.exe")
exit

<snip />

Actually if you look they are not my instructions at all, it is the exact same structure you posted last time, (which I corrected), the only difference is that you've changed 'Autowpi.exe' for '1.exe', 'application.exe' for '2.exe' and 'SHUTDOWN -r -t 0' for '2.exe'.

Looking at what you've posted I'm assuming that this will do the same thing:

IF EXIST "%CDROM%\1.EXE" (START "" /WAIT "%CDROM%\1.EXE")
"C:\2.EXE"

In order for that and the other examples we've given you to work you need to ensure that you have defined the %CDROM% variable; (maestrodellaves has previously given you one way to do so).

Link to comment
Share on other sites

well..i have tried on many ways but when %CDROM%\1.EXE doesnt exist, the cmd doesnt continue with the script execution (2.exe) and the cmd is closed

if 1.exe exist, the cmd works excelent :blink:

could be that "if" parameter is bad written?

(i also have tried Yzöwl commands):

IF EXIST "%CDROM%\1.EXE" (START "" /WAIT "%CDROM%\1.EXE")
"C:\2.EXE"

and then

if exist...

else...

if no exist...

goto...

&&...

but no way

PD: if i have %cdrom% on a pendrive..or hard disk..all works fine, the problem is when %cdrom% is a cdrom or a dvd :wacko:

Edited by cloferba
Link to comment
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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...