Jump to content

%YEAR% Variable in Batch File


Recommended Posts

Once again I am requesting help for my batch file... all of this wouldn't happen if I had a computer BEFORE Windows 95 got out. ;)

All I want is the code to get the current year to my RunOnceEx title. I've been searching on the net and I found some variable like DATE/T and TIME/T but I'm stuck.

This is what I want:

REG ADD %KEY% /V TITLE /D "IT Departement %YEAR%" /f

so this way, I wouldn't have to care to update the year in all my batch files.

Link to comment
Share on other sites


well this is what i use on my programs.cmd, hope it helps...

SET %TIME%=TIME /T
ECHO.
ECHO.

ECHO [ Estableciendo Variable "CDROM" ]
FOR %%d IN (c: d: e: f: g: h: i: j: k: l: m: n: o: p: q: r: s: t: u: v: w: x: y: z:) DO IF EXIST %%d\$OEM$\BioHaZaRD.nfo SET CDROM=%%d
ECHO Unidad %CDROM%, %TIME%

ECHO.
ECHO Instalando AveDesk 1.1 (En), %TIME%
ECHO Por favor espera...
..

Link to comment
Share on other sites

try this

set YEAR=%DATE:~-4%

%DATE% on my system is

Thu 12/23/2004

and after the above SET command I get

echo %YEAR%
2004

:~-4 gets the last 4 digits.

Also remember

SET %YEAR%=

is NOT proper syntax. should be

SET YEAR=

Then to use YEAR you put the %'s on either side.

Edited by Alanoll
Link to comment
Share on other sites

I get this:

2-23

:lol:

I need the first 4 caracters not the last one...

But this will give different results if the regional setting are set differently...

Is there a way to get it for sure? Independent from the configuration

EDIT: it works on my system with this code:

set YEAR=%DATE:~0,4%

Link to comment
Share on other sites

do you plan on servicing any that are set to US region?

Everywhere else I believe uses the same. GO AMERICANS (US ones anyway)

But that's the only DATE i know of on the system, without using VBS or something to get it directly form the system.

Link to comment
Share on other sites

This Is What This Code Produces

echo off
cls
color F8
mode con: Cols=55 Lines=3
Title What Year Is It
SET YEAR=%date:~10,10%
echo.
set /p = The Year Is %YEAR%

Re-Edit Both Work Here

@echo off
cls
color F2
mode con: Cols=55 Lines=4
Title What Year Is It Test 2
SET YEAR=%date:~-4,10%
echo.
@echo The Year Is %YEAR%
pause

Edited by gunsmokingman
Link to comment
Share on other sites

This is different for every system. It depends on the language and short date settings. %DATE% returns 24-12-04 on my system :) So a batch file isn't the best way to get the year from a system. After a little bit of searching I found that this returns the year in a VBS file:

DatePart("yyyy", Date)

But I don't know if it bothers you to set the title from the vbs file, and do the rest from the batch file, or if you want to pass the year from the vbs file to the batch file, and set it from there, or whatever :)

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...