Manipulate MS DOS Dates

August 4, 2014 at 18:20:16
Specs: Windows Vista
I cannot get this code to give the right output. It is suppose to, based upon the day of the month use either the past month name or current month name for a file. If its the first three days of the new month, the previous month name will be used. If the date is day 4 or greater it should use the current month name.
I believe my error is happening in the comparison line, but don't know why.
Any help would be great. I would like to stick with as much of my code as possible.

Thanks.

echo on
@title Month

FOR /F "tokens=2-4 delims=/-/ " %%A IN ("%date%") DO (
SET cwMonth=%%A
SET cwDay=%%B
SET cwYear=%%C
)

for /f "usebackq tokens=2 delims=/ " %%n in ('%date%') do set m=%%n
if %cwday% LEQ 03 set /A m=%m%-01
set "m=0%m%"
set "m=%m:~-2%"
if "%m%" == "01" set month=January
if "%m%" == "02" set month=February
if "%m%" == "03" set month=March
if "%m%" == "04" set month=April
if "%m%" == "05" set month=May
if "%m%" == "06" set month=June
if "%m%" == "07" set month=July
if "%m%" == "08" set month=August
if "%m%" == "09" set month=September
if "%m%" == "10" set month=October
if "%m%" == "11" set month=November
if "%m%" == "12" set month=December
set abc=%month%
set efg=Numbers.xls
set x=My
set y=Express
set z=File
set xyz=%abc% %x% %y% %z% %efg%
start /max "" "%xyz%"


See More: Manipulate MS DOS Dates

Report •

#1
August 4, 2014 at 20:10:18
Comparison works fine. The problem is the leading "0" during August / September. A leading zero denotes octal base, so the numerals "8" and "9" are not allowed. The text to int conversion fails, so you're left with a value of 0, which you subtract 1 from. This is why you debug your scripts with ECHO on.

Fewest byte changes would be this:

for /f "usebackq tokens=2 delims=/ " %%n in ('%date%') do set /a m=1%%n - 100

How To Ask Questions The Smart Way


Report •

#2
August 5, 2014 at 00:06:03
R2 got the octal snag.

You can save some clutter with this:

@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

set/p M= month number ?

:main
for /f "tokens=%M% delims= " %%a in ("JAN FEB MAR APR") do (
set Month=%%a
)
echo !Month!
goto :eof

=====================
M2 Golden-Triangle


Report •

#3
August 5, 2014 at 05:39:19
Thank you very much.

Report •

Related Solutions

#4
August 5, 2014 at 05:40:18
Thanks you very much to both of you

Report •

#5
October 4, 2014 at 06:09:53
Razor2.3

I made the change in my above code, inserting your suggestion:
for /f "usebackq tokens=2 delims=/ " %%n in ('%date%') do set /a m=1%%n - 100

This worked while in August and September, but this morning - October the 4th, I got an error code again. Why would that be?
(Using the windows calendar I changed the month back to September and everything worked. Changed it to October the 5th and everything worked).


Report •

#6
October 4, 2014 at 06:11:22
Would need to see your code; it's working for me.

How To Ask Questions The Smart Way


Report •

#7
October 4, 2014 at 06:13:08
rem opens current month American Express Budget xls
@echo off
@title Month
cd c:\American Express\2014 Budget\Monthly Log
FOR /F "tokens=2-4 delims=/-/ " %%A IN ("%date%") DO (
SET cwMonth=%%A
SET cwDay=%%B
SET cwYear=%%C
)
rem for /f "usebackq tokens=2 delims=/ " %%n in ('%date%') do set m=%%n
for /f "usebackq tokens=2 delims=/ " %%n in ('%date%') do set /a m=1%%n - 100
if "%cwday%" LEQ "03" set /A m=m-01
set "m=0%m%"
set "m=%m:~-2%"
if "%m%" == "01" set month=January
if "%m%" == "02" set month=February
if "%m%" == "03" set month=March
if "%m%" == "04" set month=April
if "%m%" == "05" set month=May
if "%m%" == "06" set month=June
if "%m%" == "07" set month=July
if "%m%" == "08" set month=August
if "%m%" == "09" set month=September
if "%m%" == "10" set month=October
if "%m%" == "11" set month=November
if "%m%" == "12" set month=December
echo.
set abc=%month%
set efg=Charges2.xls
set x=American
set y=Express
set z=Current
set xyz=%abc% %x% %y% %z% %efg%
start /max "" "%xyz%"


Report •

#8
October 4, 2014 at 06:35:13
Your suggested changes are correct and working.
I discovered I forgot to make your suggested changes ALSO in the template batch file.
Sorry for the miscommunication on this.
MY BAD.

Thanks again for your help.


Report •

Ask Question