1.

Solve : IF statement question????

Answer»

I have an application that writes a file and I have created a script to record the contents of that file to an environment variable. I want to write an IF statement to copy a file and make a registry change based on the variable. My problem is that in one instance it doesn't create an environment variable at all and so I need to adjust the IF statment for that.

This is what I have, but line :4 doesn't work. I can run this manuall and it work - 'if not exist %orcad_lm% regedit /s c:\orcad_dongle\server.reg' but all together this doesn't work... any ideas?



if %orcad_lm%==9-1b73c073 goto 1
if %orcad_lm%==9-3f0343b1 goto 2
if %orcad_lm%==9-5071f0d1 goto 3
if not exist %orcad_lm% goto 4

:1
copy c:\orcad_dongle\9-1b73c073.lic c:\orcad\license_manager\license.dat /y
regedit /s c:\orcad_dongle\local.reg
goto end


:2
copy c:\orcad_dongle\9-3f0343b1.lic c:\orcad\license_manager\license.dat /y
regedit /s c:\orcad_dongle\local.reg
goto end

:3
copy c:\orcad_dongle\9-5071f0d1.lic c:\orcad\license_manager\license.dat /y
regedit /s c:\orcad_dongle\local.reg
goto end

:4
regedit /s c:\orcad_dongle\server.reg
goto end

:END
exitQuote from: jerhiggs on July 03, 2007, 01:56:21 PM

I want to write an IF statement to copy a file and make a registry change based on the variable. My problem is that in one instance it doesn't create an environment variable at all and so I need to adjust the IF statment for that.

if not exist %orcad_lm% goto 4


If exist and if not exist only work with FILES.

if exist "C:\autoexec.bat" goto label

if not exist "D:\test\readme.txt" echo file not found

I presume this "works" because there is no file with a name corresponding to the expansion of the variable %orcad_lm% in the current directory:-

Quote
'if not exist %orcad_lm% regedit /s c:\orcad_dongle\server.reg'

if you want to check that a variable "does not exist", ie has no value, you need to perform this test

if "%orcad_lm%"=="" then goto 4



You SAID IF exist and if not exist only work with FILE...

I am not positive but I think maybe you meant that only "if not exist" works with files.

My IF exist statment works fine looking for a variable, so I'm a little confused why the IF not exist woudln't work.

Quote from: jerhiggs on July 11, 2007, 07:59:25 AM
You said IF exist and if not exist only work with FILE...

I am not positive but I think maybe you meant that only "if not exist" works with files.

My IF exist statment works fine looking for a variable, so I'm a little confused why the IF not exist woudln't work.

Maybe you need to read my post again a couple of times?

Anyway, you can check it yourself, you don't have to take my word if you don't want to. It continually surprises me that people have these odd ideas about DOS/NT commands which they could check just by

1. Opening a command window
2. Typing the command, followed by a space, a forward slash and a question mark

This is the standard DOS/NT command line way of accessing the help for a command.

Most of the queries on here wouldn't be needed if people did this.

So... just the first few lines of the IF help are all we need.

Remember, this is not my theory, this is Microsoft's own help. In fact I have been using BATCH files for over 20 years, so I think I know how IF works by now, but like I said, you don't believe me, take Microsoft's word for it, not mine.

I have made the relevant line red in colour.

Quote
c:\>IF /?
Performs conditional processing in batch programs.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

NOT Specifies that Windows XP should carry out
the command only if the condition is false.

So we see that IF EXIST works only with filenames. Like I said. The command is only executed if a file with a name corresponding to what follows EXIST actually exists. With IF NOT EXIST the command is only executed if the file with that name does NOT exist.

If you got IF EXIST to work with a variable, that variable MUST have held the name of a valid file.

eg

Quote
SET var="test.txt"
echo hello > test.txt
IF EXIST %var% echo yes
Ok, well since you are a "mentor" with 20 years of experience and you say that IF statements ONLY work with Files... then open up your own command window one more time and type these commands -- Let me know what you get.

set orcad_lm=test **HIT ENTER KEY**

Then type -

if %orcad_lm%==test notepad.exe **HIT ENTER KEY**

Did notepad open up OF COURSE IT DID! - Do you have a file called orcad_lm ANYWHERE on your PC I REALLY DOUBT IT!

You are still going to tell me, with your 20 years of of batch file usage, that the IF statement ONLY works with files?

How about this? Go back to your command window again and type "the command, followed by a space, a forward slash and a question mark".

Do you see above the "relevant line red in colour" where it says IF [NOT] ERRORLEVEL or string1 ? Do you really think that errorlevel or string1 are referring to filenames???

Read a little further down in the IF help where it states this -

The DEFINED conditional works just like EXISTS except it takes an
environment variable name and returns true if the environment variable
is defined. ENVIRONEMENT VARIABLE - just like in my batch script... hmmm?

Oh, and since you seem to believe that you know everything and I know nothing... Maybe you will believe MS. Here is a little something that Microsoft calls a knowledge base with SOLID proof (from Microsoft) that IF statements work with something other than files as you so kindly and all-knowingly pointed out to me - http://support.microsoft.com/kb/121170

Oh, and since you have NO idea who I am or what I know or how LONG I've been working with IF statements... why do you presume that you are the only one that could be correct? If you are going to talk crap and act like a know-it-all-bully around here... at least be able to back it up. It's people like you who troll forums looking for noobies to pick on because you have nothing better to do. 1072 (12.916 per day) posts in less than 3 months? I wonder how many of them are picking on people like this one? Sorry... this newb actually knows what he is talking about and did do research before I posted a legitimate question.

Is there anyone else out there who is actually willing to give some positive suggestions as to why my IF NOT statment doesn't work correctly?Quote from: jerhiggs on July 11, 2007, 11:58:26 AM
set orcad_lm=test**HIT ENTER KEY**

Now the variable orcad_lm is storing "test". OK so far.

Quote
Then type -

if %orcad_lm%==test notepad.exe **HIT ENTER KEY**

This says "if the variable orcad_lm expands to the string "test" then run notepad.exe". Surprise surprise, it worked.

Quote
Did notepad open up OF COURSE IT DID! - Do you have a file called orcad_lm ANYWHERE on your PC I REALLY DOUBT IT!

You are still going to tell me, with your 20 years of of batch file usage, that the IF statement ONLY works with files?

You are moving the goal posts. Let's see what you wrote before.

Quote
You said IF exist and if not exist only work with FILE...

I am not positive but I think maybe you meant that only "if not exist" works with files.

My IF exist statment works fine looking for a variable, so I'm a little confused why the IF not exist woudln't work.

Admit it. You SCREWED up. You seem to think that if you pretend you didn't type "if exist", then somehow it'll go away. Maybe, in a couple of years, when you hit 13, your ego won't be so fragile you have to hide from your mistakes by abusing others in an infantile fashion.

Quote
IF statements work with something other than files as you so kindly and all-knowingly pointed out to me

IF statements work with a variety of situations. It is you who didn't understand about the EXIST switch only working with files.

Quote
Is there anyone else out there who is actually willing to give some positive suggestions as to why my IF NOT statment doesn't work correctly?

Not the IF NOT EXIST statement you ACTUALLY asked about before? That is in your batch file you posted?







Discussion

No Comment Found