BATCH: Subtract the system date with one day before - batch-file

Is it possible that I set a parameter one day before today?
So I read out the system time:
set ARCHIV=%DATE:~0,2%%DATE:~2,2%%DATE:~4,6%
And now the output is 12.06.2014 (DD.MM.YYYY)
And I want that the output is 11.06.2014
I really don't know if it's possible to subtract the output date with one day.
I had found only commands to subtract it with another date.
Regards

Try below code for delete the file with name of one day less than current date (format YYYYMMDD)
#echo off
setlocal
Call :GetDateTime Year Month Day
set A=%Year%%Month%%Day%
Call :SubtractDate %Year% %Month% %Day% -1 Ret
set b=%Ret%
#echo %b%
#echo ###start Coping
:: variables
Set source=F:\
Set destination=F:\
set backupcmd=xcopy /s /c /d /e /h /i /r /y
echo %drive%
echo %destination%
echo ### Backing up My folder...
%backupcmd% "%source%" "%destination%\%b%"
:SubtractDate Year Month Day <+/-Days> Ret
::Adapted from DosTips Functions::
setlocal & set a=%4
set "yy=%~1"&set "mm=%~2"&set "dd=%~3"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
if %a:~0,1% equ + (set /a JD=%JD%+%a:~1%) else set /a JD=%JD%-%a:~1%
set /a L= %JD%+68569, N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447, K= L-2447*J/80, L= J/11
set /a J= J+2-12*L, I= 100*(N-49)+I+L
set /a YYYY= I, MM=100+J, DD=100+K
set MM=%MM:~-2% & set DD=%DD:~-2%
set ret=%YYYY: =%%MM: =%%DD: =%
endlocal & set %~5=%ret%
exit /b
:GetDateTime Year Month Day Hour Minute Second
#echo off & setlocal
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
( ENDLOCAL
IF "%~1" NEQ "" set "%~1=%YYYY%"
IF "%~2" NEQ "" set "%~2=%MM%"
IF "%~3" NEQ "" set "%~3=%DD%"
IF "%~4" NEQ "" set "%~4=%HH%"
IF "%~5" NEQ "" set "%~5=%Min%"
IF "%~6" NEQ "" set "%~6=%Sec%"
)
exit /b

Related

How to subtract - day from a batch file?

How can I subtract 1 day from this cmd code:
%date:~10,4%%date:~4,2%%date:~7,2%
Every post I see here says I need to use power shell and etc. I just would like to remove 1 day from here.
The cmd returns this:
20180227
And I would like to:
20180226
I don't know about coding but this work for me:
set YY=%date:~10,4%
set MM=%date:~4,2%
set DD=%date:~7,2%
set /a DD1= %DD% - 1
echo %YY%%MM%%DD1%
Here is two code options. I am using functions in both to get the current date and time. If you don't need that you can remove that.
This is the fastest of the two options. It converts the date to a Julian date to do the math, then converts it back to a normal date.
#echo off
setlocal
REM Get the current date
REM Will return variables YY, YYYY, MM, DD, HH, Min and Sec
Call :GetDateTime
REM Add or Subtract from the current date
REM Must use + or - symbol
REM Revised date will be assigned to RetVar
REM LeapDay
Call :AddSubDate 2016 03 01 -1 LeapDay
REM Yesterday
Call :AddSubDate %YYYY% %MM% %DD% -2 past
REM Tomorrow
Call :AddSubDate %YYYY% %MM% %DD% +2 future
echo LeapDay : %LeapDay%
echo Past : %past%
echo Today : %YYYY%%MM%%DD%
echo Future : %future%
pause
GOTO :EOF
:AddSubDate Year Month Day <+/-Days> RetVar
setlocal & set a=%4
set "yy=%~1"&set "mm=%~2"&set "dd=%~3"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
if %a:~0,1% equ + (set /a JD=%JD%+%a:~1%) else set /a JD=%JD%-%a:~1%
set /a L= %JD%+68569, N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447, K= L-2447*J/80, L= J/11
set /a J= J+2-12*L, I= 100*(N-49)+I+L
set /a YYYY= I, MM=100+J, DD=100+K
set MM=%MM:~-2% & set DD=%DD:~-2%
set ret=%YYYY: =%%MM: =%%DD: =%
endlocal & set %~5=%ret%
exit /b
:GetDateTime Year Month Day Hour Minute Second
#echo off & setlocal
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
( ENDLOCAL
set "YY=%YY%"
set "YYYY=%YYYY%"
set "MM=%MM%"
set "DD=%DD%"
set "HH=%HH%"
set "Min=%Min%"
set "Sec=%Sec%"
)
exit /b
The second option uses a trick with the xcopy command to check if the date is a valid date. So it is technically not doing any date math like the previous solution which is converting to the Julian date to do that date math. This option only does subtraction. The first option can add or subtract.
#echo off
setlocal
REM set the number of days to substract
SET DAYS=180
REM Call function to check if the date is valid.
CALL :validdate "%days%" subdate
echo Old date: %subdate%
pause
endlocal
GOTO :EOF
:validdate
setlocal
set "day=%~1"
set rand=%random%
md "%temp%\dummy%rand%\empty%rand%"
REM Get todays date
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
REM set year month and day into its own variables.
set /a y=%dt:~0,4%
set /a m=1%dt:~4,2%
set /a d=1%dt:~6,2%
:loop
if "%day%"=="0" (
rd /s /q "%temp%\dummy%rand%"
endlocal &set "%~2=%y%%m:~-2%%d:~-2%"
GOTO :EOF
)
set /a d-=1
if %d% lss 101 (
set d=131
set /a m-=1
if %m% lss 101 (
set m=112
set /a y-=1
)
)
xcopy /d:%m:~-2%-%d:~-2%-%y% /t "%temp%\dummy%rand%\empty%rand%" "%temp%\dummy%rand%" >nul 2>&1 && (set /a day-=1 & goto loop) || goto loop
GOTO :EOF

Is there any easy way to get 2 days ago date using batch scripting CDM?

I am trying to change the code in order to get two days ago date using the batch file. In this example, I can take today date
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
set LogDateTime=%datetime:~0,8%_%datetime:~8,6%
However, I don't know how to modify the code to get two days ago data. I am not looking for PowerShell, and not any other solution. We have to use only the batch.
You need to convert the date into a Julian Date to do date math in batch files. Here are two function. One to get the current date and then one to do that date math.
#echo off
setlocal
REM Get the current date
REM Will return variables YY, YYYY, MM, DD, HH, Min and Sec
Call :GetDateTime
REM Add or Subtract from the current date
REM Must use + or - symbol
REM Revised date will be assigned to RetVar
Call :AddSubDate %YYYY% %MM% %DD% -2 past
echo Past : %past%
pause
GOTO :EOF
:AddSubDate Year Month Day <+/-Days> RetVar
setlocal & set a=%4
set "yy=%~1"&set "mm=%~2"&set "dd=%~3"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
if %a:~0,1% equ + (set /a JD=%JD%+%a:~1%) else set /a JD=%JD%-%a:~1%
set /a L= %JD%+68569, N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447, K= L-2447*J/80, L= J/11
set /a J= J+2-12*L, I= 100*(N-49)+I+L
set /a YYYY= I, MM=100+J, DD=100+K
set MM=%MM:~-2% & set DD=%DD:~-2%
set ret=%YYYY: =%%MM: =%%DD: =%
endlocal & set %~5=%ret%
exit /b
:GetDateTime Year Month Day Hour Minute Second
#echo off & setlocal
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
( ENDLOCAL
set "YY=%YY%"
set "YYYY=%YYYY%"
set "MM=%MM%"
set "DD=%DD%"
set "HH=%HH%"
set "Min=%Min%"
set "Sec=%Sec%"
)
exit /b

Batch File - getting yesterdays date and applying in current batch file

First off i am very new to batch files but here is what i need to do.
i need to change IE's Homepage every day, but i need to get yesterdays date and apply it to the web address. example...
this is what i have currently.
#echo off
REG DELETE "HKCU\Software\Microsoft\Internet Explorer\Main" /v "Start Page" /f
REG ADD "HKCU\Software\Microsoft\Internet Explorer\Main" /v "Start Page" /d "http://ibapps.ibank.local/Reports/address-discrepancy-report-02.17.2015.html" /f
What needs to happen is to get yesterdays date and put it in where the date is on the web address.. ie 02.17.2015
any help would be much appreciated..
Try this:
#echo off
setlocal
Call :GetDateTime Year Month Day
Echo %Year% %Month% %Day%
Call :SubtractDate %Year% %Month% %Day% -1 Ret
echo %Ret%
REG DELETE "HKCU\Software\Microsoft\Internet Explorer\Main" /v "Start Page" /f
REG ADD "HKCU\Software\Microsoft\Internet Explorer\Main" /v "Start Page" /d "http://ibapps.ibank.local/Reports/address-discrepancy-report-%ret%.html" /f
exit /b
:SubtractDate Year Month Day <+/-Days> Ret
::Adapted from DosTips Functions::
setlocal & set a=%4
set "yy=%~1"&set "mm=%~2"&set "dd=%~3"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
if %a:~0,1% equ + (set /a JD=%JD%+%a:~1%) else set /a JD=%JD%-%a:~1%
set /a L= %JD%+68569, N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447, K= L-2447*J/80, L= J/11
set /a J= J+2-12*L, I= 100*(N-49)+I+L
set /a YYYY= I, MM=100+J, DD=100+K
set MM=%MM:~-2% & set DD=%DD:~-2%
set ret=%MM: =%.%DD: =%.%YYYY: =%
endlocal & set %~5=%ret%
exit /b
:GetDateTime Year Month Day Hour Minute Second
#echo off & setlocal
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
( ENDLOCAL
IF "%~1" NEQ "" set "%~1=%YYYY%"
IF "%~2" NEQ "" set "%~2=%MM%"
IF "%~3" NEQ "" set "%~3=%DD%"
IF "%~4" NEQ "" set "%~4=%HH%"
IF "%~5" NEQ "" set "%~5=%Min%"
IF "%~6" NEQ "" set "%~6=%Sec%"
)
exit /b
This is a .bat/jscript hybrid and should be saved with .bat extension.
#if (#X)==(#Y) #end /* JScript comment
#echo off
rem :: the first argument is the script name as it will be used for proper help message
for /f %%$ in ('cscript //E:JScript //nologo "%~f0"') do set "yesterday=%%$"
REG DELETE "HKCU\Software\Microsoft\Internet Explorer\Main" /v "Start Page" /f
REG ADD "HKCU\Software\Microsoft\Internet Explorer\Main" /v "Start Page" /d "http://ibapps.ibank.local/Reports/address-discrepancy-report-%yesterday%.html" /f
exit /b %errorlevel%
#if (#X)==(#Y) #end JScript comment */
$today = new Date();
$yesterday = new Date($today);
$yesterday.setDate($today.getDate() - 1);
var $dd = $yesterday.getDate();
var $mm = $yesterday.getMonth()+1; //January is 0!
var $yyyy = $yesterday.getFullYear();
if($dd<10){$dd='0'+dd} if($mm<10){$mm='0'+$mm} $yesterday = $dd+'.'+$mm+'.'+$yyyy;
WScript.Echo($yesterday);

Batch script to delete file older than 1 day using creation date

Batch script to delete file older than 1 day.
D means: Run date am passing through command prompt.
How to subtract one day in D.
I want to delete one day old file,the file name starting with olddayfile_20140323.txt in same code.
Am using below code for Deleting D (D:current day file) files.
#echo on
rem %1 %2 %3 - YYYY MM DD
set folder1="C:\FX\in"
set folder2="C:\FX\out"
set D=%1%2%3%
cd /d %folder2%
del test1_%1%2%3*
del test2_%1%2%3*
del test3_%1%2%3*
Please note below script is working,but it's working only modified date files.
forfiles.exe /p C:\FX\out /s /m olddayfile_* /d -1 /c "cmd /c del #file"
Please suggest me am new for Batch script.
Try below code for delete the file with name of one day less than current date (format YYYYMMDD)
#echo off
setlocal
Call :GetDateTime Year Month Day
set A=%Year%%Month%%Day%
Call :SubtractDate %Year% %Month% %Day% -1 Ret
set b=%Ret%
#echo ###start Deleting
:: variables
Set source=G:\
echo ### Deleting up old backup...
DEL /Q /S "%source%\%B%*.*"
:SubtractDate Year Month Day <+/-Days> Ret
::Adapted from DosTips Functions::
setlocal & set a=%4
set "yy=%~1"&set "mm=%~2"&set "dd=%~3"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
if %a:~0,1% equ + (set /a JD=%JD%+%a:~1%) else set /a JD=%JD%-%a:~1%
set /a L= %JD%+68569, N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447, K= L-2447*J/80, L= J/11
set /a J= J+2-12*L, I= 100*(N-49)+I+L
set /a YYYY= I, MM=100+J, DD=100+K
set MM=%MM:~-2% & set DD=%DD:~-2%
set ret=%YYYY: =%%MM: =%%DD: =%
endlocal & set %~5=%ret%
exit /b
:GetDateTime Year Month Day Hour Minute Second
#echo off & setlocal
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
( ENDLOCAL
IF "%~1" NEQ "" set "%~1=%YYYY%"
IF "%~2" NEQ "" set "%~2=%MM%"
IF "%~3" NEQ "" set "%~3=%DD%"
IF "%~4" NEQ "" set "%~4=%HH%"
IF "%~5" NEQ "" set "%~5=%Min%"
IF "%~6" NEQ "" set "%~6=%Sec%"
)
exit /b

Set Yesterday BATCH FILE

If this gives me today's date...
SET TODAY=%date:~7,2%.%date:~-10,2%.%date:~-4,4%
...how can i get yesterday's date? Can I get it in the same style?
Thanks for your help!
Change the order of the three variables in the 3rd last line to suit you:
:: yesterdays date
#echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "date-yesterday=%yyyy%-%mm%-%dd%"
echo Yesterday was "%date-yesterday%"
pause
Here is a script I wrote to do this:
#echo off
setlocal
Call :GetDateTime Year Month Day
Call :SubtractDate %Year% %Month% %Day% -1 Ret
echo %Ret%
pause
:SubtractDate Year Month Day <+/-Days> Ret
::Adapted from DosTips Functions::
setlocal & set a=%4
set "yy=%~1"&set "mm=%~2"&set "dd=%~3"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
if %a:~0,1% equ + (set /a JD=%JD%+%a:~1%) else set /a JD=%JD%-%a:~1%
set /a L= %JD%+68569, N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447, K= L-2447*J/80, L= J/11
set /a J= J+2-12*L, I= 100*(N-49)+I+L
set /a YYYY= I, MM=100+J, DD=100+K
set MM=%MM:~-2% & set DD=%DD:~-2%
set ret=%DD: =%.%MM: =%.%YYYY: =%
endlocal & set %~5=%ret%
exit /b
:GetDateTime Year Month Day Hour Minute Second
#echo off & setlocal
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
( ENDLOCAL
IF "%~1" NEQ "" set "%~1=%YYYY%"
IF "%~2" NEQ "" set "%~2=%MM%"
IF "%~3" NEQ "" set "%~3=%DD%"
IF "%~4" NEQ "" set "%~4=%HH%"
IF "%~5" NEQ "" set "%~5=%Min%"
IF "%~6" NEQ "" set "%~6=%Sec%"
)
exit /b

Resources