Jump to content

Need help with extremely simple batch file


Recommended Posts

My head is about to explode. Despite writing it just like it says in help, it doesn't work.

if exist d:\win7\boot\cs-cz\nul (set language=cz) else set language=

if %language%==cz (
copy e:\unattended\AeroNoBackground-cz.theme d:\win7\sources\$OEM$\$$\resources\themes\AeroNoBackground.theme
) else copy e:\unattended\AeroNoBackground.theme d:\win7\sources\$OEM$\$$\resources\themes

 

When I run it, I always get an error saying "( was unexpected at this time.". I don't get it. Please help.

Link to comment
Share on other sites


Try changing

if %language%==cz (

to

if [%language%]==[cz] (

or

if "%language%"=="cz" (

and/or change

else set language=

to

else set language=notcz

What I think you are running into was if language was empty or null or not defined, ie "else set language=", then your "if %language%==cz (" became "if ==cz (" and the syntax was just wrong and "( was unexpected at this time.". I hope that makes sense.

Cheers and Regards

Link to comment
Share on other sites

BUT you can also remove altogether the condition:

 

if exist d:\win7\boot\cs-cz\nul (

set language=cz
copy e:\unattended\AeroNoBackground-cz.theme d:\win7\sources\$OEM$\$$\resources\themes\AeroNoBackground.theme
) else (

set language=

copy e:\unattended\AeroNoBackground.theme d:\win7\sources\$OEM$\$$\resources\themes

)

 

 

jaclaz

Link to comment
Share on other sites

I got it. It was this: else set language=. After changing it to set language="", it worked. Apparently what I originally did didn't set the variable empty, but rather removed it alltogether (which I did notice when simply typing set, but didn't understand what was going on).

 

Jaclaz, you are probably right, but I didn't mention there was one more (might be even more in future if I bother with this stuff further) similar copy operation below. I did it like this to save a few lines.

 

I wish I wasn't so dumb when it comes to anything even remotely related to scripting/programming, but I just can't wrap my brains around it. No sh.. I failed even at such simple thing as Turbo Pascal 20 years ago :D

Edited by Octopuss
Link to comment
Share on other sites

I'm not completely sure, but if you don't need the "language" variable anywhere else I think you could also simplify it even further with:

 

if exist d:\win7\boot\cs-cz\nul (set language=-cz) else (set language=)

copy e:\unattended\AeroNoBackground%language%.theme d:\win7\sources\$OEM$\$$\resources\themes\AeroNoBackground.theme

 

If you do need the language variable elsewhere you could also change the AeroNoBackground-cz.theme to AeroNoBackgroundcz.theme and use:

 

if exist d:\win7\boot\cs-cz\nul (set language=cz) else (set language=)

copy e:\unattended\AeroNoBackground%language%.theme d:\win7\sources\$OEM$\$$\resources\themes\AeroNoBackground.theme

 

In any case, it is always better to enclose both sides of your compare tests with something and not leave them "naked" like you did - if %language%==cz ( - just to prevent potential problems or misunderstandings.

 

Cheers and Regards
Link to comment
Share on other sites

I have another stupid question (and I feel really dumb this time).

 

:ntb
set /p brand="Manufacturer's name: "
if %brand%=="" goto :noinput
goto :start

:noinput
echo.
echo. No input. Try again.
goto :ntb

 

No matter what I type, it always ends up with "goto was unexpected at this time". I read some documentation about the various commands several times and still don't understand why it doesn't work.

Link to comment
Share on other sites

I have another stupid question (and I feel really dumb this time).

 

You should. :)

 

In any case, it is always better to enclose BOTH sides of your compare tests with something ... just to prevent potential problems or misunderstandings.

 

Personally, I prefer using square brackets [ ] rather than quotes " ", but both will work.  A couple of references I use are http://ss64.com/nt/if.html and http://www.robvanderwoude.com/if.php

 

From the second one:

 

When comparing strings, make sure neither of them is empty or contains spaces only (both leading and trailing spaces are ignored).

Empty strings can be prevented easily by adding some meaningless but non-empty string at both sides of the equal sign:

IF X%1==X/? GOTO Helpscreen

Even if no parameter is given (in which case %1 is an empty string) the string before the equal sign won't be empty.

A commonly used way to prevent empty strings is enclosing them in either quotes or (square) brackets:

IF "%1"=="/?" ... or

IF [%1]==[/?] ...

If %1 itself may contain quotes you're in trouble: if %1 equals "/?" including the quotes, IF "%1"=="/?" ... evaluates to IF ""/?""=="/?" ... which will generate an error message.

As usual, jaclaz beat me with his post. :)

As a slight alternative to his code, this also works the same way:

:ntbset /p brand="Manufacturer's name: "if not defined brand echo.&echo. No input. Try again.&goto :ntb:start

Of course for both of our codes, no error checking is provided that a valid manufacturer has been entered. If the user enters nonsense, or even just a single space, then both codes will accept that just fine. (Unless some special problematic characters are entered, but that's a whole different set of problems.)

Cheers and Regards

Edited by bphlpt
Link to comment
Share on other sites

Well, usually 1/10th of the time is used to write the batch and the remaining 8/10th's are used to prevent the user from entering nonsense.

 

Before you ask, the 1/10th missing in the above is wasted in looking at squirrels ;) or similar.

 

jaclaz

Link to comment
Share on other sites

Hmmm. 

What about:

:ntbset /p brand=Manufacturer's name: if "%brand%"=="" echo.&echo. No input. Try again.&goto :ntbgoto :start

jaclaz

Ok... what does the & char do, and why does this work and what I did doesn't? Since I posted the question I tried it with the quotes, like so

 

P.S. These are all stupid little scripts I use for myself when I occasionally prepare a Windows image, so there's no need for any user stupidity checks (OR maybe is, lol).

 

edit:

If bold and red font wasn't enough... ok, "%brand%" is what it needed. I don't get it though!! Nowhere I looked did it say you have to compare strings like this. Grr.

Oh well.

This works:

:ntbset /p brand="Manufacturer's name: "if "%brand%"=="" (echo. No input. Try again.goto :ntb) else (goto :start)
Edited by Octopuss
Link to comment
Share on other sites

I am starting to think the whole thing is bugged.

 

if "%type%"=="some string" (

...several copy lines

)

 

This didn't work at all until I removed the quotes around %type%!!!

Edited by Octopuss
Link to comment
Share on other sites

Too little information.  What are the possible values of "some string"?  Can you share both the section of code where %type% is set and the section where it is tested and used?

 

Cheers and Regards

Link to comment
Share on other sites

I thought comparing strings is always the same, no?

 

Well this is the (almost, edited the actual copy/whatever operations out) whole thing:

choice /c:123 /m "Is this a: (1:desktop, 2:notebook, 3:my own PC, 4:N/A) ?"if errorlevel==4 goto :startif errorlevel==3 set type="-ja"if errorlevel==2 goto :ntbif errorlevel==1 set type=""goto :start:ntbset type="-ntb"set /p brand="Manufacturer's name: "if "%brand%"=="" (echo.echo No input. Try again.echo.goto :ntb) else (goto :start):startif exist d:\win7\boot\cs-cz\nul (set language="cz") else (set language="")...copy operationsif "%language%"=="cz" (copy "e:\unattended\keyb-EN-US+CZ-QWERTZ.xml" d:\win7\sources\$OEM$\$1\install /y) else (copy "e:\unattended\keyb-EN-US+CZ-QWERTY.xml" d:\win7\sources\$OEM$\$1\install /y)if %type%=="-ja" (...more copy operations)

When the .xml file is being chosen, the if works with quotes.

And it doesn't at the end. It makes zero sense. A string is a string. If command is always if.

Edited by Octopuss
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...