Page 1 of 1
Batch File First day and last day of current month
Posted: 11 Dec 2014 23:38
by doctork11
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%
Re: Batch File First day and last day of current month
Posted: 11 Dec 2014 23:45
by ALbino
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.
Re: Batch File First day and last day of current month
Posted: 11 Dec 2014 23:47
by foxidrive
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
Re: Batch File First day and last day of current month
Posted: 11 Dec 2014 23:55
by doctork11
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.
Re: Batch File First day and last day of current month
Posted: 12 Dec 2014 01:31
by ALbino
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 :)
Re: Batch File First day and last day of current month
Posted: 12 Dec 2014 03:12
by foxidrive
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.
Re: Batch File First day and last day of current month
Posted: 12 Dec 2014 07:11
by Squashman
And doesn't care about leap years!
Re: Batch File First day and last day of current month
Posted: 12 Dec 2014 08:09
by doctork11
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...
Re: Batch File First day and last day of current month
Posted: 12 Dec 2014 08:50
by Squashman
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
Re: Batch File First day and last day of current month
Posted: 12 Dec 2014 13:41
by ALbino
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.
Hah, good point, hadn't thought of that!
Re: Batch File First day and last day of current month
Posted: 12 Dec 2014 14:56
by aGerman
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