Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


Photo

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

- - - - -

  • Please log in to reply
19 replies to this topic

#1
cloferba

cloferba

    Member

  • Member
  • PipPip
  • 119 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?


How to remove advertisement from MSFN

#2
MrJinje

MrJinje

    Tool™ Developer

  • Developer
  • 1,032 posts
  • OS:none specified
  • Country: Country Flag
Here goes nothing.

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


#3
cloferba

cloferba

    Member

  • Member
  • PipPip
  • 119 posts
thank you very mthis message appears:

"Windows is going to be restart"

is any way to hide this and restart?

#4
MrJinje

MrJinje

    Tool™ Developer

  • Developer
  • 1,032 posts
  • OS:none specified
  • Country: Country Flag
Could change it to this

SHUTDOWN -r -f -t 01

or this

WMIC OS Where Primary=TRUE Call Reboot

More details

#5
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,530 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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


#6
cloferba

cloferba

    Member

  • Member
  • PipPip
  • 119 posts
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


#7
MrJinje

MrJinje

    Tool™ Developer

  • Developer
  • 1,032 posts
  • OS:none specified
  • Country: Country Flag

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.

#8
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,530 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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!

#9
cloferba

cloferba

    Member

  • Member
  • PipPip
  • 119 posts
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

#10
maestrodellaves

maestrodellaves

    Newbie

  • Member
  • 10 posts
  • OS:Windows 7 x64
  • Country: Country Flag
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)


#11
MrJinje

MrJinje

    Tool™ Developer

  • Developer
  • 1,032 posts
  • OS:none specified
  • Country: Country Flag
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, 24 May 2010 - 12:30 AM.


#12
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,043 posts
  • OS:none specified
  • Country: Country Flag

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

#13
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,530 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

<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).

#14
cloferba

cloferba

    Member

  • Member
  • PipPip
  • 119 posts
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, 26 May 2010 - 11:45 AM.


#15
MrJinje

MrJinje

    Tool™ Developer

  • Developer
  • 1,032 posts
  • OS:none specified
  • Country: Country Flag
N/M

Edited by MrJinje, 26 May 2010 - 03:37 PM.


#16
allen2

allen2

    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,812 posts
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"


#17
MrJinje

MrJinje

    Tool™ Developer

  • Developer
  • 1,032 posts
  • OS:none specified
  • Country: Country Flag
Fine, he's all yours...

Edited by MrJinje, 26 May 2010 - 03:37 PM.


#18
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,530 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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.

#19
maestrodellaves

maestrodellaves

    Newbie

  • Member
  • 10 posts
  • OS:Windows 7 x64
  • Country: Country Flag
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, 31 May 2010 - 01:38 AM.


#20
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,530 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users



How to remove advertisement from MSFN