Welcome to MSFN

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.


cloferba

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

20 posts in this topic

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?

0

Share this post


Link to post
Share on other sites

Here goes nothing.

if exist "C:\1.exe" (Start /wait "C:\1.exe")ELSE (shutdown -r)
exit

0

Share this post


Link to post
Share on other sites

thank you very mthis message appears:

"Windows is going to be restart"

is any way to hide this and restart?

0

Share this post


Link to post
Share on other sites

Could change it to this

SHUTDOWN -r -f -t 01

or this

WMIC OS Where Primary=TRUE Call Reboot

More details

0

Share this post


Link to post
Share on other sites

Powershell!

If (Test-Path C:\1.exe) {C:\1.exe} Else {Restart-Computer}

0

Share this post


Link to post
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

0

Share this post


Link to post
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.

0

Share this post


Link to post
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!

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
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)

0

Share this post


Link to post
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
0

Share this post


Link to post
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

0

Share this post


Link to post
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).

0

Share this post


Link to post
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
0

Share this post


Link to post
Share on other sites

N/M

Edited by MrJinje
0

Share this post


Link to post
Share on other sites

Did you tried this:

if not exist "%CDROM%\1.EXE" goto next
if exist "%CDROM%\1.EXE" (start "" /wait "%CDROM%\1.EXE")
:next
"D:\2.EXE"

0

Share this post


Link to post
Share on other sites

Fine, he's all yours...

Edited by MrJinje
0

Share this post


Link to post
Share on other sites
could be that "if" parameter is bad written?

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

Your if is bad written because you don't have good parenthesis. You need make every command go inside one set of parenthesis, but that doesn't matter, because you ignore my posts, so I am wasting my time telling you. Good luck figuring it out.

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

Well I disagree here, the code you've quoted was mine and was given because it was believed to be correct for the task which was intended.
if exist "%CDROM%\1.exe" (Start /wait "%CDROM%\1.exe") run "C:\2.exe" ELSE (run "C:\2.exe")
exit

It appears from that example that C:\2.exe is intended to be run regardless of whether or not %CDROM%\1.exe exists, (it is found on both sides of the ELSE statement). For that reason the parentheses are closed correctly.
0

Share this post


Link to post
Share on other sites

At the final script is resolved as follows:

@ Echo off
for %%i in (C: 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\1.exe start /wait %%i\1.exe
"%~dp02.exe"

The error was SET CDROM=%%i:

Edited by maestrodellaves
0

Share this post


Link to post
Share on other sites
At the final script is resolved as follows: <snip />
Well without seeing the script I would be hesitant in believing that anything has been solved.

If I was however going to use the 'check every letter of the alphabet' method then I'd probably use a system to break the loop when found. (the trouble with the method you've suggested is that if the file is found at D:, the script would still check for it a E: F: G:.. etc.)

@ECHO OFF
SETLOCAL
(SET HOST_DRV=)
FOR %%# IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO (
CALL :SUB %%#:||GOTO :NEXT)
:NEXT
REM The commands you provided in your last message.

IF DEFINED HOST_DRV START "" /WAIT "%HOST_DRV%\1.EXE"
IF EXIST "C:\2.EXE" START "" /WAIT "C:\2.EXE"

REM The rest of your commands go here.




GOTO :EOF
:SUB
IF EXIST %1\1.EXE (SET "HOST_DRV=%1"&&EXIT/B 1)
EXIT/B 0

0

Share this post


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

  • Recently Browsing   0 members

    No registered users viewing this page.