Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: BATCH - DATE /t combining with IF  (Read 10899 times)

0 Members and 1 Guest are viewing this topic.

Dronefang

  • Guest
BATCH - DATE /t combining with IF
« on: January 07, 2008, 12:21:13 PM »
Goodday,

i want to combine the DATE command with an IF command.
I'm not sure wether this code has any correct stuff in it, but this is the best i can do:
Code: [Select]
@echo off
if 'date' == 'mon 01/07/2008' then goto :play else goto :forbidden

:play
start notepad.exe
pause

:forbidden
exit
When i activate this code, it does open notepad but whatever date i put in, it still opens notepad.
I'm probably forgetting some basic stuff, but if some of you could please help me with this.

Further more, i would like the input (the dates) for the IF be taken out of a external .txt file which is favorably on the internet.

Thanks for helping,
Dronefang

diablo416

  • Guest
Re: BATCH - DATE /t combining with IF
« Reply #1 on: January 07, 2008, 02:33:25 PM »
@echo off
    IF "%date%"=="07/01/2008"  (
        Goto play
    ) ELSE ( 
        Goto forbidden
    )



if "%date%"=="01/01/2008" set td=g
if "%date%"=="07/01/2008" set td=g
if "%date%"=="14/01/2008" set td=g
if "%date%"=="20/01/2008" set td=g
if "%date%"=="27/01/2008" set td=g

    IF "%td%"=="g"  (
        Goto play
    ) ELSE ( 
        Goto forbidden
    )
[/b]

proper if syntax
every monday on in january

Dronefang

  • Guest
Re: BATCH - DATE /t combining with IF
« Reply #2 on: January 08, 2008, 04:39:52 AM »
Since cmd gives this date:
month/day/year
I just added both:
Code: [Select]
@echo off
if "%date%"=="01/08/2008" set td=g
if "%date%"=="08/01/2008" set td=g
if "%td%"=="g"
(Goto :play)
ELSE
(Goto :forbidden)

:play
start notepad.exe
echo Able to play
pause
exit

:forbidden
echo Unable to play
pause
exit
But this code doesn't work. I see the CMD window popping up but it disappears immediately without opening notepad or giving a pause.

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: BATCH - DATE /t combining with IF
« Reply #3 on: January 08, 2008, 05:44:16 AM »
Code: [Select]
@echo off
if "%date%"=="01/08/2008" set td=g
if "%date%"=="08/01/2008" set td=g
if "%td%"=="g" (Goto play) ELSE (Goto forbidden)

:play
start notepad.exe
echo Able to play
pause
exit

:forbidden
echo Unable to play
pause
exit

The window closes because you have a exit statement in your code. Unfortunately, by closing the window, you missed the error message for the if statement.

The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Dronefang

  • Guest
Re: BATCH - DATE /t combining with IF
« Reply #4 on: January 08, 2008, 06:36:31 AM »
When i took the code you put in your post, i did this:
When i delete the exit statement, it doesn't open notepad though it should  ??? but i do get the break/pause
When i don't delete the exit statement, it gets to pause and doesn't open notepad.
And what error should i get according to you from the if statement? I don't get an error

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: BATCH - DATE /t combining with IF
« Reply #5 on: January 08, 2008, 06:47:44 AM »
Are you running the batch file from the command line or double-clicking it in explorer? Did you delete both exit statements?

The error from the if statement: The syntax of the command is incorrect

When debugging batch files it's easier if you turn echo on so you can watch the values during execution.
                                             
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Dronefang

  • Guest
Re: BATCH - DATE /t combining with IF
« Reply #6 on: January 08, 2008, 07:03:26 AM »
I'm activating it via the Exploror with Vista Premium.
When i turn echo on i still don't get an error.

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: BATCH - DATE /t combining with IF
« Reply #7 on: January 08, 2008, 07:12:14 AM »
OK. Cool, so everything is fine? The batch file runs as it should?

The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Dronefang

  • Guest
Re: BATCH - DATE /t combining with IF
« Reply #8 on: January 08, 2008, 08:21:26 AM »
No, it runs but it doesn't open notepad and i don't see an error.
I think the date input is wrong because it goes to :forbidden instead of :play.

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: BATCH - DATE /t combining with IF
« Reply #9 on: January 08, 2008, 09:00:42 AM »
You may not have a syntax error. I ran your code on a XP machine. Vista may have accepted the if statement as it was.

Quote
I think the date input is wrong

Seems right. The if statement is the only logic point in the file. Turn echo on and run the batch file from the command prompt. You'll be able to see how %date% resolves and adjust the if statement accordingly.

Quote
When debugging batch files it's easier if you turn echo on so you can watch the values during execution.

I should have added the part about running from the command prompt.  :D
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Dronefang

  • Guest
Re: BATCH - DATE /t combining with IF
« Reply #10 on: January 09, 2008, 09:26:00 AM »
After correcting the date to todays actual date, i tried this:
On Vista:
I ran the file in CMD and made a snapshot:

As you can see nothing happens.
On Windows:
Again, I ran the file in CMD and made a snapshot:


I don't get an error on a XP machine and it pretty much looks the same. Why doesn't it work ?  ???

I'm working on the dutch versions of XP and Vista so "Druk op een toets om verder te gaan..." means "Press any button to continue..." or something like that.

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: BATCH - DATE /t combining with IF
« Reply #11 on: January 09, 2008, 12:15:21 PM »
I based my observation that the if statement was in error on reply #2. Apparently it was fixed.  :D 

When the %date% variable expands on your machine, the day of the week is included.  The right side of the equals does not include the day of the week. You can either add the day of the week to the right side or drop it from the left (not both).

This snippet drops the DOW from the left side:

Code: [Select]
@echo off
set today=%date:~4,10%
if "%today%"=="01/09/2008" set td=g
if "%today%"=="09/01/2008" set td=g
if "%td%"=="g" (Goto play) ELSE (Goto forbidden)

:play
start notepad.exe
echo Able to play
pause
exit

:forbidden
echo Unable to play
pause
exit

Good luck.  8) 
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Dronefang

  • Guest
Re: BATCH - DATE /t combining with IF
« Reply #12 on: January 10, 2008, 08:53:06 AM »
Thank alot, but now to my second problem:
Quote from: Dronefang
Further more, i would like the input (the dates) for the IF be taken out of a external .txt file which is favorably on the internet.
How can i solve this problem?

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: BATCH - DATE /t combining with IF
« Reply #13 on: January 10, 2008, 12:18:51 PM »
I'm not understanding your request.

Are the dates located in a file? and if so what is the format of that file? Are there multiple dates? Are there multiple lines?

If the file is located on the internet, the best batch code can do is launch the browser and indirectly a specific site. Batch code cannot interact with the browser.

Perhaps if we knew what you are trying to accomplish, it would be easier to find a solution.

Let us know.  8)

Just for my own knowledge, does Vista come with Powershell?
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Dronefang

  • Guest
Re: BATCH - DATE /t combining with IF
« Reply #14 on: January 12, 2008, 02:19:41 AM »
Format = .txt (if there's a better one, that's ok too)
Multiple dates = Yes
Multiple lines = X

I make the .txt format myself so i can put them on multiple lines or on just one line.

Quote from: Sidewinder
Batch code cannot interact with the browser.
So it would be impossible to let the same batch file update itself?

Quote from: Sidewinder
does Vista come with Powershell?
Yes, according to Wikipedia:
Windows Shell: The new Windows shell is significantly different from Windows XP, offering a new range of organization, navigation, and search capabilities.