Solved

Find Day of week through batch file

Posted on 2001-06-06
27
797 Views
Last Modified: 2007-12-19
Does anyone know how to find the current day of week through a batch file running on Windows NT 4.0?

Thanks!
0
Comment
Question by:jgaull
  • 16
  • 6
  • 2
  • +2
27 Comments
 
LVL 3

Expert Comment

by:czpczp
ID: 6161335
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%
0
 
LVL 3

Expert Comment

by:czpczp
ID: 6161365
In my second to last REM statement, I meant to say "or pipe it to a file".
0
 

Author Comment

by:jgaull
ID: 6163687
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
0
 
LVL 3

Expert Comment

by:czpczp
ID: 6163770
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
0
 
LVL 3

Expert Comment

by:czpczp
ID: 6163836
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.
0
 
LVL 12

Expert Comment

by:Otta
ID: 6164035
Run:

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

and then run;

type c.c

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

0
 

Author Comment

by:jgaull
ID: 6164318
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!

0
 
LVL 3

Expert Comment

by:czpczp
ID: 6164431
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
0
 
LVL 12

Expert Comment

by:Otta
ID: 6164498
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.




0
 
LVL 3

Expert Comment

by:czpczp
ID: 6164543
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....
0
 
LVL 3

Expert Comment

by:czpczp
ID: 6164557
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%
0
 
LVL 3

Expert Comment

by:czpczp
ID: 6164591
Also, aside from deleting the get.bat, delete the "str" temp file.

del get.bat >nul
del str >nul
0
 

Author Comment

by:jgaull
ID: 6164663
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!

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:czpczp
ID: 6164671
jgaull, please try my second batch file.
0
 

Author Comment

by:jgaull
ID: 6164690
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!
0
 
LVL 3

Expert Comment

by:czpczp
ID: 6164734
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
0
 
LVL 3

Expert Comment

by:czpczp
ID: 6164761
The tokens=5 and day=%%d do not need one space after the equal sign.  Just the delims= ".
0
 
LVL 24

Expert Comment

by:SunBow
ID: 6164828
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.
0
 
LVL 3

Expert Comment

by:czpczp
ID: 6164893
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%
0
 
LVL 24

Expert Comment

by:SunBow
ID: 6164894
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.
0
 
LVL 3

Accepted Solution

by:
czpczp earned 100 total points
ID: 6165108

Hi jgaull.  I now see why my first batch file didn't work for you.  Because I used Get as opposed to the word THE.  I was recently using a similar batch where I used GET as my parameter and I inadvertently used it in my example.  Using THE is VITAL for this batch to work.  So in the first batch you, if you still would like to use it, would replace G#t.bat with T#e.bat and Get.bat with The.bat.


How it works is, that in the first echo command, the T#e.bat gets the string "The current date is: Thu 06/07/2001" and the The.bat gets Set day=%4.

So, the third line "calls" T#e.bat which exectutes the The.bat file 'cause the word "THE" is the first word in "The current date is: Thu 06/07/2001".  It then utilizes REPLACEABLE parameters to set your environment variable (%0, %1, %2, %3, %4) which represents each word in that sentence.

%0 = The.bat  - it always the name of the of the batch file
%1 = "current"
%2 = "date"
%3 = "is:"
%4 = "Thu"  <----  This is where we get the day which we assign to variable "day"




Here are both batch files once again.


rem ~~~~~~~~~~~~~~~~~~~~~ Batch File 1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@echo off
cls

echo.|date|find "is:" >T#e.bat
echo set day=%%4 > The.bat
call T#e.bat
rem del T?e.bat > nul


Rem The following are several example uses:

Rem echo to screen
echo Today is %day%
rem ~~~~~~~~~~~~~~~~~~~~~ Batch File 1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





rem ~~~~~~~~~~~~~~~~~~~~~ Batch File 2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@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%
rem ~~~~~~~~~~~~~~~~~~~~~ Batch File 2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
 

Author Comment

by:jgaull
ID: 6165244
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!
0
 
LVL 3

Expert Comment

by:czpczp
ID: 6165248
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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
 

Author Comment

by:jgaull
ID: 6165258
Thanks!   This worked for me.  Thank you for all of your time.  You have been very helpful.
0
 
LVL 3

Expert Comment

by:czpczp
ID: 6165304
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.
0
 
LVL 3

Expert Comment

by:czpczp
ID: 6165452
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% !!
0
 

Expert Comment

by:jhawklyn
ID: 6169589
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

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Occasionally Windows/Microsoft Updates will fail to update. We have found a code that will delete all temporary files and re-register all dll's related to Windows/Microsoft Updates! This works 99% of the time to get the updates working again! The…
#Citrix #POC #XenDesktop #vCenter #VMware #ESX
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now