Link to home
Start Free TrialLog in
Avatar of jgaull
jgaull

asked on

Find Day of week through batch file

Does anyone know how to find the current day of week through a batch file running on Windows NT 4.0?

Thanks!
Avatar of czpczp
czpczp
Flag of United States of America image

Hi jgaull. This batch does what you described.  I also gave you some examples of usage.


@echo off
cls

echo.|date|find "is:" >G#t.bat
echo set day=%%4> Get.bat
call G#t.bat
del G#t.bat > nul


Rem The following are several example uses:

Rem echo to screen
echo Today is %day%


Rem or ipe to file
echo %day% > test.txt


Rem or create an environment variable
set SetDay=%day%
In my second to last REM statement, I meant to say "or pipe it to a file".
Avatar of jgaull
jgaull

ASKER

I copied the code provided and here's what I got:

The name specified is not recognized as an
internal or external command, operale program or batch file.
Today is


Here's the exact code I ran.  


@echo off
cls

echo.|date|find "is:" >G#t.bat
echo set day=%%4> Get.bat
call G#t.bat
del G#t.bat > nul


Rem The following are several example uses:

Rem echo to screen
echo Today is %day%


Pause
jgaull -- strange.   You don't have any typos...

It works perfectly fine for me on my NT 4.0 machine.

I even tried cutting and pasting your version of my batch and it worked fine.

Try following my exact steps:

 1) cut and paste into a batch file on your local drive
 2) click on Start/Run and type CMD and click OK
 3) go to where the file resides and execute


Let me know if this test works.


Additionally, are you shelling to a DOS prompt (if so, with cmd.exe or command.com) or are you running this via a shortcut?


Andother thing you can try is, add the following to the batch (it MUST be the FIRST line):

cmd /c
Also, the fact that you got this message,

" The name specified is not recognized as an
internal or external command, operale program or batch file."


tells me that the batch is unable to locate the FIND command that the batch tries to execute.  The find.exe resides in your \WINNT\SYSTEM32 folder.



Type PATH at DOS prompt to ensure that C:\WINNT\SYSTEM32 is included OR try executing the batch from THAT folder (by doing so, it'll tell you if your path is o.k.).


One last thing, the find.exe DOS command is copied to that folder by default during the installation of NT.  Make sure it's STILL there in that folder.  Some people delete certain DOS executables to increase drive space 'cause they feel they'll never use them.
Run:

echo.|date|find "is:" > c.c

and then run;

type c.c

and show us the output from the 'type' command.

Avatar of jgaull

ASKER

I looked at my path and c:\windows\system32 is in it (That's where find.exe) is located.  I tried running the batch file in that directory and it still didn't work.

I am not shelling out to DOS.  I get to it by typing "cmd" at the start..run box.

I added the cmd /c but it didn't do anything.


I ran echo.|date|find "is:" > c.c and it added the following text to the c.c file.

The current date is: Thu 06/07/2001


What I would like to do is have the "Thu" in a variable.  I think I'm close but still not there.

Thanks for your help!

Remove this line "del G#t.bat > nul" (actually, it should be "del G?t.bat > nul" -- the "?" will remove both temp files, get.bat and g#t.bat).


Then type out what each has in it.

get.bat should have "set day=%4"

g#t.bat should have "The current date is: Thu 06/07/2001
Run:

echo.|date|find "is:" > cc.bat

Run:

type cc.bat

Run:

cc

Under OS/2 Warp, I get:

G:\>Current date is: Thu  6-07-2001
SYS3088: The name CURRENT is not recognized as an internal
or external command, an operable program, or a batch file.

i.e., it is looking for a file called 'current.bat'.
Under your Operating System, it should be looking
for a file called 'the.bat' (but your file is named 'get.bat').

Run:

copy get.bat the.bat

Run;

cc.bat

which should run the 'the.bat' file.




Hi guys.  I rewrote the batch using a different method to get around the problem you're having.  It does what you're asking.  Give me a few minutes to test it a little more.

I'll be posting my second version in just a few minutes....
Hi.  This is another way to do it.  I'm making use of the FINDSTR command and the For statement.


@echo off
cls

echo.|date|find "is:" >Get.bat

findstr "is:" get.bat > Str
for /f "tokens=5 delims=" %%d in (str) do set day=%%d

del get.bat

Rem echo to screen
echo Today is %day%
Also, aside from deleting the get.bat, delete the "str" temp file.

del get.bat >nul
del str >nul
Avatar of jgaull

ASKER

I looked at my path and c:\windows\system32 is in it (That's where find.exe) is located.  I tried running the batch file in that directory and it still didn't work.

I am not shelling out to DOS.  I get to it by typing "cmd" at the start..run box.

I added the cmd /c but it didn't do anything.


I ran echo.|date|find "is:" > c.c and it added the following text to the c.c file.

The current date is: Thu 06/07/2001


What I would like to do is have the "Thu" in a variable.  I think I'm close but still not there.

Thanks for your help!

jgaull, please try my second batch file.
Avatar of jgaull

ASKER

When running the code on my machine, It says

Today is


Here's the code I ran:

@echo off
cls

echo.|date|find "is:" >Get.bat

findstr "is:" get.bat > Str
for /f "tokens=5 delims=" %%d in (str) do set day=%%d

del get.bat

Rem echo to screen
echo Today is %day%

Thanks!
Theres one typo.  There should be ONE space after the equal sign (delims= ") and before the " in the FOR line.

Use this:
for /f "tokens=5 delims= " %%d in (str) do set day=%%d
The tokens=5 and day=%%d do not need one space after the equal sign.  Just the delims= ".
If you can consider 3rd party, From NT ResourceKit I ran KiXtart from batch file to to this. While supplied by Microsoft, they don't claim to support their Kit, so ithey likely treat it as 3rd party app.  Links:

http://netnet.net/~swilson/kix/nfKiX.htm
http://www.microsoft.com/TechNet/inside/11-22-99.asp
http://www.kixtart.org/

It has macros that can give you DayOfWeek and Month, etc, either name or number, in a form that can then be used to easily place in a set variable or filename.

I used it more for naming of Directories, to pool storage for a number of applications under a single common directory for everything done to_date (erasing/clearing it at begin of day, then let it accumulate the days work effort). Satisfactory Batch Jobs on an NT4 box.
Fixing the typo in the For line as I mentioned will solve your problem.  Here is your final batch file:


@echo off
cls

echo.|date|find "is:" >Get.bat

findstr "is:" get.bat > Str
for /f "tokens=5 delims= " %%d in (str) do set day=%%d


del get.bat
del str

Rem echo to screen
echo Today is %day%
Nifty, use of 'find' above. Seems to be program ignored lately in the DOS topic area. FWIW, progress of jgaull to date seems to have at least the single line, so a quick fyi, this stage I find very useful to stick into my log files (text), using same technique redirecting I/O.

For the Kix piece, they were more geared for supporting user login script, but another 'pro' I liked was the ability to manage set variables for the different levels of user|local|machine, yielding improved control over the longevity and/or disposability.
ASKER CERTIFIED SOLUTION
Avatar of czpczp
czpczp
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jgaull

ASKER

When running the code on my machine, It says

Today is


Here's the code I ran:

@echo off
cls

echo.|date|find "is:" >Get.bat

findstr "is:" get.bat > Str
for /f "tokens=5 delims=" %%d in (str) do set day=%%d

del get.bat

Rem echo to screen
echo Today is %day%

Thanks!
Since you're using NT, you can take advantage of the /T switch in the Date command to accomplish what your want.  Here's essentially a ONE liner:


rem ~~~~~~~~~~~~~~~~~~~~~ Batch File 3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Echo off
cls
for /f %%a in ('date /t') do set %day%=%%a

Echo Today is %day%
rem ~~~~~~~~~~~~~~~~~~~~~ Batch File 3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Avatar of jgaull

ASKER

Thanks!   This worked for me.  Thank you for all of your time.  You have been very helpful.
Hi jagaull.   Glad to have helped.  Just one thing.  In your second to last post, the batch you pasted is the original one (the SECOND) that I gave you (you're still missing a space in the "delims= " line).  If you take a look at my posts I corrected it and showed you what to do.  You essentially now have three batch files that do the same thing WITHOUT ONLY giving you "Today is".

I agree, the third one was short and to the point.
I don't know what you're using this for but, if you want the day fully spelled out as opposed to Mon, Tue etc, put this at the END of your batch file:

if %day%==Mon set day=Monday
if %day%==Tue set day=Tuesday
if %day%==Wed set day=Wednesday
if %day%==Thu set day=Thursday
if %day%==Fri set day=Friday
if %day%==Sat set day=Saturday
if %day%==Sun set day=Sunday

echo Today is %day% !!
Looks like a lot of work.  I'm a fan of JPSoftware command shell replacement utility 4DOS/4NT.

Using 4DOS finding the day is a matter of accessing an internal variable:

c:\> echo %_dow
c:\> Fri

http://www.jpsoft.com/index.htm