Link to home
Start Free TrialLog in
Avatar of Samooramad
Samooramad

asked on

Windows Batch script Date format

Hi experts,
I have a Windows script that is setting the name of a file with extension of date format as yyyymmdd.

It is working but I am wondering how I could modify it to make the date that of yesterday's instead of todays. Here is part of the script


set today=%date:~-4%%date:~4,2%%date:~7,2%
set spt_file_name=SPT.%today%

The command above works to make the file name for example SPT.20071030
I would like it to be SPT.20071029

Avatar of Samooramad
Samooramad

ASKER

Also it would be helpful if you could explain to me how this line works exactly
set today=%date:~-4%%date:~4,2%%date:~7,2%

as I just copied this from another batch file and don't completly understand how the day month and year parts are being formatted

Thanks
You cannot subtract from variable %date%. It is not date variable, it is a string variable. Windows batch processor does not provide date manipulation features. For that you would have to use some other tool, for example Visual Basic Script.
SOLUTION
Avatar of ubig
ubig
Flag of Lithuania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Aha, since I cannot do this with Batch should I wait for someone to show me how to do this in VBscript or open a new question

At least now I understand how it is used

Thanks
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:)
Sorry for the stupid question, but is that a batch file? it looks like one but I'm a beginner so I'm not sure.

Do you mean I should replace my 2 lines with all that? :-)

then add the rest of my code after it? I am FTPing some files after formatting the file names
Here is VBS code for you:

Yesterday=DateAdd("d", -1, Now ()) ' Adds minus 1 day (parameter "d") to current date and time
spt_file_name="SPT." & Year(Yesterday) & Month(Yesterday) & Day(Yesterday)
It is a batch file. The main portion is here:

@echo off

setlocal

call :GETDATEPARTS "%date%"
call :SUBTRACTDAYS 1

set spt_file_name=SPT.%yy%%mm%%dd%

echo spt_file_name=%spt_file_name%

goto :EOF

You would place whatever code you have here:

@echo off

setlocal

call :GETDATEPARTS "%date%"
call :SUBTRACTDAYS 1

set spt_file_name=SPT.%yy%%mm%%dd%

REM ** Place you code in here
REM ** Ending here

goto :EOF
SteveGTR:

That worked beautifully, Thank you