Link to home
Start Free TrialLog in
Avatar of Jacob L
Jacob LFlag for United States of America

asked on

subtracting days from a date in a batch file.

I am looking for best solution to subtract a date from a timestamp in my batch file. I have read calling a vbscript to make the calculation is a better option. Any help would be appreciated.

below is part of my script.

rem Setting the Start Date & Time that the batch was launched
===============================================================
set yyyy=%date:~10,4%
set mm=%date:~4,2%
set dd=%date:~7,2%
set hh=%time:~0,2%
set mn=%time:~3,2%
set ss=%time:~6,2%

set yyyy=%date:~10,4%
set mm=%date:~4,2%
set dd=%date:~7,2%
for /f "tokens=1-2 delims=: " %%a in ('time /t') do (set hh=%%a& set mn=%%b)
set ss=%time:~6,2%
set dstamp="%yyyy%"-"%mm%"-"%dd%"_"%hh%%mn%%ss%"
set dstamp2="%yyyy%"."%mm%"."%dd%"
set dstamp3 = dstamp2 - 6 days                  REM<---------------------------------------------------------this is the parameter i need changed
===============================================================

ren  "TRD_Cost.PSV"  "TRD_Cost_%dstamp3%_%dstamp2%.PSV"     REM<----------------------------------------------------renamed here
rem zip -j %exepath%\..\HostTran\Export\Dunnhumby\TRD_Cost_%dstamp2%.PSV.zip  %exepath%\..\HostTran\Export\Dunnhumby\TRD_Cost*.PSV >> %log%
rem move %exepath%\..\HostTran\Export\Dunnhumby\TRD_Cost_%dstamp2%.PSV.zip   %exepath%\..\HostTran\Export\Dunnhumby\backup\ >> %log%
rem del %exepath%\..\HostTran\Export\Dunnhumby\TRD_Cost*.PSV >> %log%

REM so this is what i want to rename the file: TRD_Cost_dstamp3 _dstamp2.psv where dstamp3 = dstamp2 - 7 days.

Open in new window

Avatar of NVIT
NVIT
Flag of United States of America image

Here is DATEADD.BAT, which I've used to add or subtract days.
Avatar of Jacob L

ASKER

NVIT I appreciate that but that seems like an aweful lot of code for something as simple as a date subtraction. I was hoping to maybe just a call a vbscript if that would work.
Avatar of Bill Prew
Bill Prew

Sure.  I'd like to tidy the whole script up a bit though.  So what do you need for the whole script.  Just two dates in yyyy.mm.dd format, one for
today and one for 6 days before today?

It doesn't look like you ever use the time variable %dstamp% so is that not needed?  Since we are going to leverage vbscript we can remove many lines of code...


»bp
Avatar of Jacob L

ASKER

Hi Bill,
Yes that is correct. I actually cut out a lot of the code to only focus on what is needed here. dstamp is used for logging but code not included so you can remove it. And yes the end results is to rename a file name will be for example  TRD_Cost_2017.07.21_2017.07.28.PSV
Powershell to the rescue..
save as filename.ps1
remove the -whatif in production
<#rem Setting the Start Date & Time that the batch was launched
===============================================================
set yyyy=%date:~10,4%
set mm=%date:~4,2%
set dd=%date:~7,2%
set hh=%time:~0,2%
set mn=%time:~3,2%
set ss=%time:~6,2%

set yyyy=%date:~10,4%
set mm=%date:~4,2%
set dd=%date:~7,2%
for /f "tokens=1-2 delims=: " %%a in ('time /t') do (set hh=%%a& set mn=%%b)
set ss=%time:~6,2%
set dstamp="%yyyy%"-"%mm%"-"%dd%"_"%hh%%mn%%ss%"
set dstamp2="%yyyy%"."%mm%"."%dd%"
set dstamp3 = dstamp2 - 6 days                  REM<---------------------------------------------------------this is the parameter i need changed
===============================================================

ren  "TRD_Cost.PSV"  "TRD_Cost_%dstamp3%_%dstamp2%.PSV"     REM<----------------------------------------------------renamed here
rem zip -j %exepath%\..\HostTran\Export\Dunnhumby\TRD_Cost_%dstamp2%.PSV.zip  %exepath%\..\HostTran\Export\Dunnhumby\TRD_Cost*.PSV >> %log%
rem move %exepath%\..\HostTran\Export\Dunnhumby\TRD_Cost_%dstamp2%.PSV.zip   %exepath%\..\HostTran\Export\Dunnhumby\backup\ >> %log%
rem del %exepath%\..\HostTran\Export\Dunnhumby\TRD_Cost*.PSV >> %log%

REM so this is what i want to rename the file: TRD_Cost_dstamp3 _dstamp2.psv where dstamp3 = dstamp2 - 7 days.
#>
$date = get-date.adddays(-6) 
$strdate = $date.tostring("yyyymmdd")
zip -j %exepath%\..\HostTran\Export\Dunnhumby\TRD_Cost_$strdate.PSV.zip  %exepath%\..\HostTran\Export\Dunnhumby\TRD_Cost*.PSV >> %log%
move-item %exepath%\..\HostTran\Export\Dunnhumby\TRD_Cost_$strdate.PSV.zip   %exepath%\..\HostTran\Export\Dunnhumby\backup\ >> %log% -WhatIf
remove-item %exepath%\..\HostTran\Export\Dunnhumby\TRD_Cost*.PSV >> %log% -WhatIf

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

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
Avatar of Jacob L

ASKER

Hey Bill, sorry for late reply. i have been on vacation.
So this is what i am trying:

@echo on
setlocal

rem Calculate todays date, and 6 days ago, in YYYY.MM.DD format
call :Eval today "Year(Date)&'.'&Right('0'&Month(Date),2)&'.'&Right('0'&Day(Date),2)"
call :Eval today-6 "Year(Date-6)&'.'&Right('0'&Month(Date-6),2)&'.'&Right('0'&Day(Date-6),2)"


rem cd %exepath%\..\HostTran\Export\Dunnhumby\
ren "..\HostTran\Export\Dunnhumby\TRD_Cost.PSV" "..\HostTran\Export\Dunnhumby\TRD_Cost_%today-6%_%today%.PSV"    

exit /b

:Eval [variable] [expression]
  if not exist "%TEMP%\_eval.vbs"   echo Wscript.Echo Eval(Replace(WScript.Arguments.Item(0),Chr(39),Chr(34)))>"%TEMP%\_eval.vbs"
  for /F "tokens=*" %%A in ('cscript //nologo %TEMP%\_eval.vbs "%~2"') do set "%~1=%%A"
  exit /b


and i am getting "the syntax of the command is incorrect"
Avatar of Jacob L

ASKER

ok so i got it to run but it made the file name TRD_Cost_%today-6%_%today instead of the actual date

@echo off
setlocal

rem Calculate todays date, and 6 days ago, in YYYY.MM.DD format
call :Eval today "Year(Date)&'.'&Right('0'&Month(Date),2)&'.'&Right('0'&Day(Date),2)"
call :Eval today-6 "Year(Date-6)&'.'&Right('0'&Month(Date-6),2)&'.'&Right('0'&Day(Date-6),2)"

ren "..\HostTran\Export\Dunnhumby\TRD_Cost.psv" "TRD_Cost_%today-6%_%today%.psv"    

exit /b

:Eval [variable] [expression]
  if not exist "%TEMP%\_eval.vbs"   echo Wscript.Echo Eval(Replace(WScript.Arguments.Item(0),Chr(39),Chr(34)))>"%TEMP%\_eval.vbs"
  for /F "tokens=*" %%A in ('cscript //nologo %TEMP%\_eval.vbs "%~2"') do set "%~1=%%A"
  exit /b
Don't understand that, I ran the code you posted last, and just put an ECHO in front of the REN statement, and got the following output, seems to be working.

ren "..\HostTran\Export\Dunnhumby\TRD_Cost.psv" "TRD_Cost_2017.08.02_2017.08.08.psv"

Open in new window


»bp
Avatar of Jacob L

ASKER

Weird, it works for me now as well. Awesome. Thank you! Quick question then. The bottom part of the script...that's just creating a vbs file in a  temporary folder? Any specific reason you chose that location?

:Eval [variable] [expression]
  if not exist "%TEMP%\_eval.vbs"   echo Wscript.Echo Eval(Replace(WScript.Arguments.Item(0),Chr(39),Chr(34)))>"%TEMP%\_eval.vbs"
  for /F "tokens=*" %%A in ('cscript //nologo %TEMP%\_eval.vbs "%~2"') do set "%~1=%%A"
  exit /b
Avatar of Jacob L

ASKER

Thank you. While the other solutions probably would have worked as well, this is exactly what I was asking for.
Any specific reason you chose that location?

The environment variable %TEMP% should reference a temporary folder designated by Windows.  This is often a good place to use as a temp folder for these type of transient files.

Another approach you can use is to actually create small VBS helper scripts like this in a permanent folder, and then reference them when needed from BAT scripts.  Saves the effort of building them each time the BAT script executes, but means they have to exist before the script runs.  Using the approach in this question the BAT script is self contained, it creates the small VBS file it needs on the fly.


»bp