Previous Month in YYYYMM Format

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rpatel
Posts: 3
Joined: 19 Aug 2015 06:51

Previous Month in YYYYMM Format

#1 Post by rpatel » 19 Aug 2015 07:14

I need help getting previous month and year in the following format YYYYMM. For example, the file would show 201507 if it runs in August 2015 or 201508 if it runs in September 2015. Using other threads I've got the as following but isn't working properly.

Code: Select all

@ECHO Off
set ff=%date:~10%
set aa=%date:~3,5%
if %date:~3,5%==01 set /a ff=%date:~6%-1
set /a "bb=1%aa%-101"
if %bb%==1 set bb=01
if %bb%==2 set bb=02
if %bb%==3 set bb=03
if %bb%==4 set bb=04
if %bb%==5 set bb=05
if %bb%==6 set bb=06
if %bb%==7 set bb=07
if %bb%==8 set bb=08
if %bb%==9 set bb=09
if %bb%==0 set bb=12
echo %ff%%bb%
Call "OriginalPath1.bat" LIVE YYYYMM
Call "OriginalPath.bat"
Echo The update completed successfully!
exit


Thank you for your help in advance.

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

Re: Previous Month in YYYYMM Format

#2 Post by foxidrive » 19 Aug 2015 08:23

This should return the last month, and year if it is run in January.
You'll have to test it.

Code: Select all

@echo off
set day=0
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s)-1,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%"
if "%MM%"=="00" set "MM=12" & set /a YYYY=YYYY-1
set "data=%yyyy%%mm%"

echo last year/month was "%data%"
pause

rpatel
Posts: 3
Joined: 19 Aug 2015 06:51

Re: Previous Month in YYYYMM Format

#3 Post by rpatel » 19 Aug 2015 08:43

I just ran the code and worked. Thank you.

Is there a guide or something that brakes down the date syntax?

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Previous Month in YYYYMM Format

#4 Post by Compo » 19 Aug 2015 13:31

A WMIc based version

Code: Select all

@Echo Off & SetLocal
For /F %%A In ('WMIc OS Get LocalDateTime') Do For %%B In (%%A) Do Set _=%%B
Set "YYYY=%_:~,4%" & Set "MM=%_:~4,2%"
If %MM% Equ 01 (Set/A "YYYY-=1" & Set "MM=12") Else (Set/A "MM=1%MM%-1"
   Call Set "MM=%%MM:~-2%%")
Echo=%YYYY%%MM% & Pause
Last edited by Compo on 21 Aug 2015 09:26, edited 1 time in total.

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

Re: Previous Month in YYYYMM Format

#5 Post by foxidrive » 20 Aug 2015 03:24

Compo wrote:A WMIc based version


Nicely brief compo, but you'll have to subtract the month too. It should show 201507 for this month.

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

Re: Previous Month in YYYYMM Format

#6 Post by foxidrive » 20 Aug 2015 03:27

rpatel wrote:Is there a guide or something that brakes down the date syntax?



It's more complex than it needs to be because I modified existing code that I already had here.
Compo may provide you with easier to follow code using Wmic as long as you can run it with your user permissions (I think it requires admin).

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Previous Month in YYYYMM Format

#7 Post by dbenham » 20 Aug 2015 05:26

Here is a corrected WMIC version:

Code: Select all

@echo off
setlocal
for /f %%A In ('wmic os get localDateTime') do for %%B in (%%A) do set ts=%%B
set /a "YYYY=%ts:~,4%, MM=1%ts:~4,2%"
if %MM% Equ 101 (set /a "YYYY-=1, MM=112") else set /a "MM-=1"
set "prevMonth=%YYYY%%MM:~1%"
echo %prevMonth%


Or you could use getTimestamp.bat:

Code: Select all

@echo off
call getTimestamp /om -1 /f "{yyyy}{mm}" /r prevMonth
echo %prevMonth%


Both solutions are locale agnostic, meaning they will work on any Windows machine in the world, regardless what date settings are used by the locale.


Dave Benham

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

Re: Previous Month in YYYYMM Format

#8 Post by foxidrive » 20 Aug 2015 07:40

It's great when others chime in with different solutions, coz it spurs me on to greater effort too.

I noticed that my previous code has a flaw and uses the wrong variable for change of year - dd instead of mm.
I've edited that to fix it.

Here's the same style of solution I presented above but wrapped a little differently, and 'simplified'

Code: Select all

@echo off
(
echo s=DateAdd("d",0,now^)
echo WScript.Echo Cstr(year(s^)^)+" "+Cstr(right(100+month(s^)-1,2^)^)
)>"%temp%\%~n0.vbs"

for /f "tokens=1,2" %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "YYYY=%%a" & set "MM=%%b"
del "%temp%\%~n0.vbs"
if "%MM%"=="00" set "MM=12" & set /a YYYY=YYYY-1
set "data=%YYYY%%MM%"
echo last year/month was "%data%"
pause


@ rpatel - To get descriptions of how the code works, ask about the solution you choose to use and be specific about which sections you'd like explained - so it is easier to tell you what you need to know.

rpatel
Posts: 3
Joined: 19 Aug 2015 06:51

Re: Previous Month in YYYYMM Format

#9 Post by rpatel » 20 Aug 2015 11:19

Thank you everyone for jumping in and providing multiple solutions. I got @Compo's code to work and complete my task.

@foxidrive - To be honest I have no clue other than just the basic of batch files. :D

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Previous Month in YYYYMM Format

#10 Post by Compo » 21 Aug 2015 09:32

I have fixed my code in the original post to include what I was supposed to include, (and thought I had).

Unfortunately I was using a touch screen unit and ended up overwriting/deleting more lines by accident than I'd ever care to admit to!

PS. To the best of my knowledge there is no need for admin privileges to run this code.

Post Reply