bphlpt Posted January 13, 2015 Share Posted January 13, 2015 (edited) I thought comparing strings is always the same, no?Well, yes and no, 'SET' statements and compare tests are extremely literal and very, very picky as to format and can easily cause annoyingly hard to spot problems, especially for new programmers.This time the quote marks are messing you up in your 'set' statements 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: 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. The way you are setting 'type' is causing 'type' to contain embedded quotes, ie:set type="-ja"...set type="-ntb"means that [type] will NOT equal [-ja] or [-ntb] which is what I assume you intended, but rather [type] will equal ["-ja"] or ["-ntb"], including the quote marks. So, assuming the case of '3:my own PC', when you had quote marks surrounding %type% in your IF statementif %type%=="-ja" (actually evaluated to:if ""-ja""=="-ja" (which would fail every time. You could change your set statements to:if errorlevel==3 set "type=-ja"...set "type=-ntb"or justif errorlevel==3 set type=-ja...set type=-ntbto fix that particular problem.I assume you are relatively new to batch/cmd programming?Cheers and Regards Edited January 13, 2015 by bphlpt Link to comment Share on other sites More sharing options...
jaclaz Posted January 13, 2015 Share Posted January 13, 2015 Hmmm. @ECHO OFFSETLOCAL ENABLEEXTENSIONSSET yes="ja"SET yesCALL :check %yes%GOTO :EOF:checkECHO This is check, %1 without quotes is %~1 and inside quotes is "%~1" jaclaz Link to comment Share on other sites More sharing options...
Octopuss Posted January 13, 2015 Author Share Posted January 13, 2015 (edited) bphlpt, I can't say I understand. It seems like no matter what documentation I read, there is always a case where it doesn't apply somewhere.I thought I had to enclose the string I put in a variable in quotes to prevent the system from getting confused. You even said it yourself. And yet, here is a case where it is wrong.Btw I do use ss64 for reference most of the time. And yes, this is all pretty new to me. Not that I never touched batch files in the last 20 years, but this is about the most complicated stuff I ever attempted. It smells too much of programming, and my brain is just incompatible with that :/ Also, apparently it's impossible to insert empty string into a variable. Either there's a value or the variable is deleted.Expanding on that, and going back to the previous problem, is it even correct to do this? if "%brand%"==""Doesn't that actually check whether the variable equals two quotes rather than being empty? I've just gotten completely lost. I don't even know when am I supposed to use the quotes anymore. Guess all of this is lost to me. Even in such primitive form. Edited January 13, 2015 by Octopuss Link to comment Share on other sites More sharing options...
Octopuss Posted January 13, 2015 Author Share Posted January 13, 2015 One more mystery about all the quotes and whatnot.If set A="B" is wrong (I see it is now), how comes this works? :jaset architecture="x64"set language="en"set index="3"set personal="yes"goto :continue ...xcopy "e:\install\os\windows images\win7_%architecture%_%language%" d:\win7 /e /i /h /k /yIt correctly copies e:\Install\OS\Windows images\Win7_x64_en\ to d:\win7!! Link to comment Share on other sites More sharing options...
bphlpt Posted January 13, 2015 Share Posted January 13, 2015 ... is it even correct to do this?if "%brand%"==""Doesn't that actually check whether the variable equals two quotes rather than being empty? Yes it is OK, and no you are not checking "whether the variable equals two quotes rather than being empty". Situations like this is why I tend to use square brackets rather than quotes. If you rewrite the above using square brackets:if [%brand%]==[]does that make it clearer? You are comparing what is inside the brackets, or quotes. The brackets, or quotes, that are common to both sides get factored out, so to speak. Or you could do as I suggested above: ... this also works the same way:if not defined brand----------------------------------- One more mystery about all the quotes and whatnot.If set A="B" is wrong (I see it is now), how comes this works? set architecture="x64"set language="en"...xcopy "e:\install\os\windows images\win7_%architecture%_%language%" d:\win7 /e /i /h /k /yIt correctly copies e:\Install\OS\Windows images\Win7_x64_en\ to d:\win7!! The xcopy command is evaluated as:xcopy "e:\install\os\windows images\win7_"x64"_"en"" d:\win7 /e /i /h /k /yAs you've seen, xcopy is tolerant of paired, or an even number of quotes when they are located in that particular part of the command string. As you said. "It seems like no matter what documentation I read, there is always a case where it doesn't apply somewhere."Cheers and Regards Link to comment Share on other sites More sharing options...
Octopuss Posted January 13, 2015 Author Share Posted January 13, 2015 (edited) The brackets, or quotes, that are common to both sides get factored out, so to speak.Ah! That's what I needed to know. Or you could do as I suggested above:Yup, ended up doing just that. But I ran into another oddity there - when checking whether a variable is defined, for some obscure reason you have to do it without %%. That is even more confusing. Are you also saying that xcopy ignores quotes in the path as long as they are paired? That's pretty weird! I learned a lot today, even if I made myself look like a clueless id*** who shouldn't dig into anything deeper than Windows GUI I never knew a lot, and combined with being unemployed for 3 years, I forgot large part of the little I knew Edited January 13, 2015 by Octopuss Link to comment Share on other sites More sharing options...
bphlpt Posted January 13, 2015 Share Posted January 13, 2015 ... when checking whether a variable is defined, for some obscure reason you have to do it without %%. That is even more confusing. That's because when you add the %% then you are referring to the value of the variable, not the variable itself, and you are trying to find out whether the variable has been defined. So it all depends on whether you are talking about the variable or its value as to whether you use the %% or not. Read again the syntax of the IF statement at ss64. Of course sometimes you have to surround the variable with !! instead of %% to find out its current value. One more 'gotcha' for you to learn about. Look here - http://ss64.com/nt/delayedexpansion.html. Are you also saying that xcopy ignores quotes in the path as long as they are paired? That's pretty weird! I've never seen the written rules for that, I just tried various combinations as an experiment.Cheers and Regards Link to comment Share on other sites More sharing options...
DosProbie Posted January 20, 2015 Share Posted January 20, 2015 You could also streamline your script by adding copy and path variables as well, that way you can easily change your paths if they need revising, here is a snippet of code to demonstrate.~DP:: VariablesSet "Cpy=xcopy /eihky"Set Ck_Lang="d:\win7\boot\cs-cz\nul"Set Src_Path="e:\install\os\windows images\win7_%architecture%_%language%"Set Dest_Path="d:\win7"Set xml_Path="e:\unattended\keyb-EN-US+CZ-QWERTZ.xml"Set Install_Path="d:\win7\sources\$OEM$\$1\install":: Condition CheckIf Exist %Ck_Lang% (Set Language=-cz) Else (Set Language=)Goto Copy:Copy%Cpy% "%Src_Path%" "%Dest_Path%">nul%Cpy% "%xml_Path%" "%Install_Path%">nul Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now