0

I need to add certain files to TodayDate.rar for this I am using cmd batch file with following code which is working fine.

"C:\Program Files\WinRAR\rar" a -agmmddyy ".rar"

Now I am changing above code and using If Condition as in following code then it is not working. why?

if %time:~0,2% lss 13 (
"C:\Program Files\WinRAR\rar" a -agmmddyy " Morning.rar"
) else (
"C:\Program Files\WinRAR\rar" a -agmmddyy "Afternoon.rar"
)

Actually I need to do this Two time, first in the morning and second in afternoon that's why I added if condition to evaluate time and write the Name of file with Morning and Afternoon accordingly.

UPDATE:

I have checked both conditions (before and after HH=13) and its working fine, showing results "yes" and "no" respectively, using following code:

if %HH% LSS 13 (
echo "yes" 
) else (
echo "no" 
)

But when I used RAR to above-mentioned code, it is not working. please help: showing no result

if %HH% LSS 13 (
"C:\Program Files\WinRAR\rar" a -agmmddyy "Morning .rar"
echo "yes" 
) else (
"C:\Program Files\WinRAR\rar" a -agmmddyy "Afternoon .rar"
echo "no" 
)

I tried to use following still No result:

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%" & set "hh=%dt:~8,2%"
rem M=month D=day Y=Year h=hour
set datestamp=%MM%%DD%%YY%
echo %HH%
ren "CEEMEA & LATAM.Docx" "CEEMEA & LATAM %datestamp%.Docx"
"C:\Program Files\WinRAR\rar" a -agmmddyy ".rar" '''''' THIS LINE IS WORKING FINE
pause
if %HH% LSS 13 (
"C:\Program Files\WinRAR\rar" a -agmmddyy "Morning .rar"
echo "yes" 
) else (
"C:\Program Files\WinRAR\rar" a -agmmddyy "Afternoon .rar"
echo "no" 
)
pause
Ibn e Ashiq
  • 623
  • 2
  • 9
  • 23
  • 2
    How is your `%time%` formatted? Leading zero or leading space before 10 am? Oh, and there is a mandatory space before the opening paranthese: `... lss 13 (` – Stephan Apr 03 '20 at 07:44
  • @Mofi right now its is `1:13 PM` and `echo %TIME%` shows 13:13:35.25 in my machine – Ibn e Ashiq Apr 03 '20 at 08:14
  • yes, we got that. But what does it show *before* 10 AM? `8:15:59.99` or `08:15:59.99` or `8:15:59.99`? – Stephan Apr 03 '20 at 08:28
  • Npocmaka did a nice listing of [possible solutions](https://stackoverflow.com/a/19799236/2152082) to get a date/time string independent of locale settings. – Stephan Apr 03 '20 at 08:33
  • @Stephan before 10 am it shows with `Space` and without `Zero` for example ` 1:45:19.54` – Ibn e Ashiq Apr 03 '20 at 08:45
  • 2
    in this case, simply adding the mentioned space (correcting the `if` syntax) should be enough to make it work. If not, run it from an already open command prompt and with `echo on` and analyze/post the exact output. – Stephan Apr 03 '20 at 08:51
  • @Stephan I have updated my question, please take a look, the syntax is correct and I checked all conditions. there is another issue. – Ibn e Ashiq Apr 07 '20 at 09:56
  • `%HH%` will have a leading zero before 10:00, so it will be treated as an octal value. `08` and `09` are invalid octal values, so you'll get an error between 08:00 and 10:00. The easiest way to cure that is to force string comparison: `if "%HH%" LSS "13"` – Stephan Apr 07 '20 at 10:21

0 Answers0