Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Batch: if statement

Reply
 
Old 10-11-2009   #1 (permalink)
Slickuser


 
 

Batch: if statement

I cannot find a BATCH scripting groups, was post in other group.

Hoping I will get quick respond here because this group is targeting
developers.



When I execute this batch script with two prompts set to N.
What am I doing wrong that it's entering the second "if statement"?

Result:
Enter (Y/N) N
Enter2 (Y/N) N
Msg 1
C_PATH exist
Should not be here!

@SET OVERWRITE=N
@SET /P OVERWRITE="Enter (Y/N) "
@SET C_PATH=C:\Windows

:: BEGIN
@IF "%OVERWRITE%" == "Y" (
@ECHO Hey %USERNAME%
@ECHO Got IN overwrite

@IF EXIST %C_PATH% (
@ECHO %C_PATH% exist
)

@IF "A" == "A" (
@ECHO Hello!!
)

@ECHO Should not display this message!
)

@SET COMPILE=N
@SET C_OVERWRITE=N
@SET /P COMPILE="Enter2 (Y/N) "

@IF "%COMPILE%" == "Y" (
@ECHO Hey2 %USERNAME%

:: Check if directory is already exist
@IF EXIST %C_PATH% (
@ECHO %C_PATH% exist
@SET /P C_OVERWRITE="Overwrite %C_PATH% [Y/N] "
)

:: Ask for overwrite
@IF "%C_OVERWRITE%" == "Y" (
@ECHO C overwrite
)

@ECHO Msg 1
@IF EXIST %C_PATH% (
@ECHO C_PATH exist
)
@ECHO Should not be here!
)

My System SpecsSystem Spec
Old 10-11-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: Batch: if statement


"Slickuser" <slick.users@newsgroup> wrote in message
news:36ec619e-29fd-4726-a702-a3c13605a814@newsgroup
Quote:

>I cannot find a BATCH scripting groups, was post in other group.
>
> Hoping I will get quick respond here because this group is targeting
> developers.
>
>
>
> When I execute this batch script with two prompts set to N.
> What am I doing wrong that it's entering the second "if statement"?
Your batch file needs some improvements:
- Having an @ at the start of each line is distracting. Use a single "@echo
off" at the beginning instead.
- Surrounding your == signs with spaces can cause trouble. Get rid of them!
- Setting variables inside a multi-line statement (i.e. a statement that
starts with a bracket and extends over several lines) does not work the way
you use it, because multi-line statements are scanned and interpreted just
once and *as a single line*. To make it work, you must force a rescan, using
the syntax below and surrounding rescanned variables with exclamation marks
instead of percentage signs.
- There is an extremely active batch file newsgroup: alt.msdos.batch.nt.

@echo off
SetLocal EnableDelayedExpansion

SET OVERWRITE=N
SET /P OVERWRITE="Enter (Y/N) "
SET C_PATH=C:\Windows

:: BEGIN
IF "%OVERWRITE%"=="Y" (
ECHO Hey %USERNAME%
ECHO Got IN overwrite

IF EXIST %C_PATH% ECHO %C_PATH% exist
IF "A"=="A" ECHO Hello!!
ECHO Should not display this message!
)

SET COMPILE=N
SET C_OVERWRITE=N
SET /P COMPILE="Enter2 (Y/N) "

IF "%COMPILE%"=="Y" (
ECHO Hey2 %USERNAME%

:: Check if directory is already exist
IF EXIST %C_PATH% (
ECHO %C_PATH% exist
SET /P C_OVERWRITE="Overwrite %C_PATH% [Y/N] "
)

:: Ask for overwrite
IF "%C_OVERWRITE%" == "Y" ECHO C overwrite
ECHO Msg 1
IF EXIST !C_PATH! ECHO C_PATH exist
ECHO Should not be here!
)


My System SpecsSystem Spec
Old 10-12-2009   #3 (permalink)
Al Dunbar


 
 

Re: Batch: if statement


"Pegasus [MVP]" <news@newsgroup> wrote in message
news:utNuFsjSKHA.1236@newsgroup
Quote:

>
> "Slickuser" <slick.users@newsgroup> wrote in message
> news:36ec619e-29fd-4726-a702-a3c13605a814@newsgroup
Quote:

>>I cannot find a BATCH scripting groups, was post in other group.
>>
>> Hoping I will get quick respond here because this group is targeting
>> developers.
>>
>>
>>
>> When I execute this batch script with two prompts set to N.
>> What am I doing wrong that it's entering the second "if statement"?
>
> Your batch file needs some improvements:
> - Having an @ at the start of each line is distracting. Use a single
> "@echo off" at the beginning instead.
> - Surrounding your == signs with spaces can cause trouble. Get rid of
> them!
Or, better still, use the newer relational operators:

EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal

These need to be separated from the operands, which makes things easier to
read.
Quote:

> - Setting variables inside a multi-line statement (i.e. a statement that
> starts with a bracket and extends over several lines) does not work the
> way you use it, because multi-line statements are scanned and interpreted
> just once and *as a single line*. To make it work, you must force a
> rescan, using the syntax below and surrounding rescanned variables with
> exclamation marks instead of percentage signs.
> - There is an extremely active batch file newsgroup: alt.msdos.batch.nt.
See also microsoft.public.win2000.cmdprompt.admin.

/Al
Quote:

>
> @echo off
> SetLocal EnableDelayedExpansion
>
> SET OVERWRITE=N
> SET /P OVERWRITE="Enter (Y/N) "
> SET C_PATH=C:\Windows
>
> :: BEGIN
> IF "%OVERWRITE%"=="Y" (
> ECHO Hey %USERNAME%
> ECHO Got IN overwrite
>
> IF EXIST %C_PATH% ECHO %C_PATH% exist
> IF "A"=="A" ECHO Hello!!
> ECHO Should not display this message!
> )
>
> SET COMPILE=N
> SET C_OVERWRITE=N
> SET /P COMPILE="Enter2 (Y/N) "
>
> IF "%COMPILE%"=="Y" (
> ECHO Hey2 %USERNAME%
>
> :: Check if directory is already exist
> IF EXIST %C_PATH% (
> ECHO %C_PATH% exist
> SET /P C_OVERWRITE="Overwrite %C_PATH% [Y/N] "
> )
>
> :: Ask for overwrite
> IF "%C_OVERWRITE%" == "Y" ECHO C overwrite
> ECHO Msg 1
> IF EXIST !C_PATH! ECHO C_PATH exist
> ECHO Should not be here!
> )
>


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
If statement with a message VB Script
Help with SQL statement VB Script
First 400 Records in SQL Statement VB Script
just a statement Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46