Batch File First day and last day of current month

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
doctork11
Posts: 6
Joined: 17 Nov 2014 12:51

Batch File First day and last day of current month

#1 Post by doctork11 » 11 Dec 2014 23:38

I need a batch file command that adds to the file name the first and last day of the month. For example, filename_2014-12-01:2014-12-31.txt

I've tried all kinds of things with no luck.

Here's something that I tried, but I can't get the last day of current month (FYI - i was playing around)

Code: Select all

@echo off
REM Use VBScript to get date.  This will get the first day of this month and take one off to get last day of last month
(echo eolm=dateserial^(year^(date^),month^(date^),1^)-0
echo wscript.echo year^(eolm^) ^& right^(100 + month^(eolm^),2^) ^& right^(100+day^(eolm^),2^)) > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yyyymmdd=%%a


echo First day of this month is %yyyymmdd%

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Batch File First day and last day of current month

#2 Post by ALbino » 11 Dec 2014 23:45

Since the first and last day of the month are always the same, why not just hardcode them? You don't need to perform a calculation to know that the first and last day of December are the 1st and 31st.

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

Re: Batch File First day and last day of current month

#3 Post by foxidrive » 11 Dec 2014 23:47

doctork11 wrote:I need a batch file command that adds to the file name the first and last day of the month. For example, filename_2014-12-01:2014-12-31.txt


What you have shown above uses an illegal character in the filename, which makes me think that the task could be a little different
than what you have described - because you're not all that sure of what the task should be.

See here: viewtopic.php?f=3&t=6108

doctork11
Posts: 6
Joined: 17 Nov 2014 12:51

Re: Batch File First day and last day of current month

#4 Post by doctork11 » 11 Dec 2014 23:55

The batch file runs every month, so I dont really want to hard code a bunch of possibilities. I want to run the job the execute the batch file that makes the output file that is named filename_2014-12-01:2014-12-31.txt or something similar (like filename_20141201_20141231.txt)

Not all months end in 31, so I guess I could set the month to start, but I still need to know that it's December, so make it 20141201.

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Batch File First day and last day of current month

#5 Post by ALbino » 12 Dec 2014 01:31

Since there's only 12 possibilities, it's really not that big of a deal:

Code: Select all

@echo off

set FirstDay=01

set Month=%date:~4,2%

set Year=%date:~10,4%

if %Month%==01 set LastDay=31 & goto foundate
if %Month%==02 set LastDay=28 & goto foundate
if %Month%==03 set LastDay=31 & goto foundate
if %Month%==04 set LastDay=30 & goto foundate
if %Month%==05 set LastDay=31 & goto foundate
if %Month%==06 set LastDay=30 & goto foundate
if %Month%==07 set LastDay=31 & goto foundate
if %Month%==08 set LastDay=31 & goto foundate
if %Month%==09 set LastDay=30 & goto foundate
if %Month%==10 set LastDay=31 & goto foundate
if %Month%==11 set LastDay=30 & goto foundate
if %Month%==12 set LastDay=31 & goto foundate


:foundate
echo The year is: %Year%
echo The month is: %Month%
echo First day of this month is: %FirstDay%
echo Last day of this month is: %LastDay%


It's inelegant, but it works :)

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

Re: Batch File First day and last day of current month

#6 Post by foxidrive » 12 Dec 2014 03:12

ALbino wrote:Since there's only 12 possibilities, it's really not that big of a deal:

It's inelegant, but it works :)


Yep. As long as the OP is in the same region of the world as you, or uses the same date format. :D

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

Re: Batch File First day and last day of current month

#7 Post by Squashman » 12 Dec 2014 07:11

And doesn't care about leap years!

doctork11
Posts: 6
Joined: 17 Nov 2014 12:51

Re: Batch File First day and last day of current month

#8 Post by doctork11 » 12 Dec 2014 08:09

:lol:
Given the time constraints I have to convert all these files to this method, this will work PERFECTLY for this request. Thank you everyone for the help and P.S. I dont care about Leap Year today... :?

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

Re: Batch File First day and last day of current month

#9 Post by Squashman » 12 Dec 2014 08:50

Did a little Google Search Myself. Some vbscript code was posted in this thread.
It was actually the first link Google found when I searched for: vbscript get last day of month
http://p2p.wrox.com/access-vba/102-find ... month.html

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Batch File First day and last day of current month

#10 Post by ALbino » 12 Dec 2014 13:41

foxidrive wrote:
ALbino wrote:Since there's only 12 possibilities, it's really not that big of a deal:

It's inelegant, but it works :)


Yep. As long as the OP is in the same region of the world as you, or uses the same date format. :D


Hah, good point, hadn't thought of that! :D

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

Re: Batch File First day and last day of current month

#11 Post by aGerman » 12 Dec 2014 14:56

Calculating the number of days is a simple task in Batch. There is a very short algorithm (n = 30 + ...) that you can use in many programming languages:

Code: Select all

@echo off &setlocal

for /f "skip=1 delims=." %%d in ('wmic os get LocalDateTime ^| findstr .') do set "timestamp=%%d"
echo %timestamp:~0,4% %timestamp:~4,2%

call :DaysOfMonth %timestamp:~0,4% %timestamp:~4,2%
echo Days Of Month: %errorlevel%

pause
goto :eof


:DaysOfMonth Year Month
setlocal DisableDelayedExpansion
set /a "yy = %~1, mm = 100%~2 %% 100"
set /a "n = 30 + !(((mm & 9) + 6) %% 7) + !(mm ^ 2) * (!(yy %% 4) - !(yy %% 100) + !(yy %% 400) - 2)"
endlocal &exit /b %n%

Regards
aGerman

Post Reply