Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Long date variable in DOS

Status
Not open for further replies.

BarryMVS

IS-IT--Management
Apr 17, 2003
172
GB
Hi all,

I'm running a DOS BATCH logon script.

I want to record when everyone logs on to the server. By doing this I need to use the following line:
Code:
echo IN - %time% - %username% - %COMPUTERNAME% >>"m:\%date%.log"
This code creates and/or copies a log file with the date as it's name. The problem I have is the %date% variable returns the short date which has '/' which you can't use in a file name.
I need a variable for the long date format so that I can use that as the file name.

Any help would be most welcomed.

Thanks,

Barry

ICT Network Administrator
IT Services Manager
 
You need to change your date format in the Regional Settings. Make it use the period or dash instead.



"In space, nobody can hear you click..."
 
Hmm. I don't think the dash will work.. I wrote that a litte too fast, but the period will for sure, I use it. :)



"In space, nobody can hear you click..."
 
ReddLefty,

Thanks for your suggestion, but I've already tried changing the short date format which worked fine for my usage, (dd-MM-yyyy), but it crashed 3 different software packages that run on our network because they didn't recognise the date format!

I know, bad software, but it wasn't my choice and I just need a solution to this problem. So, this is why I can't use the short date format.

Any ideas on the long date variable?

Thanks for your help.

Barry

ICT Network Administrator
IT Services Manager
 
If your client PCs are Windows 2000 Pro (I think will work) or Windows XP you could parse the date using the FOR command:
Code:
for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do set dt=%%l-%%j-%%k
Using today's date, dt would be:
2004-05-11
Your command would then become:
Code:
echo IN - %time% - %username% - %COMPUTERNAME% >>"m:\%dt%.log"
If your clients include Win9x (which can't use that form of the FOR command), you could try either
a) having the date in an acceptable format in a publicly available file and reading the date from there or
b) calling the file something like "today.log" and having another job rename the file at midnight.
 
TO all,

Thanks for your input.

I need the files to have the date as their names so I can track any issues on those dates. Each file will then contain the login history for that day by all users.

I can't use short date format as it contains '/' which can not be used in file names and I can not change the short date format to '-' as that crashes software on the network.

The FOR command looks useful. Does this just change the date format for that instance rather than changing it for the regional settings of the client PC's?

If so, then that might well solve my problem.

Thanks for your help.

Barry

ICT Network Administrator
IT Services Manager
 
Crobin1,

Thanks for the FOR command. I can see how this works and I've tried it out.
It looks like it should solve the problem I'm having, but it seems to be missing part of the date in the result.

I copied your code exactly and entered it into a test batch file.
Code:
for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do set dt=%%l-%%j-%%k

echo IN - %time% - %username% - %COMPUTERNAME% >>"m:\%dt%.log"

Unfortunatlly, it is returning the result as

set dt=-05-2004

It seems to be missing the day from the date.

Thanks again for your help with this. I'll keep trying to see if I can solve it. Any other help would be welcomed.

Vbrocks - I am looking into using VB script, but I haven't had the time to try it out and I'm not sure if the scriptblocker in SAV will stop it from running. This is something that I will try out at a later date. Just need a quick solution now to get the system running.
Thanks for the suggestion though.

Cheers everyone. Help is most apprechiated.


Barry

ICT Network Administrator
IT Services Manager
 
It may be a difference in the reginal settings that you have. Try this:
Code:
for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do set dt=%%i-%%j-%%k-%%l
echo %dt%
See what output that gives you to figure out which variables are which parts of the date. Also, post an example of the date you get when you issue the command "date /t". It may be that the FOR command needs to be changed slightly.
 
Crobin,

Thanks.

We are deffinatlly closer. YOu're above code gives me the following result: 12-05-2004-
The date seems to have gained a hyphan but is looking better.

I'm in the UK and my regional short date format is displayed as: 12-05-2004

Thanks for your help.

Barry

ICT Network Administrator
IT Services Manager
 
OK, in that case I think what you need is:
Code:
for /f "Tokens=1-3 Delims=/ " %%i in ('date /t') do set dt=%%k-%%j-%%i
%%i should equal the day (12 for today)
%%j should equal the month
%%k should equal the year
This will put dt in YYYY-MM-DD format. If you want to arrange it in a different order, just change the order of the variables. For instance, to have it in DD-MM-YYYY order you would have ...set dt=%%i-%%j-%%k
 
Crobin,

Thanks for your help. That did it.

I have left them it in yyyy-MM-dd order to make any record easier to find.

Your help is much apprechiated.

Cheers,

Barry

ICT Network Administrator
IT Services Manager
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top