Jump to content

Recommended Posts


i have a batch file that echos the date into a txt file

echo %date% > c:\file.txt

i get this "Wed 05/11/2011"

how do i make the code just output "05/11/2011"

See here:

http://www.robvanderwoude.com/datetiment.php

Depending on how "universal" you want the solution to be, there are solutions anging from very simple to very complex.

A simple one, only valid for your example/language/regional setting could be:

FOR /F "tokens=2" %%A IN ("%Date%") DO ECHO %%A

Another one is:

CALL :SUB %Date%
....
GOTO :EOF
....
:SUB
ECHO %2
GOTO :EOF

jaclaz

Link to comment
Share on other sites

In answer to your first question

>C:\file.txt ECHO=%DATE:~-10%

For your second question, It can be done through a For loop with a counter and comparing the counter with the month name, but unless it is for a specific known locale etc. only I'd strongly suggest using something other than batch only functions for this stuff, (perhaps JScript).

Link to comment
Share on other sites

Here is a Cmd Script that makes a VBS script to make the date formate

EG Month Name, Day Name , Date, Year

CustomDateDemo.png

Save As MyDate.cmd


@Echo Off
CLS
Mode 55, 5
Color F9

Title Custom Date Demo
Set VBS=MyDate.vbs

Echo Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject") > %VBS%
Echo Dim Cmd, Ts >> %VBS%
Echo Cmd = "Date.cmd" >> %VBS%
Echo Set Ts = Fso.CreateTextFile(Cmd) >> %VBS%
Echo Ts.WriteLine "Set MyDate="^&MonthName(Month(Now)) ^& ", " ^& _ >> %VBS%
Echo WeekdayName(Weekday(Now)) ^& _ >> %VBS%
Echo ", " ^& Day(Now) ^& ", " ^& Year(Now) >> %VBS%
Echo Ts.Close >> %VBS%

%VBS%
call Date.cmd
Del %VBS%
Del Date.cmd
Echo.
Echo MyDate=%MyDate%
pause

Link to comment
Share on other sites

Here is autoit code that return a console output (The autoit script need to compile as console application)

ConsoleDate.au3

Select
Case @mon=01
$sMonth="Jan"
Case @mon=02
$sMonth="Feb"
Case @mon=03
$sMonth="Mar"
Case @mon=04
$sMonth="Apr"
Case @mon=05
$sMonth="May"
Case @mon=06
$sMonth="Jun"
Case @mon=07
$sMonth="Jul"
Case @mon=08
$sMonth="Aug"
Case @mon=09
$sMonth="Sept"
Case @mon=10
$sMonth="Oct"
Case @mon=11
$sMonth="Nov"
Case @mon=12
$sMonth="Dec"
EndSelect

$sCurrentdate=$sMonth&" "&@MDAY&", "&@YEAR
ConsoleWrite ($sCurrentdate); Will output to console as: May 12, 2011

See return sample

PYBRG.png

Edited by Geej
Link to comment
Share on other sites

Since you've had a hybrid VBScript batch and an AutoIt solution, I thought I'd provide the hybrid batch solution using JScript as I suggested earlier:

DateStrMon.cmd

@if (@X)==(@Y) @goto :Dummy @end/* Batch part
@echo off
for /f "tokens=*" %%# in ('cscript //nologo //e:jscript "%~f0"') do echo=%%#
pause
goto :eof
Jscript part */
var currMonth, currDate, currYear;
var dateObj = new Date();
var monthsArr = new Array();
currMonth = dateObj.getMonth();
currDate = dateObj.getDate();
currYear = dateObj.getFullYear();
monthsArr[0] = "January";
monthsArr[1] = "February";
monthsArr[2] = "March";
monthsArr[3] = "April";
monthsArr[4] = "May";
monthsArr[5] = "June";
monthsArr[6] = "July";
monthsArr[7] = "August";
monthsArr[8] = "September";
monthsArr[9] = "October";
monthsArr[10] = "November";
monthsArr[11] = "December";
WScript.Echo(monthsArr[currMonth] + " " + currDate + ", " + currYear);

Link to comment
Share on other sites

This "pure batch" should work.

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

::Get the current date format
FOR /F "tokens=2,3,4 delims=(/)" %%A IN ('VER ^| DATE ^| FIND "("') DO (
Set First=%%A
Set Second=%%B
Set Third=%%C
)

::Get the Current Date
FOR /F "tokens=2,3,4 delims=:/" %%A IN ('VER ^| DATE ^| FIND /V "("') DO (
Set /A First_value=%%A
Set /A Second_value=%%B
Set /A Third_value=%%C
)

::Add here Local settings
::This is Italian
IF "%First%%Second%%Third%"=="ggmmaa" (
SET Day=First
SET Month=Second
SET Year=Third
SET Lang=IT
)

::This is English-US
IF "%First%%Second%%Third%"=="mmddyy" (
SET Day=Second
SET Month=First
SET Year=Third
SET Lang=ENUS
)


::SET Variables
::Change string values of variables according to your language

IF "%Lang%"=="ENUS" Set Months=January February March April May June July August September October November December
IF "%Lang%"=="IT" Set Months=Gennaio Febbraio Marzo Aprile Maggio Giugno Luglio Agosto Settembre Ottobre Novembre Dicembre


Set Counter=0
For %%A IN (%Months%) DO (
SET /A Counter+=1
SET Month_!Counter!=%%A
)

SET Index=!%Month%_value!

::Example output (change as you wish order of variable output and/or add separators)
ECHO !%Day%_value! !Month_%Index%! !%Year%_value!

jaclaz

Link to comment
Share on other sites

This "pure batch" should work.

Scripts which are required to output localized Strings are often going to have to be altered for each environment. The example you have posted above is basically Bilingual, we could perhaps expand it by adding German, but where would it end?
Link to comment
Share on other sites

This "pure batch" should work.

Scripts which are required to output localized Strings are often going to have to be altered for each environment. The example you have posted above is basically Bilingual, we could perhaps expand it by adding German, but where would it end?

Sure :), I know where it starts, not where it ends....

...till then if you have an Italian system it works (tested) should also on a En-Us one (untested).

In other words it wasn't meant as the "ultimate batch solution to all date formatting problems" :w00t: , but rather as a "quick and dirty batch solution that may work, and YMMV" ;).

I presume that members using another localization can add their own "months" and "country code", since the few lines that need changes/additions are quite clearly laid down.

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

And if we are allowed to use WMIC, we have "datepart.cmd":

http://www.robvanderwoude.com/wmic.php

that should be "language independant" :unsure: (or at least it works allright in Italian system too :angel ) and that should be easily adapted to show the date info any which way you like.

At it's essence it resolves in:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
FOR /F "skip=1 tokens=1-3" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Month^,Year /Format:table 2^>NUL') DO ECHO Today is %%A/%%B/%%C

But of course you still need a "local language" array of strings for "months names". (or if you also want them, "day names")

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...