Anyone good with .bat files?

Ed in Aus

Well-known Member
Joined
Jul 24, 2007
Messages
829
Hi

Tried posting on another forum but looks like no one is home...

Batch 1
What I am trying to do is store the current date in a temp file (that can be called in another batch later) and wind the current system time back 10 years. i.e. Date = currentdate - 365 * 10 or something like that anyway...

Batch 2
Use the temp file created previously to restore the current date...
Delete temp file

any one know what I need to do, fairly new to bat files...

Using Vista so UAC is turned off for this.

Date format DD/MM/YYYY (Australian)
and time is not in 24 hours time

Have been researching but can't even figure out where to start
 

Some videos you may like

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Ed in Aus

Well-known Member
Joined
Jul 24, 2007
Messages
829
Not sure if anyone can help with this just hoping someone know's where to send me or what i need to do to make this work...
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,619
Does it have to be .bat files?

If not, you could use JScript of VBScript. For example, here's a .vbs file I use to change the system date by a specified number of days:

Code:
'ChangeDateByDays.vbs
'
'Changes the current system date by the number of days specified in the argument.  The argument is required and
'can be a negative number to subtract days from the date, or a positive number to add days to the date.
'Note: the Windows Time service must be stopped (net stop w32time) for the new system date to take effect. 

Set arg = WScript.Arguments
if arg.count > 0 then
  newDate = DateAdd("d",arg(0),Date())
  'wscript.echo newDate
  CreateObject("Wscript.Shell").Run "%comspec% /c date " & newDate,0,True
end if
As you can see it just uses the DOS date command to change the date. To add or subtract years instead, change the line to newDate = DateAdd("yyyy",arg(0),Date()).

I call this .vbs from my .bat using:

net stop w32time
cscript //NoLogo ChangeDateByDays.vbs -70

This is on Windows XP, so the Windows Time Service might be different for Vista.

I'm sure it's possible to change the system date back 10 years using pure DOS, but for me it was easier to use VBScript to calculate the new date. VBScript can also create/read/delete the temporary file containing the current date.
 

Ed in Aus

Well-known Member
Joined
Jul 24, 2007
Messages
829
Ok think I am following but need to double check...

create bat file that basically calls this

i.e.

net stop w32time
cscript //NoLogo ChangeDateByDays.vbs -10

Code:
'script 1
Set arg = WScript.Arguments
If arg.Count > 0 Then
  newDate = DateAdd("YYYY", arg(0), Date)
  'wscript.echo newDate
  CreateObject("Wscript.Shell").Run "%comspec% /c date " & newDate, 0, True
End If

then another one to reset the date back to original year.

net stop w32time
cscript //NoLogo ChangeDateByDays.vbs 10

I am still getting my head around some of the bat file stuff with stopping the w32time, and also where I need to have the vbs file stored can you give me a little bit more of an idea on where I should have it stored... for example c:\users\temp

also what does this part mean "cscript //NoLogo ChangeDateByDays.vbs -70"

I get the last part setting how the arg is set but not the first part

Do I need to restart the time service?
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,619

ADVERTISEMENT

I've played around and here is .bat file which changes the year back by 10 years.
Code:
@ECHO OFF
FOR /F "TOKENS=1,2 DELIMS=/ " %%A IN ('DATE /T') DO SET dd=%%A
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('DATE /T') DO SET mm=%%A
FOR /F "TOKENS=3* DELIMS=/ " %%A IN ('DATE /T') DO SET yyyy=%%A
SET /A yyyy=%yyyy%-10
SET NewDate=%dd%/%mm%/%yyyy%
REM NET STOP w32time
ECHO Current date is:
DATE /T
DATE %NewDate%
ECHO New date is:
DATE /T
PAUSE
This works with dd/mm/yyyy DOS date format. I'm not sure if it really is necessary stop w32time (it's commented out above) - you'll have to see if Windows automatically resets the date if w32time is still running and if so, use the net stop w32time to prevent it.

The .vbs can be called simply by:

ChangeDateByDays.vbs -10

The cscript //NoLogo at the start simply suppresses the displaying of the version and copyright information. Type cscript /? for more details.

The .vbs file can be stored anywhere, for example c:\users\temp. In which case call it using:

cscript //NoLogo c:\users\temp\ChangeDateByDays.vbs -10
 
Last edited:

Ed in Aus

Well-known Member
Joined
Jul 24, 2007
Messages
829

ADVERTISEMENT

Hi John,

Trying to get it to work but it is saying that "the system cannot accept the date entered"

then the next line says "Enter the new date: (dd-mm-yy) "

any ideas trying to play around with it now, I can manually add the date in the format as above. i.e. 30/03/99

and the date does its thing.
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,619
Try this, based on ideas at http://www.tech-recipes.com/rx/956/windows-batch-file-bat-to-get-current-date-in-mmddyyyy-format/

It assumes that the date displayed by

echo %date%

is in the format dd/mm/yyyy. If not, adjust the offset, length arguments as required (e.g. 0,2).
Code:
@echo off
setlocal
set dd=%date:~0,2%
set mm=%date:~3,2%
set yyyy=%date:~6,4%
set /a yyyy=%yyyy%-10
set newdate=%dd%/%mm%/%yyyy%
echo Current date is:
date /t
date %newdate%
echo New date is: 
date /t
Re stopping the Windows Time service, w32time. On my system (XP Pro), w32time synchronises the system clock with internet time every 7 days (to see this, open the clock and click Internet Time tab). So, if the date has been changed as above and w32time has not been stopped then Windows will correct the date-time at the scheduled synchronisation time, which could cause a problem for your batch files if you are running them at the time the synchronisation occurs. Therefore, it's safer to stop w32time, change the date back 10 years, do your other stuff, change date to the correct date and restart w32time. As I say though, it might be different for Vista.
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Why do you need a batch file to do this?
This can be done in VBA.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,865
Messages
5,598,539
Members
414,246
Latest member
allyciv

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top