Adding yesterdays date stamp to a file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chef423
Posts: 1
Joined: 24 Aug 2016 14:19

Adding yesterdays date stamp to a file?

#1 Post by chef423 » 24 Aug 2016 14:27

Hello everyone, I am attemping to send a Report from a POS system to a client, daily. The output is great (using AHK) but I need to send this file, every morning at 5am to the owner.

The issue is, the file, called Flash_Report_(yesterdays date) [Example: Flash_Report_8-23-2016.txt] wont attach to sendMail correctly because I need the 'code' to increment the date, on a daily basis. The web {google} is no help, I am always seeing powershell examples.

Here is the sendMail code in a Batch file:
sendEmail -o tls=yes -f someEmail@gmail.com -t chris@chris.com -s smtp.gmail.com:587 -xu someEmail@gmail.com -xp nunya -u "SitesName - Flash Report" -m "Flash Report is attached to this email" -a C:Flash Reports\Flash_Report.txt -vv

The issue is the \Flash_Report.txt part...it needs to say \Flash_Report_[yesterdays date].txt

I have tried everything I know.
Here is the code I use in AutoHotKey to set the date for the Report:
CurrentDate := A_Now
CurrentDate += -1, D
FormatTime, TimeString, %CurrentDate%, M/d/yyyy
Send, %TimeString%

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Adding yesterdays date stamp to a file?

#2 Post by aGerman » 24 Aug 2016 14:53

There is no date/time data type in batch. For that reason it's pure math :wink:
This code respects the leap year rules.

Code: Select all

@echo off &setlocal

for /f %%i in ('wmic os get LocalDateTime /value') do for /f "delims=." %%j in ("%%i") do set "%%j"
call :DaysAdd %LocalDateTime:~,4% %LocalDateTime:~4,2% %LocalDateTime:~6,2% -1 Y M D

echo Flash_Report_%M%-%D%-%Y%.txt

pause
exit /b

:: function-like sub routine
:DaysAdd ByVal_YearIn ByVal_MonthIn ByVal_DayIn ByVal_DeltaIn ByRef_YearOut ByRef_MonthOut ByRef_DayOut
setlocal EnableExtensions
set /a "Year = %~1, Month = 100%~2 %% 100, Day = 100%~3 %% 100"
set /a "a = 14 - Month, a /= 12, b = Year + 4800 - a, c = Month + 12 * a - 3, d = 153 * c + 2"
set /a "d = d / 5 + Day + b * 365 + b / 4 - b / 100 + b / 400 - 1 + %~4"
set /a "e = 4 * d + 3, e /= 146097, f = -e * 146097, f /= 4, f += d"
set /a "g = 4 * f + 3, g /= 1461, h = -1461 * g, h /= 4, h += f, i = 5 * h + 2, i /= 153, Day = 153 * i + 2, Day /= 5"
set /a "Day = -Day + h + 1, Month = -i / 10, Month *= 12, Month += i + 3, Year = e * 100 + g - 4800 + i / 10"
endlocal &set "%~5=%Year%" &set "%~6=%Month%" &set "%~7=%Day%" &exit /b

Steffen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Adding yesterdays date stamp to a file?

#3 Post by Squashman » 24 Aug 2016 15:11

chef423 wrote:The web {google} is no help, I am always seeing powershell examples.

You can use Powershell in a batch file to easily get the date and format you want.
viewtopic.php?t=5051#p29661

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Adding yesterdays date stamp to a file?

#4 Post by foxidrive » 25 Aug 2016 06:57

chef423 wrote:The issue is the \Flash_Report.txt part...it needs to say \Flash_Report_[yesterdays date].txt

I have tried everything I know.
Here is the code I use in AutoHotKey to set the date for the Report:

Code: Select all

CurrentDate := A_Now
CurrentDate += -1, D
FormatTime, TimeString, %CurrentDate%, M/d/yyyy
Send, %TimeString%


If autohotkey returns the right date format with that calculation then you just need to include it in the right place when launching the batch script.


When using this in autohotkey then open Notepad and press control-shift-t

Code: Select all

+^t::
CurrentDate := A_Now
CurrentDate += -1, D
FormatTime, TimeString, %CurrentDate%, M/d/yyyy
Sendinput "%TimeString%"
return


It prints this here "8/24/2016" and today is the 25th August, so it works.

Using this code to launch your script should work with the only change needed in your code shown below in green.

Code: Select all

+^t::
CurrentDate := A_Now
CurrentDate += -1, D
FormatTime, TimeString, %CurrentDate%, M/d/yyyy
run "c:\folder\yourbatchfile.bat", "%TimeString%"
return


sendEmail -o tls=yes -f someEmail@gmail.com -t chris@chris.com -s smtp.gmail.com:587 -xu someEmail@gmail.com -xp nunya -u "SitesName - Flash Report" -m "Flash Report is attached to this email" -a C:Flash Reports\Flash_Report_%~1.txt -vv

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Adding yesterdays date stamp to a file?

#5 Post by foxidrive » 25 Aug 2016 07:29

This gives yesterdays date, using VBS. It can be incorporated into your script easily too.

Code: Select all

@echo off
:: date yesterday (-1) or any number
set day=-1
(
echo s=DateAdd("d",%day%,now^) : d1=Cstr(weekday(s^)^) : d2=WeekdayName(d1,0^)
echo WScript.Echo Cstr(year(s^)^)+" "+Cstr(right(100+month(s^),2^)^)+" "+Cstr(right(100+day(s^),2^)^)+" "+d1+" "+d2
)>"%temp%\%~n0.vbs"

for /f "tokens=1-5" %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "YYYY=%%a" & set "MM=%%b" & set "DD=%%c" & set "daynum=%%d" & set "weekday=%%e"
del "%temp%\%~n0.vbs"
set "newdate=%YYYY%-%MM%-%DD%"
echo Using the day offset of %day% it was "%newdate%" at that time,
echo and day number=%daynum% (%weekday%)
pause

Post Reply