Batch script - IF not returning properly result - if-statement

I'm trying to make a batch script and i need to compare 2 variables which are integer numbers but i can't get it to work properly.
The comparison that i need to do is something like that:
if (201308281436 GTR 201308292309) (#ECHO true) Else (#ECHO false)
As far as i know (i don't know much about dos programming but i can do basic things), it should return "True" but it returns always "false".
I made some checks on cmd and this are the results:
if (1 GTR 2) (#ECHO true) Else (#ECHO false)
false
if (1 GTR 0) (#ECHO true) Else (#ECHO false)
false
the code that im using on bat script:
if (%filemodifieddate% GTR %filelastbackupdate%) (#ECHO true) Else (#ECHO false)
What am i doing wrong?
Thanks!

remove the parantheses!
if 1 gtr 0 (echo true) else echo false
You are comparing (1 with 0) instead of comparing 1 with 0
EDIT - The workaround given below only works if both numbers have the same number of digits. See Windows batch file IF failure - How can 30000000000000 equal 40000000000? for method to compare large integers with varying number of digits.
EDIT:
found a nice workaround: compare strings, not numbers (by adding a a in front of the numbers.
Experiment with this:
#echo off
if a%1 lss a%2 echo one
if a%2 lss a%1 echo two
(put it in a batchfile (t.bat) and execute with two parameters:
> t 1 3
one
> t 3 1
two
> t 54135143513513153 5613535314535543
one
> t 5613535314535543 54135143513513153
two
> t 5613535314535543 5413514351351315
two
> t 5413514351351315 5613535314535543
one
>

Related

How can I create a checkbox filter on c++

I'm having a problem that seems way more easy than it is. I think its more of an algorithmic problem than a coding one.
EDIT:
Imagine you have a database with a name and N boolean parameters, like if the person is blonde or not, if the person likes baseball or not, if person have a smartphone or not etc...
How could you print the name of someone that likes baseball AND is blonde, but doesn't matter if any of the other N parameters are true of false? How can I do that without having to write a test for every single of the (N^2)-1 possibilities?
I created a dictionary that maps a string to a struct with 4 boolean variables and some strings.
I want the user to select which booleans are important to them and return only the information that is true for all variable that he chose.
Something like a checkbox which you can use to filter a column in excel.
For instance if a user chose variables 1 and 2, I would like to know if there is a better way to return the result rather than testing every one of the 16 possibilities, like:
void filter(map<string, Mystruct> Mydictionary, bool bool1, bool bool2, bool bool3, bool bool4){
if(bool1 == true && bool2 == true && bool3 == false && bool4==false){
cout << Mydictionary.bool1Info << Mydictionary.bool2Info
if(bool1 == true && bool2 == false && bool3 == false && bool4==false)
...
Even more, for me its only important to test the booleans that the user picked up, so even if he didn't choose boolean3, it's not important to test if its true or false.
Any ideas?
I would be very glad if anyone could help me with this one
I can help you with your algorithm part ,
instead of checking all 16 possibilities just check 4 conditions separately for bool1,2,3,4 and print their info if they are true.
This way you can complete your task with only 4 if statements.
Hope this answers your query.

Multiple THEN statements in Tcl IF statement

I'm working with a Tcl script and want to do two separate things if an if statement is true. Code is below:
if { $Start_day >= 1 && $Start_day <= 15} {set Meteo_file "edas.$Start_month2$Start_year.001"
set Meteo_file_back "edas.$Start_month2_back$Start_year_back.002"}
However, I error out and get this:
wrong # args: should be "set varName ?newValue?"
while executing
"set Meteo_file "edas.$Start_month2$Start_year.001"
set Meteo_file_back "edas.$Start_month2_back$Start_year_back.002""
Is there any way (syntax) to have an if statement do two separate things when it comes true?
It would be more readable IMO to have them on separate lines:
if {$Start_day >= 1 && $Start_day <= 15} {
set Meteo_file "edas.$Start_month2$Start_year.001"
set Meteo_file_back "edas.$Start_month2_back$Start_year_back.002"
}
And I cannot get your error... The above works fine for me.
As a side note, could it be that you're using both set on the same line? I don't see why there'd be a lot of whitespaces to the right of the first set otherwise.

Batch File If Error

Ok so I am working on a project and I am doing this and I get to the if statement in confirmOne and it gives me "( was not expected at this time." Please help!
Many of the stray "You got to here!" messages are from me trying to debug it. I really need this soon. Please help. I also tried deleting parts and it still doesn't seem to work. If you see any other errors please tell me as I need all the help I can get. Thank you!
:grabInput
echo Please enter the username of the user you wish to access.
REM - } End Echoing Information/Main Menu | Grab Input {
set /p result=
goto correctName
REM - } End Grab Input | Process Input {
:correctName
set /p input=%result%
goto confirmOne
:confirmOne
echo Got to confirmOne
pause
if %input%==[] (
pause
cls
echo Oops! Looks like you didn't enter anything! Try Agian!
echo.
echo ................................................................................
echo.
goto grabInput
) ELSE (
goto confirmTwo
)
:confirmTwo
echo Got to ConfirmTwo
pause
if %input%==~help (
goto helpMenu
) ELSE (
goto confirmThree
)
:confirmThree
echo Got to ConfirmThree
if %input%==~info (
goto infoMenu
) ELSE (
goto swapDrive
)
Well, if you didn't enter anything for %input%, then your if statement would look like if ==[] (.
Your if statement should look like if [%input%] == [] (
I also see a lot of unnecessary code, you should take a look over your script.
Batch ALWAYS works on strings.
with the statement if %input%==[], when %input% is set to [nothing] (which is what you're trying the detect), batch substitutes [nothing] fo %input% and gets
IF ==[] (
and is confused because '(' is not a comparison operator.
[] is not some magic mantra. It's an old method of detecting the presence of parameters such that [%1] would equal [] if the parameter was absent. It doesn't work when the variable contains spaces or some other characters.
if "%var%"=="" is better
if not defined var is better still
Note that
set /p var=
will NOT set var to [nothing] in you simply press enter, It will leave var unchanged.
Hence this
set var=something
set /p var=
will leave var set to something. You should code this as
set "var="
set /p var="Some prompt "
if not defined var echo VAR is not defined
The quotes around var= ensures that var is NOT set to [some spaces] if there are trailing spaces on the line.
Other than that, the sequence
goto somelabel
:somelabel
(REM lines are irrelevant) is superfluous.
equally, in
if somecondition (goto somewhere) else (goto somewhereelse)
:somewhereelse
the else condition is superfluous
Batch only notices :label as a DESTINATION for a GOTO or a CALL. and will otherwise simply charge straight through any :label it finds as though they were remarks statements.

what is wrong with my if -condition?

So I've been trying to figure out what is wrong with my if-condition, but I am getting nowhere. I am still new to R, so maybe I am not understanding some very basic concept here?
I have a dataframe (dc) to which I appended a column with logical "FALSE". Now I want to change each FALSE into a TRUE based on the values in two columns of dc (dc$Probe and dc$Resp) that I specified using regexpr().
What it does so far is that, for both if-conditions, it changes each FALSE into TRUE regardless of the values in column 5 of dc. When I run the if-conditions seperately, I can see that they seem to be working fine on the OR-part of the condition, meaning the code only generates TRUE when the strings in dc$Probe match one of the strings specified in the OR-part. However, the AND-part seems to be ignored? Thus, when I run the complete code, I get a column with only TRUE, which is not what I want.
Edit: I should get a TRUE only if the string in Probe ends in a certain pattern (as specified in either of the two if conditions I wrote) and if the corresponding value in Resp is a "100" for the patterns specified in my first condition or a "200" for the patterns specified in my second condition. Thus, for strings ending in (sg|s|w1|w3|s1|s2), Resp must be "100" to get a TRUE and for strings ending in (\d\dg|\d\d), Resp must be "200" to get a TRUE. All other cases should be FALSE. For example, if a string ends in s1 and the corresponding value in Resp is 200, the code should return FALSE.
Edit: Some example data:
>dc<-data.frame(Subject=rep("SN",6), item.c=(1:6), Stim=c("XYZc02s03","XYZc01s30","XYZc02s29", "XYZc01s38", "XYZc02s11", "XYZc06w21"), Probe=c("XYzf02s03","XYZf01s30g","XYZf02s29w1","XYZf01s38sg","XYZf02s11s","XYZv06w21s1"), Resp=c(200, 100, 100, 100, 100, 200))
This is my code:
>dc$Resp<-as.character(dc$Resp) #column 5 in dc
dc$Probe<-as.character(dc$Probe)
dc$correct_response <- FALSE
for (i in 1:nrow(dc)) {
if (regexpr("^.*sg$", dc$Probe[i])==1 || regexpr("^.*s$", dc$Probe[i])==1 || regexpr("^.*w1$", dc$Probe[i])==1 || regexpr("^.*w3$", dc$Probe[i])==1 || regexpr("^.*s1$", dc$Probe[i])==1 || regexpr("^.*s2$", dc$Probe[i])==1 && dc[i,5]=="100") {(dc$correct_response[i]<- TRUE)}
if (regexpr("^.*\\d\\dg$", dc$Probe[i])==1 || regexpr("^.*\\d\\d$", dc$Probe[i])==1 && dc[i,5]=="200") {(dc$correct_response[i]<- TRUE)}
}
Is there something wrong with the regular expressions I am using? I checked them with glob2rx() and it seems like they are ok...Is my use of "OR" (||) or/and "AND" (&&) incorrect? How do I implement the AND-part properly? I have also tried the following code for the AND-part, but it didn't change anything:
regexpr("200", dc$Resp[i])==1
I read the R-help on regular expressions and control flow, but I still don't see what I am doing wrong. Consulting other webpages on logical expressions did not help me either.
Please help!
Im wondering if it can all be reduced to the following:
dc<- read.table(header=T,text="Subject item.c Stim Probe Resp
SN 1 XYZc02s03 XYzf02s03 200
SN 2 XYZc01s30 XYZf01s30g 100
SN 3 XYZc02s29 XYZf02s29w1 100
SN 4 XYZc01s38 XYZf01s38sg 100
SN 5 XYZc02s11 XYZf02s11s 100
SN 6 XYZc06w21 XYZv06w21s1 200")
cond1<-regexpr("^.*(sg|s|w1|w3|s1|s2)$", dc$Probe)==1 & dc$Resp==100
cond2<-regexpr("^.*(\\d\\dg|\\d\\d)$", dc$Probe)==1 & dc$Resp==200
dc$correct_response<-cond1|cond2
For one thing, you are missing a logical operator between the 2nd and 3rd clauses of your first if statement.

How can I use multiple conditions in "If" in batch file?

Can I specify multiple conditions with "or"/"and" in batch file if block?
If not that complex, can I at least use something like:
if value1 < value < value2
Basically my purpose is to check whether current system time falls in a certain interval(2.05 AM and 7.55 AM to be precise) and if it does, to execute certain commands.
Adding to dbenham's answer, you can emulate both logical operators (AND, OR) using a combination of if and goto statements.
To test condition1 AND codition2:
if <condition1> if <condition2> goto ResultTrue
:ResultFalse
REM do something for a false result
goto Done
:ResultTrue
REM do something for a true result
:Done
To test condition1 OR codition2:
if <condition1> goto ResultTrue
if <condition2> goto ResultTrue
:ResultFalse
REM do something for a false result
goto Done
:ResultTrue
REM do something for a true result
:Done
The labels are of course arbitrary, and you can choose their names as long as they are unique.
There are no logic operators in batch. But AND is easy to mimic with two IF statements
if value1 lss value if value lss value2 REM do something
Batch IF statements don't know how to compare times. Batch IF knows how to compare integers and strings.
One option is to convert the times into minutes or seconds past midnight.
The other option is to format the time with both hours, minutes (and seconds if needed) to be 2 digits wide each (0 prefixed as needed). The hours should be 24 hour format (military time).
You can break your condition into 2 and use if and if for setting 2 conditions
if %value% GTR %value1% (
echo Value is greater that %value1% ) else call :Error
if %value% LSS %value2% (
echo Value is less than %value2% ) else call :Error
::Write your Command if value lie under specified range
exit
:Error
echo Value doesn't lie in the range
::Write your command for if value doesn't lie in the range
exit
Set "IF=Set "or_=" & (If"
Set "OR=set "or_=1" )& (If "
Set "AND= If "
Set "THEN= set "or_=1" ) & If defined or_ "
Echo: Sample 1
%IF% 1 EQU 2 %OR% 6 NEQ 3 %OR% /I "stp" EQU "Stp" %THEN% Echo Pass
Echo: Sample 2
%IF% 1 EQU 2 %OR% 6 EQU 3 %AND% "stp" EQU "Stp" %THEN% (
Echo Pass
) Else Echo Faill
You may mix it but should keep in mind:
IF ~ if (
OR ~ ) OR (
AND ~ AND (parentless)
THEN ~ ) Then
So example 2 will be treated as:
If ( ex1 ) or ( ex2 and ex3 ) then